123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use App\Models\Custom;
- use App\Models\Orders;
- use App\Facades\Servers\Logs\Log;
- use EasyWeChat\Factory;
- use Illuminate\Http\Request;
- use App\Servers\WechatPay\Payment;
- use App\Facades\Servers\WechatMini\Mini;
- use WeChatPay\Formatter;
- use WeChatPay\Crypto\AesGcm;
- use WeChatPay\Crypto\Rsa;
- /**
- * 微信支付接口
- *
- * @author JUN
- *
- * */
- class WechatPay extends Api{
- /**
- * 小程序微信支付下单 /api/wechat_pay/pay
- *
- * */
- public function pay(Custom $Custom)
- {
- // 检查登录
- $uid = $this->checkLogin();
- $code = request('code','');
- $orderId = request('order_id','');
- $orderInfo = Orders::query()->where('id','=',$orderId)->first()->toArray();
- if (empty($orderInfo))
- return json_send(['code'=>'error','msg'=>'订单不存在','data'=>['id'=>null]]);
- if ($orderInfo['custom_uid'] != $uid)
- return json_send(['code'=>'error','msg'=>'无权操作','data'=>['id'=>null]]);
- if ($orderInfo['status'] != 1)
- return json_send(['code'=>'error','msg'=>'订单已支付或取消','data'=>['id'=>null]]);
- //获取openid
- $result = Mini::jscode2session($code);
- if (!$result['openid']) return json_send(['code'=>'error','msg'=>'获取openid失败','data'=>$result['error']]);
- $openid = $result['openid'];
- $payment = new Payment();
- return $payment->pay(['out_trade_no' => $orderInfo['snowflake_id'],'openid' => $openid,'description' => '开邻智教课程','total_price' => $orderInfo['pay_total']]);
- }
-
- /**
- * 小程序微信支付回调 /api/wechat_pay/notify
- *
- * */
- public function notify(Custom $Custom)
- {
- $post_data = request();
- Log::log('notify_wechat_pay', 'post_data:' . $post_data);
- //获取headers参数
- $headers = request()->header();
- Log::log('notify_wechat_pay', '微信支付回调返回headers参数:' . json_encode($headers));
- $inWechatpaySignature = $headers['wechatpay-signature'];
- $inWechatpayTimestamp = $headers['wechatpay-timestamp'];
- $inWechatpaySerial = $headers['wechatpay-serial'];
- $inWechatpayNonce = $headers['wechatpay-nonce'];
- $inBody = $post_data;
- $apiv3Key = Config('wechat.APIV3');// 在商户平台上设置的APIv3密钥
- // 根据通知的平台证书序列号,查询本地平台证书文件,
- $platformCertificateFilePath = Config('wechat.platformCertificate');
- $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
- // 检查通知时间偏移量,允许5分钟之内的偏移
- $timeOffsetStatus = 300 >= abs(Formatter::timestamp() - (int)$inWechatpayTimestamp);
- Log::log('notify_wechat_pay', '时间偏移量:' . $timeOffsetStatus);
- $verifiedStatus = Rsa::verify(
- // 构造验签名串
- Formatter::joinedByLineFeed($inWechatpayTimestamp, $inWechatpayNonce, $inBody),
- $inWechatpaySignature,
- $platformPublicKeyInstance
- );
- Log::log('notify_wechat_pay', '验签:' . $verifiedStatus);
- $orderService = new OrderService();
- if ($timeOffsetStatus && $verifiedStatus) {
- // 转换通知的JSON文本消息为PHP Array数组
- $inBodyArray = (array)json_decode($inBody, true);
- // 使用PHP7的数据解构语法,从Array中解构并赋值变量
- ['resource' => [
- 'ciphertext' => $ciphertext,
- 'nonce' => $nonce,
- 'associated_data' => $aad
- ]] = $inBodyArray;
- // 加密文本消息解密
- $inBodyResource = AesGcm::decrypt($ciphertext, $apiv3Key, $nonce, $aad);
- // 把解密后的文本转换为PHP Array数组
- $inBodyResourceArray = (array)json_decode($inBodyResource, true);
- Log::log('notify_wechat_pay', '打印解密后的结果:' . json_encode($inBodyResourceArray));
- Log::log('notify_wechat_pay', '参数:' . $inBodyResourceArray['trade_state'] . '订单号' . $inBodyResourceArray['out_trade_no'] . '微信支付号' . $inBodyResourceArray['transaction_id']);
- if ($inBodyResourceArray['trade_state'] == "SUCCESS") {
- Log::log('notify_wechat_pay', '通知订单');
- $res = $orderService->payCallback($inBodyResourceArray['out_trade_no'], $inBodyResourceArray['transaction_id']);
- Log::log('notify_wechat_pay', '通知返回' . json_encode($res));
- }
- }
- }
- }
|