alipay.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class alipay {
  3. private $config;
  4. public function __construct($payment_info = array(), $order_info = array()) {
  5. if (!empty($payment_info)) {
  6. $this->config = array(
  7. //应用ID,您的APPID。
  8. 'app_id' => $payment_info['payment_config']['alipay_appid'],
  9. //商户私钥
  10. 'merchant_private_key' => $payment_info['payment_config']['private_key'],
  11. //异步通知地址
  12. 'notify_url' => str_replace('/index.php', '', HOME_SITE_URL) . '/payment/alipay_notify.html', //通知URL,
  13. //同步跳转
  14. 'return_url' => str_replace('/index.php', '', HOME_SITE_URL) . "/payment/alipay_return.html", //返回URL,
  15. //编码格式
  16. 'charset' => "UTF-8",
  17. //签名方式
  18. 'sign_type' => "RSA2",
  19. //支付宝网关
  20. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  21. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  22. 'app_cert_path' => $payment_info['payment_config']['app_cert_path'],
  23. 'alipay_cert_path' => $payment_info['payment_config']['alipay_cert_path'],
  24. 'root_cert_path' => $payment_info['payment_config']['root_cert_path'],
  25. );
  26. }
  27. }
  28. /**
  29. * 获取支付接口的请求地址
  30. *
  31. * @return string
  32. */
  33. public function get_payform($order_info) {
  34. require_once dirname(__FILE__) . '/aop/AopCertClient.php';
  35. require_once dirname(__FILE__) . '/aop/request/AlipayTradePagePayRequest.php';
  36. $aop = new AopCertClient ();
  37. $appCertPath = $this->config['app_cert_path'];
  38. $alipayCertPath = $this->config['alipay_cert_path'];
  39. $rootCertPath = $this->config['root_cert_path'];
  40. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  41. $aop->appId = $this->config['app_id'];
  42. $aop->rsaPrivateKey = $this->config['merchant_private_key'];
  43. $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
  44. $aop->apiVersion = '1.0';
  45. $aop->signType = 'RSA2';
  46. $aop->postCharset = 'utf-8';
  47. $aop->format = 'json';
  48. $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  49. $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  50. $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  51. $request = new AlipayTradePagePayRequest ();
  52. $request->setNotifyUrl($this->config['notify_url']);
  53. $request->setReturnUrl($this->config['return_url']);
  54. $request->setBizContent("{" .
  55. "\"out_trade_no\":\"" . $order_info['order_type'] . '-' . $order_info['pay_sn'] . "\"," .
  56. "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," .
  57. "\"total_amount\":" . round($order_info['api_pay_amount'],2) . "," .
  58. "\"subject\":\"" . $order_info['subject'] . "\"," .
  59. "\"body\":\"" . $order_info['order_type'] . "\"" .
  60. " }");
  61. $result = $aop->pageExecute($request);
  62. echo $result;exit;
  63. }
  64. public function return_verify() {
  65. require_once dirname(__FILE__) . '/aop/AopCertClient.php';
  66. $arr = $_GET;
  67. $return_result = array(
  68. 'trade_status' => '0',
  69. );
  70. $temp = explode('-', input('param.out_trade_no'));
  71. $out_trade_no = $temp['1']; //返回的支付单号
  72. $order_type = $temp['0'];
  73. $aop = new AopCertClient ();
  74. $appCertPath = $this->config['app_cert_path'];
  75. $alipayCertPath = $this->config['alipay_cert_path'];
  76. $rootCertPath = $this->config['root_cert_path'];
  77. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  78. $aop->appId = $this->config['app_id'];
  79. $aop->rsaPrivateKey = $this->config['merchant_private_key'];
  80. $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
  81. $aop->apiVersion = '1.0';
  82. $aop->signType = 'RSA2';
  83. $aop->postCharset = 'utf-8';
  84. $aop->format = 'json';
  85. $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  86. $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  87. $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  88. $result = $aop->rsaCheckV1($arr, $aop->alipayrsaPublicKey, $aop->signType);
  89. if ($result) {
  90. $return_result = array(
  91. 'out_trade_no' => $out_trade_no, #商户订单号
  92. 'trade_no' => input('param.trade_no'), #交易凭据单号
  93. 'total_fee' => input('param.total_amount'), #涉及金额
  94. 'order_type' => $order_type,
  95. 'trade_status' => '1',
  96. );
  97. }
  98. return $return_result;
  99. }
  100. public function verify_notify() {
  101. require_once dirname(__FILE__) . '/aop/AopCertClient.php';
  102. $arr = $_POST;
  103. $notify_result = array(
  104. 'trade_status' => '0',
  105. );
  106. $aop = new AopCertClient ();
  107. $appCertPath = $this->config['app_cert_path'];
  108. $alipayCertPath = $this->config['alipay_cert_path'];
  109. $rootCertPath = $this->config['root_cert_path'];
  110. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  111. $aop->appId = $this->config['app_id'];
  112. $aop->rsaPrivateKey = $this->config['merchant_private_key'];
  113. $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
  114. $aop->apiVersion = '1.0';
  115. $aop->signType = 'RSA2';
  116. $aop->postCharset = 'utf-8';
  117. $aop->format = 'json';
  118. $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  119. $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  120. $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  121. $result = $aop->rsaCheckV1($arr, $aop->alipayrsaPublicKey, $aop->signType);
  122. if ($result) {
  123. if ($arr['trade_status'] == 'TRADE_SUCCESS') {
  124. $out_trade_no = explode('-', input('param.out_trade_no'));
  125. $out_trade_no = $out_trade_no['1'];
  126. $notify_result = array(
  127. 'out_trade_no' => $out_trade_no, #商户订单号
  128. 'trade_no' => input('param.trade_no'), #交易凭据单号
  129. 'total_fee' => input('param.total_amount'), #涉及金额
  130. 'order_type' => input('param.body'),
  131. 'trade_status' => '1',
  132. );
  133. }
  134. }
  135. return $notify_result;
  136. }
  137. /**
  138. * 原路退款
  139. */
  140. public function trade_refund($order_info, $refund_amount) {
  141. require_once dirname(__FILE__) . '/aop/AopCertClient.php';
  142. require_once dirname(__FILE__) . '/aop/request/AlipayTradeRefundRequest.php';
  143. $aop = new AopCertClient ();
  144. $appCertPath = $this->config['app_cert_path'];
  145. $alipayCertPath = $this->config['alipay_cert_path'];
  146. $rootCertPath = $this->config['root_cert_path'];
  147. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  148. $aop->appId = $this->config['app_id'];
  149. $aop->rsaPrivateKey = $this->config['merchant_private_key'];
  150. $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
  151. $aop->apiVersion = '1.0';
  152. $aop->signType = 'RSA2';
  153. $aop->postCharset = 'utf-8';
  154. $aop->format = 'json';
  155. $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  156. $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  157. $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  158. $request = new AlipayTradeRefundRequest ();
  159. $request->setBizContent("{" .
  160. "\"out_request_no\":\"".$order_info['out_request_no']."\"," .
  161. "\"trade_no\":\"".$order_info['trade_no']."\"," .
  162. "\"refund_amount\":".$refund_amount."," .
  163. "\"refund_reason\":\"".'订单退款'."\"" .
  164. " }");
  165. $result = $aop->execute($request);
  166. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  167. $resultCode = $result->$responseNode->code;
  168. if (!empty($resultCode) && $resultCode == 10000) {
  169. return ds_callback(TRUE, $result->$responseNode->msg);
  170. } else {
  171. return ds_callback(FALSE, $result->$responseNode->sub_msg);
  172. }
  173. }
  174. /* 统一转账 */
  175. public function fund_transfer($withdraw_info) {
  176. require_once dirname(__FILE__) . '/aop/AopCertClient.php';
  177. require_once dirname(__FILE__) . '/aop/request/AlipayFundTransUniTransferRequest.php';
  178. /**
  179. * 证书类型AopCertClient功能方法使用测试,特别注意支付宝根证书预计2037年会过期,请在适当时间下载更新支付更证书
  180. * 1、execute 证书模式调用示例
  181. * 2、sdkExecute 证书模式调用示例
  182. * 3、pageExecute 证书模式调用示例
  183. */
  184. //1、execute 使用
  185. $aop = new AopCertClient ();
  186. $appCertPath = $this->config['app_cert_path'];
  187. $alipayCertPath = $this->config['alipay_cert_path'];
  188. $rootCertPath = $this->config['root_cert_path'];
  189. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  190. $aop->appId = $this->config['app_id'];
  191. $aop->rsaPrivateKey = $this->config['merchant_private_key'];
  192. $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
  193. $aop->apiVersion = '1.0';
  194. $aop->signType = 'RSA2';
  195. $aop->postCharset = 'utf-8';
  196. $aop->format = 'json';
  197. $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  198. $aop->appCertSN = $aop->getCertSN($appCertPath); //调用getCertSN获取证书序列号
  199. $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath); //调用getRootCertSN获取支付宝根证书序列号
  200. $request = new AlipayFundTransUniTransferRequest ();
  201. $request->setBizContent("{" .
  202. "\"out_biz_no\":\"" . $withdraw_info['pdc_sn'] . "\"," .
  203. "\"trans_amount\":" . $withdraw_info['pdc_amount'] . "," .
  204. "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
  205. "\"biz_scene\":\"DIRECT_TRANSFER\"," .
  206. "\"order_title\":\"" . config('ds_config.site_name') . "账户提现\"," .
  207. "\"original_order_id\":\"\"," .
  208. "\"payee_info\":{" .
  209. "\"identity\":\"" . $withdraw_info['pdc_bank_no'] . "\"," .
  210. "\"identity_type\":\"ALIPAY_LOGON_ID\"," .
  211. "\"name\":\"" . $withdraw_info['pdc_bank_user'] . "\"" .
  212. " }," .
  213. "\"remark\":\"\"" .
  214. " }");
  215. $result = $aop->execute($request);
  216. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  217. $resultCode = $result->$responseNode->code;
  218. if (!empty($resultCode) && $resultCode == 10000) {
  219. return ds_callback(TRUE, $result->$responseNode->msg,array('pdc_trade_sn'=>$result->$responseNode->order_id));
  220. } else {
  221. return ds_callback(FALSE, $result->$responseNode->sub_msg);
  222. }
  223. }
  224. }