alipay.php 12 KB

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