AlipayTradeService.php 7.6 KB

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