Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AlipayTradeService.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /* *
  3. * 功能:支付宝电脑网站支付
  4. * 版本:2.0
  5. * 修改日期:2017-05-01
  6. * 说明:
  7. * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
  8. */
  9. require_once dirname(dirname(dirname ( __FILE__ ))).'/AopSdk.php';
  10. class AlipayTradeService {
  11. //支付宝网关地址
  12. public $gateway_url = "https://openapi.alipay.com/gateway.do";
  13. //支付宝公钥
  14. public $alipay_public_key;
  15. //商户私钥
  16. public $private_key;
  17. //应用id
  18. public $appid;
  19. //编码格式
  20. public $charset = "UTF-8";
  21. public $token = NULL;
  22. //返回数据格式
  23. public $format = "json";
  24. //签名方式
  25. public $signtype = "RSA2";
  26. function __construct($alipay_config){
  27. $this->gateway_url = $alipay_config['gatewayUrl'];
  28. $this->appid = $alipay_config['app_id'];
  29. $this->private_key = $alipay_config['merchant_private_key'];
  30. $this->alipay_public_key = $alipay_config['alipay_public_key'];
  31. $this->charset = $alipay_config['charset'];
  32. $this->signtype=$alipay_config['sign_type'];
  33. if(empty($this->appid)||trim($this->appid)==""){
  34. throw new Exception("appid should not be NULL!");
  35. }
  36. if(empty($this->private_key)||trim($this->private_key)==""){
  37. throw new Exception("private_key should not be NULL!");
  38. }
  39. if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
  40. throw new Exception("alipay_public_key should not be NULL!");
  41. }
  42. if(empty($this->charset)||trim($this->charset)==""){
  43. throw new Exception("charset should not be NULL!");
  44. }
  45. if(empty($this->gateway_url)||trim($this->gateway_url)==""){
  46. throw new Exception("gateway_url should not be NULL!");
  47. }
  48. }
  49. /**
  50. * alipay.trade.page.pay
  51. * @param $builder 业务参数,使用buildmodel中的对象生成。
  52. * @param $return_url 同步跳转地址,公网可以访问
  53. * @param $notify_url 异步通知地址,公网可以访问
  54. * @return $response 支付宝返回的信息
  55. */
  56. function pagePay($builder,$return_url,$notify_url) {
  57. $biz_content=$builder->getBizContent();
  58. //打印业务参数
  59. $this->writeLog($biz_content);
  60. $request = new AlipayTradePagePayRequest();
  61. $request->setNotifyUrl($notify_url);
  62. $request->setReturnUrl($return_url);
  63. $request->setBizContent ( $biz_content );
  64. // 首先调用支付api
  65. $response = $this->aopclientRequestExecute ($request,true);
  66. // $response = $response->alipay_trade_wap_pay_response;
  67. return $response;
  68. }
  69. /**
  70. * alipay.trade.wap.pay
  71. * @param $builder 业务参数,使用buildmodel中的对象生成。
  72. * @param $return_url 同步跳转地址,公网可访问
  73. * @param $notify_url 异步通知地址,公网可以访问
  74. * @return $response 支付宝返回的信息
  75. */
  76. function wapPay($builder,$return_url,$notify_url) {
  77. $biz_content=$builder->getBizContent();
  78. //打印业务参数
  79. $this->writeLog($biz_content);
  80. $request = new AlipayTradeWapPayRequest();
  81. $request->setNotifyUrl($notify_url);
  82. $request->setReturnUrl($return_url);
  83. $request->setBizContent ( $biz_content );
  84. // 首先调用支付api
  85. $response = $this->aopclientRequestExecute ($request,true);
  86. // $response = $response->alipay_trade_wap_pay_response;
  87. return $response;
  88. }
  89. /**
  90. * sdkClient
  91. * @param $request 接口请求参数对象。
  92. * @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。
  93. * @return $response 支付宝返回的信息
  94. */
  95. function aopclientRequestExecute($request,$ispage=false) {
  96. $aop = new AopClient ();
  97. $aop->gatewayUrl = $this->gateway_url;
  98. $aop->appId = $this->appid;
  99. $aop->rsaPrivateKey = $this->private_key;
  100. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  101. $aop->apiVersion ="1.0";
  102. $aop->postCharset = $this->charset;
  103. $aop->format= $this->format;
  104. $aop->signType=$this->signtype;
  105. // 开启页面信息输出
  106. $aop->debugInfo=true;
  107. if($ispage)
  108. {
  109. $result = $aop->pageExecute($request,"post");
  110. echo $result;
  111. }
  112. else
  113. {
  114. $result = $aop->Execute($request);
  115. }
  116. //打开后,将报文写入log文件
  117. $this->writeLog("response: ".var_export($result,true));
  118. return $result;
  119. }
  120. /**
  121. * alipay.trade.query (统一收单线下交易查询)
  122. * @param $builder 业务参数,使用buildmodel中的对象生成。
  123. * @return $response 支付宝返回的信息
  124. */
  125. function Query($builder){
  126. $biz_content=$builder->getBizContent();
  127. //打印业务参数
  128. $this->writeLog($biz_content);
  129. $request = new AlipayTradeQueryRequest();
  130. $request->setBizContent ( $biz_content );
  131. $response = $this->aopclientRequestExecute ($request);
  132. $response = $response->alipay_trade_query_response;
  133. return $response;
  134. }
  135. /**
  136. * 用于后台验证新版支付宝信息是否正确
  137. * @param $builder 业务参数,使用buildmodel中的对象生成。
  138. * @return $response 支付宝返回的信息
  139. */
  140. function IsQuery($builder,$admin_pay){
  141. $biz_content=$builder->getBizContent();
  142. //打印业务参数
  143. $this->writeLog($biz_content);
  144. $request = new AlipayTradeQueryRequest();
  145. $request->setBizContent ( $biz_content );
  146. $response = $this->aopclientRequestExecute_new($request,$admin_pay);
  147. if ('支付宝APPID错误!' == $response) {
  148. return $response;
  149. }
  150. if ('支付宝公钥错误!' == $response) {
  151. return $response;
  152. }
  153. $response = $response->alipay_trade_query_response;
  154. if (empty($response)) {
  155. $error = $response->alipay_eco_mycar_parking_parkinglotinfo_create_response;
  156. if (!empty($error)) {
  157. $response = '验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配。';
  158. }
  159. }
  160. return $response;
  161. }
  162. /**
  163. * 用于后台验证新版支付宝信息是否正确
  164. * @param $request 接口请求参数对象。
  165. * @return $response 支付宝返回的信息
  166. */
  167. function aopclientRequestExecute_new($request,$admin_pay) {
  168. $aop = new AopClient ();
  169. $aop->gatewayUrl = $this->gateway_url;
  170. $aop->appId = $this->appid;
  171. $aop->rsaPrivateKey = $this->private_key;
  172. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  173. $aop->apiVersion ="1.0";
  174. $aop->postCharset = $this->charset;
  175. $aop->format= $this->format;
  176. $aop->signType=$this->signtype;
  177. // 开启页面信息输出
  178. $aop->debugInfo=true;
  179. $result = $aop->Execute($request,'','',$admin_pay);
  180. return $result;
  181. }
  182. /**
  183. * alipay.trade.refund (统一收单交易退款接口)
  184. * @param $builder 业务参数,使用buildmodel中的对象生成。
  185. * @return $response 支付宝返回的信息
  186. */
  187. function Refund($builder){
  188. $biz_content=$builder->getBizContent();
  189. //打印业务参数
  190. $this->writeLog($biz_content);
  191. $request = new AlipayTradeRefundRequest();
  192. $request->setBizContent ( $biz_content );
  193. $response = $this->aopclientRequestExecute ($request);
  194. $response = $response->alipay_trade_refund_response;
  195. return $response;
  196. }
  197. /**
  198. * alipay.trade.close (统一收单交易关闭接口)
  199. * @param $builder 业务参数,使用buildmodel中的对象生成。
  200. * @return $response 支付宝返回的信息
  201. */
  202. function Close($builder){
  203. $biz_content=$builder->getBizContent();
  204. //打印业务参数
  205. $this->writeLog($biz_content);
  206. $request = new AlipayTradeCloseRequest();
  207. $request->setBizContent ( $biz_content );
  208. $response = $this->aopclientRequestExecute ($request);
  209. $response = $response->alipay_trade_close_response;
  210. return $response;
  211. }
  212. /**
  213. * 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
  214. * @param $builder 业务参数,使用buildmodel中的对象生成。
  215. * @return $response 支付宝返回的信息
  216. */
  217. function refundQuery($builder){
  218. $biz_content=$builder->getBizContent();
  219. //打印业务参数
  220. $this->writeLog($biz_content);
  221. $request = new AlipayTradeFastpayRefundQueryRequest();
  222. $request->setBizContent ( $biz_content );
  223. $response = $this->aopclientRequestExecute ($request);
  224. return $response;
  225. }
  226. /**
  227. * alipay.data.dataservice.bill.downloadurl.query (查询对账单下载地址)
  228. * @param $builder 业务参数,使用buildmodel中的对象生成。
  229. * @return $response 支付宝返回的信息
  230. */
  231. function downloadurlQuery($builder){
  232. $biz_content=$builder->getBizContent();
  233. //打印业务参数
  234. $this->writeLog($biz_content);
  235. $request = new alipaydatadataservicebilldownloadurlqueryRequest();
  236. $request->setBizContent ( $biz_content );
  237. $response = $this->aopclientRequestExecute ($request);
  238. $response = $response->alipay_data_dataservice_bill_downloadurl_query_response;
  239. return $response;
  240. }
  241. /**
  242. * 验签方法
  243. * @param $arr 验签支付宝返回的信息,使用支付宝公钥。
  244. * @return boolean
  245. */
  246. function check($arr){
  247. $aop = new AopClient();
  248. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  249. $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
  250. return $result;
  251. }
  252. /**
  253. * 请确保项目文件有可写权限,不然打印不了日志。
  254. */
  255. function writeLog($text) {
  256. // $text=iconv("GBK", "UTF-8//IGNORE", $text); // 原先就注释
  257. //$text = characet ( $text ); // 原先就注释
  258. // file_put_contents ( dirname ( __FILE__ ).DIRECTORY_SEPARATOR."./../../log.txt", date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
  259. }
  260. }
  261. ?>