AlipayTradeService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. //日志路径
  27. public $log_path;
  28. function __construct($alipay_config){
  29. $this->gateway_url = $alipay_config['gatewayUrl'];
  30. $this->appid = $alipay_config['app_id'];
  31. $this->private_key = $alipay_config['merchant_private_key'];
  32. $this->alipay_public_key = $alipay_config['alipay_public_key'];
  33. $this->charset = $alipay_config['charset'];
  34. $this->signtype=$alipay_config['sign_type'];
  35. $this->log_path = $alipay_config['log_path'];
  36. if(empty($this->appid)||trim($this->appid)==""){
  37. throw new Exception("appid should not be NULL!");
  38. }
  39. if(empty($this->private_key)||trim($this->private_key)==""){
  40. throw new Exception("private_key should not be NULL!");
  41. }
  42. if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
  43. throw new Exception("alipay_public_key should not be NULL!");
  44. }
  45. if(empty($this->charset)||trim($this->charset)==""){
  46. throw new Exception("charset should not be NULL!");
  47. }
  48. if(empty($this->gateway_url)||trim($this->gateway_url)==""){
  49. throw new Exception("gateway_url should not be NULL!");
  50. }
  51. }
  52. /**
  53. * alipay.trade.page.pay
  54. * @param $builder 业务参数,使用buildmodel中的对象生成。
  55. * @param $return_url 同步跳转地址,公网可以访问
  56. * @param $notify_url 异步通知地址,公网可以访问
  57. * @return $response 支付宝返回的信息
  58. */
  59. function pagePay($builder,$return_url,$notify_url) {
  60. $biz_content=$builder->getBizContent();
  61. //打印业务参数
  62. $this->writeLog($biz_content);
  63. $request = new AlipayTradePagePayRequest();
  64. $request->setNotifyUrl($notify_url);
  65. $request->setReturnUrl($return_url);
  66. $request->setBizContent ( $biz_content );
  67. // 首先调用支付api
  68. $response = $this->aopclientRequestExecute ($request,true);
  69. // $response = $response->alipay_trade_wap_pay_response;
  70. return $response;
  71. }
  72. /**
  73. * sdkClient
  74. * @param $request 接口请求参数对象。
  75. * @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。
  76. * @return $response 支付宝返回的信息
  77. */
  78. function aopclientRequestExecute($request,$ispage=false) {
  79. $aop = new AopClient ();
  80. $aop->gatewayUrl = $this->gateway_url;
  81. $aop->appId = $this->appid;
  82. $aop->rsaPrivateKey = $this->private_key;
  83. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  84. $aop->apiVersion ="1.0";
  85. $aop->postCharset = $this->charset;
  86. $aop->format= $this->format;
  87. $aop->signType=$this->signtype;
  88. // 开启页面信息输出
  89. $aop->debugInfo=true;
  90. if($ispage)
  91. {
  92. $result = $aop->pageExecute($request,"post");
  93. echo $result;
  94. }
  95. else
  96. {
  97. $result = $aop->Execute($request);
  98. }
  99. //打开后,将报文写入log文件
  100. $this->writeLog("response: ".var_export($result,true));
  101. return $result;
  102. }
  103. /**
  104. * alipay.trade.query (统一收单线下交易查询)
  105. * @param $builder 业务参数,使用buildmodel中的对象生成。
  106. * @return $response 支付宝返回的信息
  107. */
  108. function Query($builder){
  109. $biz_content=$builder->getBizContent();
  110. //打印业务参数
  111. $this->writeLog($biz_content);
  112. $request = new AlipayTradeQueryRequest();
  113. $request->setBizContent ( $biz_content );
  114. $response = $this->aopclientRequestExecute ($request);
  115. $response = $response->alipay_trade_query_response;
  116. return $response;
  117. }
  118. /**
  119. * alipay.trade.refund (统一收单交易退款接口)
  120. * @param $builder 业务参数,使用buildmodel中的对象生成。
  121. * @return $response 支付宝返回的信息
  122. */
  123. function Refund($builder){
  124. $biz_content=$builder->getBizContent();
  125. //打印业务参数
  126. $this->writeLog($biz_content);
  127. $request = new AlipayTradeRefundRequest();
  128. $request->setBizContent ( $biz_content );
  129. $response = $this->aopclientRequestExecute ($request);
  130. $response = $response->alipay_trade_refund_response;
  131. return $response;
  132. }
  133. /**
  134. * alipay.trade.close (统一收单交易关闭接口)
  135. * @param $builder 业务参数,使用buildmodel中的对象生成。
  136. * @return $response 支付宝返回的信息
  137. */
  138. function Close($builder){
  139. $biz_content=$builder->getBizContent();
  140. //打印业务参数
  141. $this->writeLog($biz_content);
  142. $request = new AlipayTradeCloseRequest();
  143. $request->setBizContent ( $biz_content );
  144. $response = $this->aopclientRequestExecute ($request);
  145. $response = $response->alipay_trade_close_response;
  146. return $response;
  147. }
  148. /**
  149. * 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
  150. * @param $builder 业务参数,使用buildmodel中的对象生成。
  151. * @return $response 支付宝返回的信息
  152. */
  153. function refundQuery($builder){
  154. $biz_content=$builder->getBizContent();
  155. //打印业务参数
  156. $this->writeLog($biz_content);
  157. $request = new AlipayTradeFastpayRefundQueryRequest();
  158. $request->setBizContent ( $biz_content );
  159. $response = $this->aopclientRequestExecute ($request);
  160. return $response;
  161. }
  162. /**
  163. * alipay.data.dataservice.bill.downloadurl.query (查询对账单下载地址)
  164. * @param $builder 业务参数,使用buildmodel中的对象生成。
  165. * @return $response 支付宝返回的信息
  166. */
  167. function downloadurlQuery($builder){
  168. $biz_content=$builder->getBizContent();
  169. //打印业务参数
  170. $this->writeLog($biz_content);
  171. $request = new alipaydatadataservicebilldownloadurlqueryRequest();
  172. $request->setBizContent ( $biz_content );
  173. $response = $this->aopclientRequestExecute ($request);
  174. $response = $response->alipay_data_dataservice_bill_downloadurl_query_response;
  175. return $response;
  176. }
  177. /**
  178. * 验签方法
  179. * @param $arr 验签支付宝返回的信息,使用支付宝公钥。
  180. * @return boolean
  181. */
  182. function check($arr){
  183. $aop = new AopClient();
  184. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  185. $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
  186. return $result;
  187. }
  188. /**
  189. * 请确保项目文件有可写权限,不然打印不了日志。
  190. */
  191. function writeLog($text) {
  192. // $text=iconv("GBK", "UTF-8//IGNORE", $text);
  193. //$text = characet ( $text );
  194. if(!empty($this->log_path) && trim($this->log_path)!=""){
  195. file_put_contents ( $this->log_path, date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
  196. }
  197. }
  198. }
  199. ?>