123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use app\common\model\GoodsOrder;
- use app\common\service\OrderCallback;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- /**
- * 微信支付管理类
- * Class WeChatPay
- * @package app\api\controller\WeChatPay
- */
- class WeChatPay extends Controller
- {
- //小程序微信支付(type为1时是货主端微信配置,type为2时是接单端微信配置)
- public static function wxPay($name='订单支付',$out_trade_no,$total_fee,$notify_url,$trade_type = 'JSAPI',$openid = ''){
- try{
- if(empty($out_trade_no) || empty($total_fee) || empty($notify_url)) return false;
- $parameter = [
- 'body' => $name,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $total_fee*100,
- 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
- 'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
- 'openid' => $openid,
- ];
- switch ($trade_type){
- case 'JSAPI':
- $app = Factory::payment(config('app.wx_pay'));
- break;
- case 'NATIVE':
- $app = Factory::payment(config('app.wx_pay'));
- break;
- case 'APP':
- $app = Factory::payment(config('app.app_wx'));
- break;
- case 'MWEB':
- $app = Factory::payment(config('app.wx_pay'));
- break;
- case 'WAP':
- $app = Factory::payment(config('app.wx_pay'));
- break;
- }
- if(in_array($trade_type ,['JSAPI'])) $parameter['openid'] = $openid;
- if(in_array($trade_type ,['MWEB'])) $parameter['spbill_create_ip'] = request()->ip();
- $result = $app->order->unify($parameter);
- $jssdk = $app->jssdk;
- if($trade_type == 'APP') {
- $config = $jssdk->appConfig($result['prepay_id']);
- }else if($trade_type == 'MWEB'){
- $config = $jssdk->bridgeConfig($result['prepay_id']);
- }else{
- $config = $jssdk->sdkConfig($result['prepay_id']);
- }
- if(!is_array($config) && $config) $config = json_decode($config,true);
- return ['code'=> $config ? 200 : 201 , 'config'=>$config,'msg'=>'ok','mweb_url'=> isset($result['mweb_url']) ? $result['mweb_url']:''];
- }catch (\Exception $e){
- return ['code'=> 201,'config'=>[],'msg'=>$e->getMessage(),'mweb_url'=>''];
- }
- }
- /**
- * 商城订单支付回调
- */
- public function goodsOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'dd_goods_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- $order_info = GoodsOrder::with('orderItem')->where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::goodsOrderCallBack($order_info,1);// 支付完成后回调
- return $back_res['ret_val'];
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- // 开通会员支付回调
- public function LevelOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'dd_level_order');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- $order_info = \app\common\model\LevelOrder::where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::levelOrderCallBack($order_info,1);// 支付完成后回调
- return $back_res['ret_val'];
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- // 活动报名回调
- public function applyOrderNotify()
- {
- $app = Factory::payment(config('app.wx_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- $this->payResultLog($message,'dd_activity_apply');
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $pay_no = $message['out_trade_no'];
- // 如果订单不存在 或者 订单已经支付过了 告诉微信,我已经处理完了,订单没找到,别再通知我了
- if ($message['result_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- $order_info = \app\common\model\ActivityApply::where('pay_no',$pay_no)->find();
- if(!$order_info) return false;
- $order_info = $order_info->toArray();
- if($order_info['status'] != 0) return true;
- $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
- return $back_res['ret_val'];
- } else if ($message['return_code'] != 'SUCCESS'){
- return $fail('通信失败,请稍后再通知我');
- }
- });
- $response->send();
- }
- /**
- * 记录支付日志
- * @param $message
- * @param $table_name
- */
- public function payResultLog($message,$table_name)
- {
- // 回调记录
- $ret_arr = [];
- $ret_arr['transaction_id'] = isset($message['transaction_id']) ?$message['transaction_id']: '';
- $ret_arr['trade_no'] = isset($message['out_trade_no']) ?$message['out_trade_no']: '';
- $ret_arr['return_code'] = isset($message['return_code']) ?$message['return_code']: '';
- $ret_arr['result_code'] = isset($message['result_code']) ?$message['result_code']: '';
- $ret_arr['create_at'] = date('Y-m-d H:i:s');
- $ret_arr['order_table'] = $table_name;
- $ret_arr['result'] = json_encode($message);
- Db::name('order_pay_result')->insert($ret_arr);
- }
- }
|