Blame view

addons/exam/controller/Notify.php 5.24 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  <?php

  

  namespace addons\exam\controller;

  

  use addons\exam\enum\PayStatus;

  use addons\exam\library\PaymentCallbackService;

  use addons\exam\library\WechatService;

  use app\admin\model\exam\PayLogModel;

  

  class Notify extends Base

  {

      protected $noNeedLogin = ['*'];

      protected $noNeedRight = ['*'];

  

      /**

       * 微信支付回调

       */

      public function pay()

      {

          $app      = WechatService::getPayment();

          $response = $app->handlePaidNotify(function ($message, $fail) {

              // 测试回调日志

              @file_put_contents('test_notify.txt',

                  json_encode(

                      [

                          'message' => $message,

                          'fail'    => $fail,

                      ], JSON_UNESCAPED_UNICODE

                  ) . PHP_EOL, FILE_APPEND);

  

              // 支付记录

              $log = PayLogModel::create([

                  'openid'         => $message['openid'],

                  'mch_id'         => $message['mch_id'],

                  'out_trade_no'   => $message['out_trade_no'],

                  'pay_money'      => $message['total_fee'] / 100,

                  'transaction_id' => $message['transaction_id'],

                  'app_id'         => $message['appid'],

                  'status'         => PayStatus::PAID,

                  'pay_time'       => time(),

                  'response'       => json_encode($message),

              ]);

  

              try {

                  if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态

                      // 用户是否支付成功

                      if ($message['result_code'] === 'SUCCESS') {

                          // 统一处理订单回调

                          $order = PaymentCallbackService::handleOrder($message);

  

                          // 支付订单相关信息

                          $log->user_id      = $order->user_id;

                          $log->payable_id   = $order->id;

                          $log->payable_type = get_class($order);

                          $log->save();

  

                      } else {

                          // 用户支付失败

                          $this->handleError($log, 'result_code FAIL');

                      }

                  } else {

                      return $fail('通信失败,请稍后再通知我');

                  }

              } catch (\Exception $ex) {

                  $this->handleError($log, $ex->getMessage());

              }

  

              return true;

          });

  

          $response->send();

      }

  

      /**

       * 记录回调错误信息

       * @param $log

       * @param $error_message

       * @return void

       */

      protected function handleError($log, $error_message)

      {

          $log->error_message = $error_message;

          $log->error_time    = time();

          $log->save();

  

          // 记录错误信息

          @file_put_contents('test_notify.txt',

              json_encode(

                  [

                      'message' => $error_message,

                  ]

              ) . PHP_EOL, FILE_APPEND);

      }

  

      // public function test()

      // {

      //     $message = json_decode('{"appid":"wxc4ae6c234d8090df","attach":"OPEN_MEMBER","bank_type":"OTHERS","cash_fee":"1","fee_type":"CNY","is_subscribe":"N","mch_id":"1540427141","nonce_str":"62d406a77ba83","openid":"oCCAp40GLKsahGW0JW5GIS8q13Eg","out_trade_no":"M2022071720550347","result_code":"SUCCESS","return_code":"SUCCESS","sign":"D71588207AD91372535F006F71B64B2B","time_end":"20220717205513","total_fee":"1","trade_type":"JSAPI","transaction_id":"4200001489202207174465227746"}', true);

      //

      //

      //     // 支付记录

      //     $log = PayLogModel::create([

      //         'openid'         => $message['openid'],

      //         'mch_id'         => $message['mch_id'],

      //         'out_trade_no'   => $message['out_trade_no'],

      //         'pay_money'      => $message['total_fee'] / 100,

      //         'transaction_id' => $message['transaction_id'],

      //         'app_id'         => $message['appid'],

      //         'response'       => json_encode($message),

      //         'pay_time'       => time(),

      //     ]);

      //

      //     try {

      //         if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态

      //             // 用户是否支付成功

      //             if ($message['result_code'] === 'SUCCESS') {

      //                 // 统一处理订单回调

      //                 $order = PaymentCallbackService::handleOrder($message);

      //

      //                 // 支付订单相关信息

      //                 $log->user_id      = $order->user_id;

      //                 $log->payable_id   = $order->id;

      //                 $log->payable_type = get_class($order);

      //                 $log->save();

      //

      //             } else {

      //                 // 用户支付失败

      //                 $this->handleError($log, 'result_code FAIL');

      //             }

      //         } else {

      //             dd('通信失败,请稍后再通知我');

      //         }

      //     } catch (\Exception $ex) {

      //         $this->handleError($log, $ex->getMessage());

      //     }

      //

      //     return 1;

      // }

  }