WechatPay.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Custom;
  4. use App\Models\Orders;
  5. use App\Facades\Servers\WechatMini\MiniPay;
  6. use EasyWeChat\Factory;
  7. use Illuminate\Http\Request;
  8. /**
  9. * 微信接口
  10. *
  11. * @author 刘相欣
  12. *
  13. * */
  14. class WechatPay extends Api{
  15. /**
  16. * 小程序微信支付下单 /api/wechat_pay/unifiedorder
  17. *
  18. * */
  19. public function unifiedorder($request)
  20. {
  21. $data = [];
  22. $result = MiniPay::unifiedOrder($data);
  23. return $result;
  24. }
  25. /**
  26. * 小程序微信支付回调 /api/wechat_pay/notify
  27. *
  28. * */
  29. public function notify($request)
  30. {
  31. $app = Factory::payment(config('wechat.mini_pay'));
  32. $response = $app->handlePaidNotify(function ($message, $fail) {
  33. // 支付成功的处理逻辑
  34. $order = Orders::query()->where('out_trade_no','=',$message['out_trade_no'])->first();
  35. // 如果订单不存在
  36. if (!$order) {
  37. return true;
  38. }
  39. $data = [];
  40. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  41. // 用户是否支付成功
  42. if ($message['result_code'] === 'SUCCESS') {
  43. $order->paid_at = time(); // 更新支付时间为当前时间
  44. $order->status = 'paid';
  45. // 用户支付失败
  46. } elseif ($message['result_code']=== 'FAIL') {
  47. $order->status = 'paid_fail';
  48. }
  49. } else {
  50. return $fail('通信失败,请稍后再通知我');
  51. }
  52. $fail('Order not exists.');
  53. return true; // 返回 true 表示处理成功
  54. });
  55. return $response;
  56. }
  57. }