新版订单消耗系统

AlipayTradeService.php 6.7KB

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