Alipay.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. Dever::apply('sdk/alipay', 'pay');
  4. class Alipay extends Core
  5. {
  6. private $signType = 'RSA2';
  7. public function __construct($config)
  8. {
  9. $project = Dever::project('pay');
  10. # 通知接口
  11. $config['notify'] = $this->url($config['type'], $config['id']);
  12. $this->config = $config;
  13. }
  14. /**
  15. * 通知
  16. */
  17. public function notify()
  18. {
  19. $type = Dever::input('type');
  20. if ($type == 1) {
  21. $input = $_GET;
  22. unset($input['account_id']);
  23. $result = $this->check($input);
  24. if ($result) {
  25. $status = 1;
  26. } else {
  27. $status = 2;
  28. }
  29. $refer = Dever::input('refer');
  30. $refer = urldecode($refer) . '&status=' . $status;
  31. Dever::location($refer);
  32. } else {
  33. $input = $_POST;
  34. if (!$input) {
  35. echo 'fail';die;
  36. }
  37. unset($input['account_id']);
  38. $this->log('支付回调-初始化', $input);
  39. $result = $this->check($input);
  40. if ($result) {
  41. $this->log('支付回调-获取数据', $result);
  42. if($input['trade_status'] == 'TRADE_FINISHED') {
  43. #退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
  44. $this->updateOrder($result['out_trade_no'], $result['total_amount'], '已超过退款期限');
  45. } elseif ($input['trade_status'] == 'TRADE_SUCCESS') {
  46. #付款完成后,支付宝系统发送该交易状态通知
  47. $this->updateOrder($result['out_trade_no'], $result['total_amount']);
  48. }
  49. echo 'success';die;
  50. } else {
  51. echo 'fail';die;
  52. }
  53. }
  54. }
  55. /**
  56. * 获取统一下单的基本信息
  57. */
  58. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false, $refer = false)
  59. {
  60. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  61. $request['out_trade_no'] = $order_id;
  62. $request['body'] = $name;
  63. $request['total_amount'] = $cash;
  64. $request['subject'] = $name;
  65. $request['timeout_express'] = $this->config['timeout'];
  66. $content = json_encode($request, JSON_UNESCAPED_UNICODE);
  67. $request = new \AlipayTradeWapPayRequest();
  68. $request->setNotifyUrl($this->config['notify']);
  69. $request->setReturnUrl($this->config['refer']);
  70. $request->setBizContent($content);
  71. if ($type == 1) {
  72. $result = $this->aop(2)->pageExecute($request, 'post');
  73. } else {
  74. $result = $this->aop(2)->Execute($request);
  75. }
  76. return $result;
  77. }
  78. private function aop()
  79. {
  80. $type = 1;
  81. if ($this->config['appCertPath'] && $this->config['alipayCertPath'] && $this->config['rootCertPath']) {
  82. $type = 2;
  83. }
  84. if ($type == 2) {
  85. $this->aop = new \AopCertClient();
  86. } else {
  87. $this->aop = new \AopClient();
  88. }
  89. $this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  90. if ($this->config['box'] == 2) {
  91. $this->aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
  92. }
  93. $this->aop->appId = $this->config['appid'];
  94. $this->aop->signType = $this->signType;
  95. // 开启页面信息输出
  96. $this->aop->debugInfo = true;
  97. if ($type == 2) {
  98. $appCertPath = Dever::local($this->config['appCertPath']);
  99. $alipayCertPath = Dever::local($this->config['alipayCertPath']);
  100. $rootCertPath = Dever::local($this->config['rootCertPath']);
  101. if (is_file($appCertPath)) {
  102. $this->aop->alipayrsaPublicKey = $this->aop->getPublicKey($alipayCertPath);
  103. $this->aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
  104. $this->aop->appCertSN = $this->aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
  105. $this->aop->alipayRootCertSN = $this->aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
  106. }
  107. } else {
  108. $this->aop->rsaPrivateKey = $this->config['private_key'];
  109. $this->aop->alipayrsaPublicKey = $this->config['public_key'];
  110. }
  111. return $this->aop;
  112. }
  113. /**
  114. * 获取二维码支付
  115. */
  116. public function qrcode($order, $refer)
  117. {
  118. $notify = new \NativePay();
  119. $result = $notify->GetPayUrl($order);
  120. $url = $result['code_url'];
  121. return $url;
  122. }
  123. /**
  124. * 获取小程序支付
  125. */
  126. public function applet($order)
  127. {
  128. $result = array();
  129. return $result;
  130. }
  131. /**
  132. * 获取页面支付
  133. */
  134. public function page($order, $refer)
  135. {
  136. return $order;
  137. }
  138. private function check($data)
  139. {
  140. $result = $this->aop()->rsaCheckV1($data, $this->config['public_key'], $this->signType);
  141. return $result;
  142. }
  143. # 退款
  144. public function refund($order_id, $cash, $order, $refund_order_id = false)
  145. {
  146. $out_trade_no = $order_id;
  147. $cash = $cash * 100;
  148. $total_fee = $cash;
  149. $refund_fee = $cash;
  150. $input = new \WxPayRefund();
  151. $input->SetOut_trade_no($out_trade_no);
  152. $input->SetTotal_fee($total_fee);
  153. $input->SetRefund_fee($refund_fee);
  154. $input->SetOut_refund_no($out_trade_no . '_' . time());
  155. $input->SetOp_user_id($this->config->GetMerchantId());
  156. $result = \WxPayApi::refund($this->config, $input);
  157. return $result;
  158. }
  159. }