PayCallbackController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-10-2
  6. * Time: 下午8:27
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Docter;
  11. use App\Models\Order;
  12. use App\Models\Payment;
  13. use App\Models\User;
  14. use App\Models\UserBalanceLog;
  15. use DB;
  16. use EasyWeChat\Factory;
  17. class PayCallbackController extends Controller
  18. {
  19. public function wechatPayNotify()
  20. {
  21. $app = Factory::payment(config('config.wechat_pay'));
  22. $response = $app->handlePaidNotify(function($message, $fail){
  23. // 使用通知里的 "交易单号" 去自己的数据库找到订单
  24. $payment = Payment::where('trade_sn', $message['out_trade_no'])->first();
  25. // 如果订单不存在 或者 订单已经支付过了
  26. if (empty($payment) || $payment['status'] != 1) {
  27. return true; // 告诉微信,我已经处理完了或者订单没找到,别再通知我了
  28. }
  29. //使用通知里的 "微信支付订单号" 去自己的数据库找是否已经存在
  30. if(Payment::where('online_sn', $message['transaction_id'])->exists()){
  31. return true; // 告诉微信,我已经处理过这单了,别再通知我了
  32. }
  33. //建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付
  34. // return_code 表示通信状态,不代表支付状态
  35. if ($message['return_code'] === 'SUCCESS') {
  36. // 用户是否支付成功
  37. if ($message['result_code'] === 'SUCCESS') {
  38. // 启动事务
  39. DB::beginTransaction();
  40. try {
  41. //更新payment
  42. Payment::where('id', $payment['id'])->update(['online_sn' => $message['transaction_id'], 'status' => 2, 'payment_time' => time()]);
  43. $order = Order::select(['id', 'user_id', 'docter_id', 'product_type', 'payment_amount'])->where('id', $payment['order_id'])->first();
  44. $order_status = 2;
  45. if ($order['product_type'] == 7) {
  46. $order_status = 3;
  47. //改变用户余额
  48. User::changeBalance($order['user_id'], $order['payment_amount'], 2, $order['id'], '充值');
  49. }
  50. //更新订单
  51. Order::where('id', $payment['order_id'])->update([
  52. 'order_status' => $order_status,
  53. 'payment_status' => 2,
  54. 'payment_time' => time(),
  55. ]);
  56. if (!empty($order['docter_id'])) {
  57. //更新医生的服务人数
  58. Docter::where('id', $order['docter_id'])->increment('service_persons');
  59. }
  60. // 提交事务
  61. DB::commit();
  62. } catch (\Exception $e) {
  63. // 回滚事务
  64. DB::rollback();
  65. trace(['微信支付回调错误' => $e->getMessage(), '错误数据' => $payment], 'error');
  66. return $fail('内部服务失败,请稍后再通知我');
  67. }
  68. }
  69. elseif ($message['result_code'] === 'FAIL') {
  70. $err_code = $message['err_code'] ?? '';
  71. $err_code_des = $message['err_code_des'] ?? '';
  72. $remark = $err_code.':'.$err_code_des;
  73. Payment::where('id', $payment['id'])->update(['remark' => $remark, 'status' => 3, 'payment_time' => time()]);
  74. }
  75. }
  76. else {
  77. trace(['微信支付回调通知数据错误,返回码return_code:' => $message['return_code'], '错误数据' => $payment], 'error');
  78. return $fail('通信失败,请稍后再通知我');
  79. }
  80. return true; // 返回处理完成
  81. });
  82. return $response;
  83. }
  84. }