AlipayTradeService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* *
  3. * 功能:支付宝手机网站alipay.trade.close (统一收单交易关闭接口)业务参数封装
  4. * 版本:2.0
  5. * 修改日期:2016-11-01
  6. * 说明:
  7. * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
  8. */
  9. require_once dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'./../../AopSdk.php';
  10. require dirname ( __FILE__ ).DIRECTORY_SEPARATOR.'./../../config.php';
  11. class AlipayTradeService {
  12. //支付宝网关地址
  13. public $gateway_url = "https://openapi.alipay.com/gateway.do";
  14. //支付宝公钥
  15. public $alipay_public_key;
  16. //商户私钥
  17. public $private_key;
  18. //应用id
  19. public $appid;
  20. //编码格式
  21. public $charset = "UTF-8";
  22. public $token = NULL;
  23. //返回数据格式
  24. public $format = "json";
  25. //签名方式
  26. public $signtype = "RSA";
  27. function __construct($alipay_config){
  28. $this->gateway_url = $alipay_config['gatewayUrl'];
  29. $this->appid = $alipay_config['app_id'];
  30. $this->private_key = $alipay_config['merchant_private_key'];
  31. $this->alipay_public_key = $alipay_config['alipay_public_key'];
  32. $this->charset = $alipay_config['charset'];
  33. $this->signtype=$alipay_config['sign_type'];
  34. if(empty($this->appid)||trim($this->appid)==""){
  35. throw new Exception("appid should not be NULL!");
  36. }
  37. if(empty($this->private_key)||trim($this->private_key)==""){
  38. throw new Exception("private_key should not be NULL!");
  39. }
  40. if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
  41. throw new Exception("alipay_public_key should not be NULL!");
  42. }
  43. if(empty($this->charset)||trim($this->charset)==""){
  44. throw new Exception("charset should not be NULL!");
  45. }
  46. if(empty($this->gateway_url)||trim($this->gateway_url)==""){
  47. throw new Exception("gateway_url should not be NULL!");
  48. }
  49. }
  50. function AlipayWapPayService($alipay_config) {
  51. $this->__construct($alipay_config);
  52. }
  53. /**
  54. * alipay.trade.wap.pay
  55. * @param $builder 业务参数,使用buildmodel中的对象生成。
  56. * @param $return_url 同步跳转地址,公网可访问
  57. * @param $notify_url 异步通知地址,公网可以访问
  58. * @return $response 支付宝返回的信息
  59. */
  60. function wapPay($builder,$return_url,$notify_url) {
  61. $biz_content=$builder->getBizContent();
  62. //打印业务参数
  63. $this->writeLog($biz_content);
  64. $request = new AlipayTradeWapPayRequest();
  65. $request->setNotifyUrl($notify_url);
  66. $request->setReturnUrl($return_url);
  67. $request->setBizContent ( $biz_content );
  68. // 首先调用支付api
  69. $response = $this->aopclientRequestExecute ($request,true);
  70. // $response = $response->alipay_trade_wap_pay_response;
  71. return $response;
  72. }
  73. function aopclientRequestExecute($request,$ispage=false) {
  74. $aop = new AopClient ();
  75. $aop->gatewayUrl = $this->gateway_url;
  76. $aop->appId = $this->appid;
  77. $aop->rsaPrivateKey = $this->private_key;
  78. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  79. $aop->apiVersion ="1.0";
  80. $aop->postCharset = $this->charset;
  81. $aop->format= $this->format;
  82. $aop->signType=$this->signtype;
  83. // 开启页面信息输出
  84. $aop->debugInfo=true;
  85. if($ispage)
  86. {
  87. $result = $aop->pageExecute($request,"post");
  88. echo $result;
  89. }
  90. else
  91. {
  92. $result = $aop->Execute($request);
  93. }
  94. //打开后,将报文写入log文件
  95. $this->writeLog("response: ".var_export($result,true));
  96. return $result;
  97. }
  98. /**
  99. * alipay.trade.query (统一收单线下交易查询)
  100. * @param $builder 业务参数,使用buildmodel中的对象生成。
  101. * @return $response 支付宝返回的信息
  102. */
  103. function Query($builder){
  104. $biz_content=$builder->getBizContent();
  105. //打印业务参数
  106. $this->writeLog($biz_content);
  107. $request = new AlipayTradeQueryRequest();
  108. $request->setBizContent ( $biz_content );
  109. // 首先调用支付api
  110. $response = $this->aopclientRequestExecute ($request);
  111. $response = $response->alipay_trade_query_response;
  112. var_dump($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. // 首先调用支付api
  127. $response = $this->aopclientRequestExecute ($request);
  128. $response = $response->alipay_trade_refund_response;
  129. var_dump($response);
  130. return $response;
  131. }
  132. /**
  133. * alipay.trade.close (统一收单交易关闭接口)
  134. * @param $builder 业务参数,使用buildmodel中的对象生成。
  135. * @return $response 支付宝返回的信息
  136. */
  137. function Close($builder){
  138. $biz_content=$builder->getBizContent();
  139. //打印业务参数
  140. $this->writeLog($biz_content);
  141. $request = new AlipayTradeCloseRequest();
  142. $request->setBizContent ( $biz_content );
  143. // 首先调用支付api
  144. $response = $this->aopclientRequestExecute ($request);
  145. $response = $response->alipay_trade_close_response;
  146. var_dump($response);
  147. return $response;
  148. }
  149. /**
  150. * 退款查询 alipay.trade.fastpay.refund.query (统一收单交易退款查询)
  151. * @param $builder 业务参数,使用buildmodel中的对象生成。
  152. * @return $response 支付宝返回的信息
  153. */
  154. function refundQuery($builder){
  155. $biz_content=$builder->getBizContent();
  156. //打印业务参数
  157. $this->writeLog($biz_content);
  158. $request = new AlipayTradeFastpayRefundQueryRequest();
  159. $request->setBizContent ( $biz_content );
  160. // 首先调用支付api
  161. $response = $this->aopclientRequestExecute ($request);
  162. var_dump($response);
  163. return $response;
  164. }
  165. /**
  166. * alipay.data.dataservice.bill.downloadurl.query (查询对账单下载地址)
  167. * @param $builder 业务参数,使用buildmodel中的对象生成。
  168. * @return $response 支付宝返回的信息
  169. */
  170. function downloadurlQuery($builder){
  171. $biz_content=$builder->getBizContent();
  172. //打印业务参数
  173. $this->writeLog($biz_content);
  174. $request = new alipaydatadataservicebilldownloadurlqueryRequest();
  175. $request->setBizContent ( $biz_content );
  176. // 首先调用支付api
  177. $response = $this->aopclientRequestExecute ($request);
  178. $response = $response->alipay_data_dataservice_bill_downloadurl_query_response;
  179. var_dump($response);
  180. return $response;
  181. }
  182. /**
  183. * 验签方法
  184. * @param $arr 验签支付宝返回的信息,使用支付宝公钥。
  185. * @return boolean
  186. */
  187. function check($arr){
  188. $aop = new AopClient();
  189. $aop->alipayrsaPublicKey = $this->alipay_public_key;
  190. $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
  191. return $result;
  192. }
  193. //请确保项目文件有可写权限,不然打印不了日志。
  194. function writeLog($text) {
  195. // $text=iconv("GBK", "UTF-8//IGNORE", $text);
  196. //$text = characet ( $text );
  197. file_put_contents ( dirname ( __FILE__ ).DIRECTORY_SEPARATOR."./../../log.txt", date ( "Y-m-d H:i:s" ) . " " . $text . "\r\n", FILE_APPEND );
  198. }
  199. /** *利用google api生成二维码图片
  200. * $content:二维码内容参数
  201. * $size:生成二维码的尺寸,宽度和高度的值
  202. * $lev:可选参数,纠错等级
  203. * $margin:生成的二维码离边框的距离
  204. */
  205. function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') {
  206. $content = urlencode($content);
  207. $image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&amp;cht=qr&chld='.$lev.'|'.$margin.'&amp;chl='.$content.'" widht="'.$size.'" height="'.$size.'" />';
  208. return $image;
  209. }
  210. }
  211. ?>