NotifyController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace addons\JoinPay\api\controllers;
  3. use addons\JoinPay\common\global_func\payment_logic\JoinPay;
  4. use addons\JoinPay\common\global_func\payment_logic\JoinPayFastPay;
  5. use yii\web\Controller;
  6. use Yii;
  7. use Exception;
  8. use common\helpers\FileHelper;
  9. use common\helpers\ArrayHelper;
  10. use common\enums\PayTypeEnum;
  11. /**
  12. * 支付回调
  13. *
  14. * Class NotifyController
  15. * @package frontend\controllers
  16. */
  17. class NotifyController extends Controller
  18. {
  19. //use PayNotify;
  20. /**
  21. * 关闭csrf
  22. *
  23. * @var bool
  24. */
  25. public $enableCsrfValidation = false;
  26. /**
  27. * 公用支付回调 - 汇聚支付
  28. * @return string
  29. */
  30. public function actionJoinpay()
  31. {
  32. $params = \Yii::$app->request->get();
  33. $params = array_map('urldecode', $params);
  34. $logPath = $this->getLogPath('joinpay');
  35. $ab_url = \Yii::$app->request->absoluteUrl;
  36. FileHelper::writeLog($logPath, var_export($params, true) . PHP_EOL . $ab_url);
  37. try{
  38. $my_param = !empty($params['r5_Mp']) ? json_decode($params['r5_Mp'],true) : []; //json格式
  39. //获取对应支付配置
  40. $mall_id = $my_param['mall_id'] ?? 0;
  41. $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.joinpay', true);
  42. $config = json_decode($config, true);
  43. //调用支付类:回调方法
  44. // $pay = Yii::$app->payment->joinpay($config);
  45. $pay = new JoinPay($config);
  46. $message = $pay->notify();
  47. FileHelper::writeLog($logPath, var_export($message, true));
  48. if ($this->pay($message,PayTypeEnum::JOINPAY)) {
  49. die('success');
  50. }
  51. die('操作失败');
  52. }catch(\Exception $e){
  53. // 记录报错日志
  54. $logPath = $this->getLogPath('error');
  55. FileHelper::writeLog($logPath, $e->getMessage());
  56. die('操作失败'); // 通知响应
  57. }
  58. }
  59. /**
  60. * 公用支付回调 - 汇聚快捷支付
  61. * @return string
  62. */
  63. public function actionJoinpayFastpay()
  64. {
  65. try{
  66. $post = \Yii::$app->request->post();
  67. $logPath = $this->getLogPath('joinpay-fastpay');
  68. FileHelper::writeLog($logPath, json_encode($post,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
  69. if(empty($post) || empty($post['data'])){
  70. die('操作失败'); // 通知响应
  71. }
  72. $data = json_decode($post['data'],true);
  73. $callback_param = json_decode($data['callback_param'],true);
  74. $mall_id = $callback_param['mall_id'] ?? 0;
  75. //获取对应支付配置
  76. $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.joinpay_fast', true);
  77. $config = json_decode($config, true);
  78. // 这种不解耦的写法已废弃
  79. // $pay = Yii::$app->payment->joinpayFastPay($config);
  80. $pay = new JoinPayFastPay($config);
  81. $message = $pay->notify($post);
  82. if(!$message){
  83. die('操作失败');
  84. }
  85. if ($this->pay($message,PayTypeEnum::JOINPAY_FAST_PAY)) {
  86. die('success');
  87. }
  88. die('操作失败');
  89. }catch(\Exception $e){
  90. // 记录报错日志
  91. $logPath = $this->getLogPath('error');
  92. FileHelper::writeLog($logPath, $e->getMessage());
  93. die('操作失败'); // 通知响应
  94. }
  95. }
  96. /**
  97. * 支付公用方法
  98. * @param $message
  99. * @param int $pay_type 支付类型
  100. * @return bool
  101. */
  102. protected function pay($message,$pay_type)
  103. {
  104. $transaction = Yii::$app->db->beginTransaction();
  105. try {
  106. if (!($payLog = Yii::$app->services->pay->getByTradeNo($message['out_trade_no']))) {
  107. throw new Exception('找不到支付信息');
  108. }
  109. // 支付完成
  110. if ($payLog->is_pay == 1) {
  111. return true;
  112. }
  113. //支付金额验证
  114. if(bccomp($message['pay_fee'], $payLog->getOnlinePayFee()) !== 0){
  115. throw new Exception('支付金额不一致');
  116. }
  117. $payLog->pay_type = $pay_type;
  118. $payLog->is_pay = 1;
  119. $payLog->pay_time = time();
  120. $payLog->third_trade_no = $message['trade_no'] ?? '';
  121. $payLog->second_trade_no = $message['r9_BankTrxNo'] ?? '';
  122. $payLog->trade_type = $message['trade_type'] ?? '';
  123. $logPath = $this->getLogPath('joinpay');
  124. FileHelper::writeLog($logPath, var_export(ArrayHelper::toArray($payLog), true));
  125. if (!$res = $payLog->save()) {
  126. throw new Exception('日志修改失败');
  127. }
  128. $transaction->commit();
  129. FileHelper::writeLog($logPath, 'update-res => ' . var_export($res, true));
  130. // 业务回调
  131. $orders = Yii::$app->services->pay->changePaymentTradeOrder($payLog,$pay_type);
  132. Yii::$app->services->pay->notify($orders);
  133. return true;
  134. } catch (\Exception $e) {
  135. $transaction->rollBack();
  136. //FileHelper::writeLog($this->getLogPath('error'), $e->getMessage());
  137. // 记录报错日志
  138. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  139. 'message' => $e->getMessage(),
  140. 'line' => $e->getLine(),
  141. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  142. 'trace' => $e->getTrace(),
  143. 'code' => $e->getCode(),
  144. ])));
  145. return false;
  146. }
  147. }
  148. /**
  149. * @param $type
  150. * @return string
  151. */
  152. protected function getLogPath($type)
  153. {
  154. return Yii::getAlias('@runtime') . "/pay-logs/" . date('Y_m_d') . '/' . $type . '.txt';
  155. }
  156. }