AlipayTradeService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * sdkClient
  71. * @param $request 接口请求参数对象。
  72. * @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。
  73. * @return $response 支付宝返回的信息
  74. */
  75. function aopclientRequestExecute($request,$ispage=false) {
  76. $aop = new AopClient ();
  77. $aop->gatewayUrl = $this->gateway_url;
  78. $aop->appId = $this->appid;
  79. $aop->rsaPrivateKey = $this->private_key;
  80. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  81. $aop->apiVersion ="1.0";
  82. $aop->postCharset = $this->charset;
  83. $aop->format= $this->format;
  84. $aop->signType=$this->signtype;
  85. // 开启页面信息输出
  86. $aop->debugInfo=true;
  87. if($ispage)
  88. {
  89. $result = $aop->pageExecute($request,"post");
  90. }
  91. else
  92. {
  93. $result = $aop->Execute($request);
  94. }
  95. //打开后,将报文写入log文件
  96. $this->writeLog("response: ".var_export($result,true));
  97. return $result;
  98. }
  99. /**
  100. * alipay.trade.query (统一收单线下交易查询)
  101. * @param $builder 业务参数,使用buildmodel中的对象生成。
  102. * @return $response 支付宝返回的信息
  103. */
  104. function Query($builder){
  105. $biz_content=$builder->getBizContent();
  106. //打印业务参数
  107. $this->writeLog($biz_content);
  108. $request = new AlipayTradeQueryRequest();
  109. $request->setBizContent ( $biz_content );
  110. $response = $this->aopclientRequestExecute ($request);
  111. $response = $response->alipay_trade_query_response;
  112. return $response;
  113. }
  114. /**
  115. * alipay.trade.refund (统一收单交易退款接口)
  116. * @param $builder 业务参数,使用buildmodel中的对象生成。
  117. * @return $response 支付宝返回的信息
  118. */
  119. function Refund($builder){
  120. $biz_content=$builder->getBizContent();
  121. //打印业务参数
  122. $this->writeLog($biz_content);
  123. $request = new AlipayTradeRefundRequest();
  124. $request->setBizContent ( $biz_content );
  125. $response = $this->aopclientRequestExecute ($request);
  126. $response = $response->alipay_trade_refund_response;
  127. return $response;
  128. }
  129. /**
  130. * alipay.trade.close (统一收单交易关闭接口)
  131. * @param $builder 业务参数,使用buildmodel中的对象生成。
  132. * @return $response 支付宝返回的信息
  133. */
  134. function Close($builder){
  135. $biz_content=$builder->getBizContent();
  136. //打印业务参数
  137. $this->writeLog($biz_content);
  138. $request = new AlipayTradeCloseRequest();
  139. $request->setBizContent ( $biz_content );
  140. $response = $this->aopclientRequestExecute ($request);
  141. $response = $response->alipay_trade_close_response;
  142. return $response;
  143. }
  144. /**
  145. * 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
  146. * @param $builder 业务参数,使用buildmodel中的对象生成。
  147. * @return $response 支付宝返回的信息
  148. */
  149. function refundQuery($builder){
  150. $biz_content=$builder->getBizContent();
  151. //打印业务参数
  152. $this->writeLog($biz_content);
  153. $request = new AlipayTradeFastpayRefundQueryRequest();
  154. $request->setBizContent ( $biz_content );
  155. $response = $this->aopclientRequestExecute ($request);
  156. return $response;
  157. }
  158. /**
  159. * alipay.data.dataservice.bill.downloadurl.query (查询对账单下载地址)
  160. * @param $builder 业务参数,使用buildmodel中的对象生成。
  161. * @return $response 支付宝返回的信息
  162. */
  163. function downloadurlQuery($builder){
  164. $biz_content=$builder->getBizContent();
  165. //打印业务参数
  166. $this->writeLog($biz_content);
  167. $request = new alipaydatadataservicebilldownloadurlqueryRequest();
  168. $request->setBizContent ( $biz_content );
  169. $response = $this->aopclientRequestExecute ($request);
  170. $response = $response->alipay_data_dataservice_bill_downloadurl_query_response;
  171. return $response;
  172. }
  173. /**
  174. * 验签方法
  175. * @param $arr 验签支付宝返回的信息,使用支付宝公钥。
  176. * @return boolean
  177. */
  178. function check($arr){
  179. $aop = new AopClient();
  180. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  181. $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
  182. return $result;
  183. }
  184. /**
  185. * 请确保项目文件有可写权限,不然打印不了日志。
  186. */
  187. function writeLog($text) {
  188. // $text=iconv("GBK", "UTF-8//IGNORE", $text);
  189. //$text = characet ( $text );
  190. file_put_contents ( dirname ( __FILE__ ).DIRECTORY_SEPARATOR."./../../log.txt", date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
  191. }
  192. }
  193. ?>