Alipay.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\Base;
  4. use app\common\model\GoodsOrder;
  5. use app\common\service\OrderCallback;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Model;
  9. use Alipay\EasySDK\Kernel\Factory;
  10. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  11. use Alipay\EasySDK\Kernel\Config;
  12. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Factory.php';
  13. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Config.php';
  14. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/EasySDKKernel.php';
  15. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/AlipayConstants.php';
  16. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/ResponseChecker.php';
  17. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/JsonUtil.php';
  18. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/Signer.php';
  19. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Client.php';
  20. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/App/Models/AlipayTradeAppPayResponse.php';
  21. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Client.php';
  22. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Kernel/Util/PageUtil.php';
  23. require_once env('root_path').'vendor/alipaysdk/easysdk/php/src/Payment/Page/Models/AlipayTradePagePayResponse.php';
  24. class Alipay extends Base
  25. {
  26. protected static $appid = [
  27. 'H5'=>'2021003155604810',
  28. 'APP'=>'2021003155604810'
  29. ];
  30. protected static $merchantPr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gkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAidau5/jnsnry1+3VDvTOO7S+Zsi1u+a0uNvacXXHAh78TQIWPBEt7U/XP7yh89+UDOZQPknxJjpfo53ciOAowEeGFH9O8NimtvSFMMF9iCQijmlhXyOCfZstLyrVRojcZTgv4//AjV877DrUBTsonpeGGSi92Ssl8COPWbr461UtYBCWjAK+UiZsh01ZI5LNTvt8bVyOWPhWDEcPvxxrkTqUF6qRO9Bvr169V0tZxiJgDLu891Ofim/TlD+tHgnOI9NfWQafRddylPCTzIHbaxZyQ0JmNJleROSCMNOpVZiWZX8+mQmhEpeodJ3MqYmAqN4Dla7fN54Y2wWHaRNkGwIDAQAB',
  36. 'APP'=>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAidau5/jnsnry1+3VDvTOO7S+Zsi1u+a0uNvacXXHAh78TQIWPBEt7U/XP7yh89+UDOZQPknxJjpfo53ciOAowEeGFH9O8NimtvSFMMF9iCQijmlhXyOCfZstLyrVRojcZTgv4//AjV877DrUBTsonpeGGSi92Ssl8COPWbr461UtYBCWjAK+UiZsh01ZI5LNTvt8bVyOWPhWDEcPvxxrkTqUF6qRO9Bvr169V0tZxiJgDLu891Ofim/TlD+tHgnOI9NfWQafRddylPCTzIHbaxZyQ0JmNJleROSCMNOpVZiWZX8+mQmhEpeodJ3MqYmAqN4Dla7fN54Y2wWHaRNkGwIDAQAB',
  37. ];
  38. /**
  39. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  40. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  41. * @throws \GuzzleHttp\Exception\GuzzleException
  42. * 支付宝支付
  43. */
  44. public static function ali_pay($subject,$outTradeNo,$totalAmount,$notifyUrl){
  45. Factory::setOptions(self::getOptions($notifyUrl,"APP"));
  46. try {
  47. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  48. //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
  49. $result = Factory::payment()->App()->pay($subject,$outTradeNo , $totalAmount);
  50. $responseChecker = new ResponseChecker();
  51. //3. 处理响应或异常
  52. if ($responseChecker->success($result)) {
  53. return $result->body;
  54. } else {
  55. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  56. }
  57. } catch (Exception $e) {
  58. return false;
  59. }
  60. }
  61. public static function ali_pay_pc($subject,$outTradeNo,$totalAmount,$notifyUrl,$returnUrl){
  62. Factory::setOptions(self::getOptions($notifyUrl,'H5'));
  63. try {
  64. $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
  65. $responseChecker = new ResponseChecker();
  66. //3. 处理响应或异常
  67. if ($responseChecker->success($result)) {
  68. return $result->body;
  69. } else {
  70. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  71. }
  72. } catch (Exception $e) {
  73. return false;
  74. }
  75. }
  76. public static function getOptions($notifyUrl,$type = 'H5'){
  77. $options = new Config();
  78. $options->protocol = 'https';
  79. $options->gatewayHost = 'openapi.alipay.com';
  80. $options->signType = 'RSA2';
  81. $options->appId = static::$appid[$type];
  82. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  83. $options->merchantPrivateKey = static::$merchantPrivateKey[$type];
  84. /*$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
  85. $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
  86. $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';*/
  87. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  88. $options->alipayPublicKey = static::$merchantPrivateKey[$type];
  89. //可设置异步通知接收服务地址(可选)
  90. $options->notifyUrl = $notifyUrl;
  91. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  92. $options->encryptKey = "";
  93. return $options;
  94. }
  95. // 会员开通回调
  96. public function LevelOrderNotify()
  97. {
  98. $result = input('post.');
  99. $this->payResultLog($result,'dd_level_order');
  100. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  101. $pay_no = $result['out_trade_no'];
  102. $order_info = \app\common\model\LevelOrder::where('pay_no',$pay_no)->find();
  103. if(!$order_info) return false;
  104. $order_info = $order_info->toArray();
  105. if($order_info['status'] != 0) return true;
  106. $back_res = OrderCallback::levelOrderCallBack($order_info,1);// 支付完成后回调
  107. return $back_res['ret_val'];
  108. }
  109. }
  110. // 活动报名回调
  111. public function applyOrderNotify()
  112. {
  113. $result = input('post.');
  114. $this->payResultLog($result,'dd_activity_apply');
  115. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  116. $pay_no = $result['out_trade_no'];
  117. $order_info = \app\common\model\ActivityApply::where('pay_no',$pay_no)->find();
  118. if(!$order_info) return false;
  119. $order_info = $order_info->toArray();
  120. if($order_info['status'] != 0) return true;
  121. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  122. return $back_res['ret_val'];
  123. }
  124. }
  125. // 商品支付回调
  126. public function goodsOrderNotify()
  127. {
  128. $result = input('post.');
  129. $this->payResultLog($result,'dd_goods_order');
  130. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  131. $pay_no = $result['out_trade_no'];
  132. $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
  133. if(!$order_info) return false;
  134. $order_info = $order_info->toArray();
  135. if($order_info['status'] != 0) return true;
  136. $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
  137. return $back_res['ret_val'];
  138. }
  139. }
  140. /**
  141. * 记录支付日志
  142. * @param $message
  143. * @param $table_name
  144. */
  145. public function payResultLog($message,$table_name)
  146. {
  147. // 回调记录
  148. $ret_arr = [];
  149. $ret_arr['transaction_id'] = isset($message['trade_no']) ?$message['trade_no']: '';
  150. $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
  151. $ret_arr['return_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
  152. $ret_arr['result_code'] = isset($message['trade_status']) ?$message['trade_status']: '';
  153. $ret_arr['create_at'] = date('Y-m-d H:i:s');
  154. $ret_arr['order_table'] = $table_name;
  155. $ret_arr['result'] = json_encode($message);
  156. $ret_arr['pay_type'] = 'zfb';
  157. Db::name('order_pay_result')->insert($ret_arr);
  158. }
  159. }