WeChatPay.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. var_dump($parameter);
  44. switch ($trade_type){
  45. case 'JSAPI':
  46. $app = Factory::payment(config('app.wx_pay'));
  47. break;
  48. case 'NATIVE':
  49. $app = Factory::payment(config('app.wx_pay'));
  50. break;
  51. case 'APP':
  52. $app = Factory::payment(config('app.app_wx'));
  53. break;
  54. case 'MWEB':
  55. $app = Factory::payment(config('app.wx_pay'));
  56. break;
  57. case 'WAP':
  58. $app = Factory::payment(config('app.wx_pay'));
  59. break;
  60. }
  61. if(in_array($trade_type ,['JSAPI'])) $parameter['openid'] = $openid;
  62. if(in_array($trade_type ,['MWEB'])) $parameter['spbill_create_ip'] = request()->ip();
  63. $result = $app->order->unify($parameter);
  64. var_dump($result);
  65. $jssdk = $app->jssdk;
  66. if($trade_type == 'APP') {
  67. $config = $jssdk->appConfig($result['prepay_id']);
  68. }else if($trade_type == 'MWEB'){
  69. $config = $jssdk->bridgeConfig($result['prepay_id']);
  70. }else{
  71. $config = $jssdk->sdkConfig($result['prepay_id']);
  72. }
  73. if(!is_array($config) && $config) $config = json_decode($config,true);
  74. return ['code'=> $config ? 200 : 201 , 'config'=>$config,'msg'=>'ok','mweb_url'=> isset($result['mweb_url']) ? $result['mweb_url']:''];
  75. }catch (\Exception $e){
  76. return ['code'=> 201,'config'=>[],'msg'=>$e->getMessage(),'mweb_url'=>''];
  77. }
  78. }
  79. /**
  80. * 商城订单支付回调
  81. */
  82. public function goodsOrderNotify()
  83. {
  84. $app = Factory::payment(config('app.wx_pay'));
  85. $response = $app->handlePaidNotify(function ($message, $fail) {
  86. $this->payResultLog($message,'dd_goods_order');
  87. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  88. $pay_no = $message['out_trade_no'];
  89. // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
  90. if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  91. $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
  92. if(!$order_info) return false;
  93. $order_info = $order_info->toArray();
  94. if($order_info['status'] != 0) return true;
  95. $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
  96. return $back_res['ret_val'];
  97. } else if ($message['return_code'] != 'SUCCESS'){
  98. return $fail('通信失败,请稍后再通知我');
  99. }
  100. });
  101. $response->send();
  102. }
  103. /**
  104. * 洗鞋订单支付
  105. */
  106. public function washOrderNotify()
  107. {
  108. $app = Factory::payment(config('app.wx_pay'));
  109. $response = $app->handlePaidNotify(function ($message, $fail) {
  110. $this->payResultLog($message,'dd_wash_order');
  111. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  112. $pay_no = $message['out_trade_no'];
  113. // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
  114. if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  115. $order_info = \app\common\model\WashOrder::with('orderItem')->where('pay_no',$pay_no)->find();
  116. if(!$order_info) return false;
  117. $order_info = $order_info->toArray();
  118. if($order_info['status'] != 0) return true;
  119. $back_res = OrderCallback::washOrderCallBack($order_info,1);// 支付完成后回调
  120. return $back_res['ret_val'];
  121. } else if ($message['return_code'] != 'SUCCESS'){
  122. return $fail('通信失败,请稍后再通知我');
  123. }
  124. });
  125. $response->send();
  126. }
  127. /**
  128. * 记录支付日志
  129. * @param $message
  130. * @param $table_name
  131. */
  132. public function payResultLog($message,$table_name)
  133. {
  134. // 回调记录
  135. $ret_arr = [];
  136. $ret_arr['transaction_id'] = isset($message['transaction_id']) ?$message['transaction_id']: '';
  137. $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
  138. $ret_arr['return_code'] = isset($message['return_code']) ?$message['return_code']: '';
  139. $ret_arr['result_code'] = isset($message['result_code']) ?$message['result_code']: '';
  140. $ret_arr['create_at'] = date('Y-m-d H:i:s');
  141. $ret_arr['order_table'] = $table_name;
  142. $ret_arr['result'] = json_encode($message);
  143. Db::name('order_pay_result')->insert($ret_arr);
  144. }
  145. }