WeChatPay.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\model\GoodsOrder;
  16. use app\common\service\OrderCallback;
  17. use EasyWeChat\Factory;
  18. use think\Controller;
  19. use think\Db;
  20. use think\Exception;
  21. use AlibabaCloud\Client\AlibabaCloud;
  22. use AlibabaCloud\Client\Exception\ClientException;
  23. use AlibabaCloud\Client\Exception\ServerException;
  24. /**
  25. * 微信支付管理类
  26. * Class WeChatPay
  27. * @package app\api\controller\WeChatPay
  28. */
  29. class WeChatPay extends Controller
  30. {
  31. //小程序微信支付(type为1时是货主端微信配置,type为2时是接单端微信配置)
  32. public static function wxPay($name='订单支付',$out_trade_no,$total_fee,$notify_url,$trade_type = 'JSAPI',$openid = ''){
  33. try{
  34. if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)) return false;
  35. $parameter = [
  36. 'body' => $name,
  37. 'out_trade_no' => $out_trade_no,
  38. 'total_fee' => $total_fee*100,
  39. 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  40. 'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
  41. 'openid' => $openid,
  42. ];
  43. switch ($trade_type){
  44. case 'JSAPI':
  45. $app = Factory::payment(config('app.wx_pay'));
  46. break;
  47. case 'NATIVE':
  48. $app = Factory::payment(config('app.wx_pay'));
  49. break;
  50. case 'APP':
  51. $app = Factory::payment(config('app.app_wx'));
  52. break;
  53. case 'MWEB':
  54. $app = Factory::payment(config('app.wx_pay'));
  55. break;
  56. case 'WAP':
  57. $app = Factory::payment(config('app.wx_pay'));
  58. break;
  59. }
  60. if(in_array($trade_type ,['JSAPI'])) $parameter['openid'] = $openid;
  61. if(in_array($trade_type ,['MWEB'])) $parameter['spbill_create_ip'] = request()->ip();
  62. $result = $app->order->unify($parameter);
  63. $jssdk = $app->jssdk;
  64. if($trade_type == 'APP') {
  65. $config = $jssdk->appConfig($result['prepay_id']);
  66. }else if($trade_type == 'MWEB'){
  67. $config = $jssdk->bridgeConfig($result['prepay_id']);
  68. }else{
  69. $config = $jssdk->sdkConfig($result['prepay_id']);
  70. }
  71. if(!is_array($config) && $config) $config = json_decode($config,true);
  72. return ['code'=> $config ? 200 : 201 , 'config'=>$config,'msg'=>'ok','mweb_url'=> isset($result['mweb_url']) ? $result['mweb_url']:'' ,'pc_pay_url'=> isset($result['code_url']) ? $result['code_url']:''];
  73. }catch (\Exception $e){
  74. return ['code'=> 201,'config'=>[],'msg'=>$e->getMessage(),'mweb_url'=>''];
  75. }
  76. }
  77. /**
  78. * 商城订单支付回调
  79. */
  80. public function goodsOrderNotify()
  81. {
  82. $app = Factory::payment(config('app.wx_pay'));
  83. $response = $app->handlePaidNotify(function ($message, $fail) {
  84. $this->payResultLog($message,'dd_goods_order');
  85. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  86. $pay_no = $message['out_trade_no'];
  87. // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
  88. if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  89. $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
  90. if(!$order_info) return false;
  91. $order_info = $order_info->toArray();
  92. if($order_info['status'] != 0) return true;
  93. $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
  94. return $back_res['ret_val'];
  95. } else if ($message['return_code'] != 'SUCCESS'){
  96. return $fail('通信失败,请稍后再通知我');
  97. }
  98. });
  99. $response->send();
  100. }
  101. // 开通会员支付回调
  102. public function LevelOrderNotify()
  103. {
  104. $app = Factory::payment(config('app.wx_pay'));
  105. $response = $app->handlePaidNotify(function ($message, $fail) {
  106. $this->payResultLog($message,'dd_level_order');
  107. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  108. $pay_no = $message['out_trade_no'];
  109. // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
  110. if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  111. $order_info = \app\common\model\LevelOrder::where('pay_no',$pay_no)->find();
  112. if(!$order_info) return false;
  113. $order_info = $order_info->toArray();
  114. if($order_info['status'] != 0) return true;
  115. $back_res = OrderCallback::levelOrderCallBack($order_info,1);// 支付完成后回调
  116. return $back_res['ret_val'];
  117. } else if ($message['return_code'] != 'SUCCESS'){
  118. return $fail('通信失败,请稍后再通知我');
  119. }
  120. });
  121. $response->send();
  122. }
  123. // 活动报名回调
  124. public function applyOrderNotify()
  125. {
  126. $app = Factory::payment(config('app.wx_pay'));
  127. $response = $app->handlePaidNotify(function ($message, $fail) {
  128. $this->payResultLog($message,'dd_activity_apply');
  129. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  130. $pay_no = $message['out_trade_no'];
  131. // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
  132. if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  133. $order_info = \app\common\model\ActivityApply::where('pay_no',$pay_no)->find();
  134. if(!$order_info) return false;
  135. $order_info = $order_info->toArray();
  136. if($order_info['status'] != 0) return true;
  137. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  138. return $back_res['ret_val'];
  139. } else if ($message['return_code'] != 'SUCCESS'){
  140. return $fail('通信失败,请稍后再通知我');
  141. }
  142. });
  143. $response->send();
  144. }
  145. /**
  146. * 记录支付日志
  147. * @param $message
  148. * @param $table_name
  149. */
  150. public function payResultLog($message,$table_name)
  151. {
  152. // 回调记录
  153. $ret_arr = [];
  154. $ret_arr['transaction_id'] = isset($message['transaction_id']) ?$message['transaction_id']: '';
  155. $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
  156. $ret_arr['return_code'] = isset($message['return_code']) ?$message['return_code']: '';
  157. $ret_arr['result_code'] = isset($message['result_code']) ?$message['result_code']: '';
  158. $ret_arr['create_at'] = date('Y-m-d H:i:s');
  159. $ret_arr['order_table'] = $table_name;
  160. $ret_arr['result'] = json_encode($message);
  161. Db::name('order_pay_result')->insert($ret_arr);
  162. }
  163. }