123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use App\Models\Custom;
- use App\Models\Orders;
- use App\Facades\Servers\WechatMini\MiniPay;
- use EasyWeChat\Factory;
- use Illuminate\Http\Request;
- /**
- * 微信接口
- *
- * @author 刘相欣
- *
- * */
- class WechatPay extends Api{
- /**
- * 小程序微信支付下单 /api/wechat_pay/unifiedorder
- *
- * */
- public function unifiedorder($request)
- {
- $data = [];
- $result = MiniPay::unifiedOrder($data);
- return $result;
- }
-
- /**
- * 小程序微信支付回调 /api/wechat_pay/notify
- *
- * */
- public function notify($request)
- {
- $app = Factory::payment(config('wechat.mini_pay'));
- $response = $app->handlePaidNotify(function ($message, $fail) {
- // 支付成功的处理逻辑
- $order = Orders::query()->where('out_trade_no','=',$message['out_trade_no'])->first();
- // 如果订单不存在
- if (!$order) {
- return true;
- }
- $data = [];
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- if ($message['result_code'] === 'SUCCESS') {
- $order->paid_at = time(); // 更新支付时间为当前时间
- $order->status = 'paid';
- // 用户支付失败
- } elseif ($message['result_code']=== 'FAIL') {
- $order->status = 'paid_fail';
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- $fail('Order not exists.');
- return true; // 返回 true 表示处理成功
- });
- return $response;
- }
- }
|