NotifyController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Services\DyrPayService;
  5. use App\Services\PaymentService;
  6. use App\Services\RedisService;
  7. /**
  8. * 回调处理
  9. * Class NotifyController
  10. * @package App\Http\Controllers\Api
  11. */
  12. class NotifyController extends webApp
  13. {
  14. /**
  15. * 支付回调
  16. * @param string $scene 场景,deposit-保证金,depositRefund-保证金退款,withdraw-收入提现
  17. * @param int $payType 支付方式:10-微信,20-支付宝
  18. * @return array|false|\Psr\Http\Message\ResponseInterface
  19. */
  20. public function callback($scene, $payType = 10)
  21. {
  22. $date = date('Y-m-d H:i:s');
  23. $result = request()->post();
  24. $key = date('YmdHis') . '_' . rand(1000, 9999);
  25. $channel = $payType == 10? 'wechat':'alipay';
  26. if (empty($result)) {
  27. $result = file_get_contents('php://input');
  28. $result = $result ? json_decode($result, true) : [];
  29. }
  30. try {
  31. RedisService::set("caches:payments:notify_{$scene}:result_".date('YmdHis').rand(10,99), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'result' => $result], 7200);
  32. $pay = PaymentService::make()->createPay($scene, $payType);
  33. if (empty($pay) || empty($scene) || empty($payType)) {
  34. if (env('APP_DEBUG')) {
  35. logger()->channel($channel)->error("【{$date} 支付回调】参数错误:" . json_encode(['scene' => $scene, 'payType' => $payType, 'data' => $result], 256));
  36. }
  37. RedisService::set("caches:payments:notify_{$scene}:error_" . $key, ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'result' => $result], 7200);
  38. return response('failed');
  39. }
  40. // 验签和重组参数
  41. $data = $pay->callback();
  42. RedisService::set("caches:payments:notify_{$scene}:data_" . $key, ['all' => request()->all(), 'scene' => $scene,'data'=>$data, 'payType' => $payType, 'result' => $result], 7200);
  43. if (env('APP_DEBUG')) {
  44. logger()->channel($channel)->info("【{$date} 支付回调】验证结果:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256));
  45. }
  46. // 回调处理
  47. if (PaymentService::make()->catchNotify($scene, $payType, $data)) {
  48. RedisService::set("caches:payments:notify_{$scene}:success_" . $key, ['all' => request()->all(), 'scene' => $scene,'data'=>$data, 'payType' => $payType, 'result' => $result], 7200);
  49. logger()->channel('alipay')->info("【{$date} 支付回调】回调成功:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256));
  50. return response('success');
  51. } else {
  52. $msg = PaymentService::make()->getError();
  53. logger()->channel($channel)->info("【{$date} 支付回调】回调失败:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType, 'error' => lang($msg)], 256));
  54. return message($msg ? $msg : 2635, false);
  55. }
  56. } catch (\Exception $exception) {
  57. RedisService::set("caches:payments:notify_{$scene}:error_" . $key, ['all' => request()->all(), 'scene' => $scene,'payType' => $payType,'error' => $exception->getMessage(), 'data' => $result], 7200);
  58. logger()->channel($channel)->error("【{$date} 支付回调】回调错误:" . json_encode(['data' => $result,'scene'=>$scene,'payType'=>$payType, 'error' => $exception->getMessage()], 256));
  59. return message(2635, false, ['error' => $exception->getMessage()]);
  60. }
  61. }
  62. /**
  63. * 支付回调
  64. * @param int $productId 产品
  65. * @return array|false|\Psr\Http\Message\ResponseInterface
  66. */
  67. public function payCallback($productId)
  68. {
  69. $date = date('Y-m-d H:i:s');
  70. $result = isset($_POST)? $_POST : request()->post();
  71. $key = isset($result['out_trade_num'])?$result['out_trade_num'] : date('YmdHis') . '_' . rand(1000, 9999);
  72. $channel = 'dryPay';
  73. if (empty($result)) {
  74. $result = file_get_contents('php://input');
  75. $result = $result ? json_decode($result, true) : [];
  76. }
  77. try {
  78. $sign = isset($result['sign'])?$result['sign']:'';
  79. unset($result['sign']);//删除掉sign字段
  80. $checkSign = DyrPayService::make()->makeSign($result, 2);
  81. RedisService::set("caches:payments:payNotify_{$productId}:result_".$key, ['all' => request()->all(), 'sign'=>$sign,'checkSign'=>$checkSign, 'product_id' => $productId, 'result' => $result], 7200);
  82. if($sign != $checkSign){
  83. if (env('APP_DEBUG')) {
  84. logger()->channel($channel)->error("【{$date} 支付回调】签名错误:" . json_encode([ 'product_id' => $productId,'data' => $result], 256));
  85. }
  86. RedisService::set("caches:payments:payNotify_{$productId}:sign_" . $key, ['all' => request()->all(), 'product_id' => $productId, 'result' => $result], 7200);
  87. return response('failed');
  88. }
  89. // 验签和重组参数
  90. $data = $result;
  91. RedisService::set("caches:payments:payNotify_{$productId}:data_" . $key, ['all' => request()->all(),'product_id' => $productId, 'result' => $result], 7200);
  92. if (env('APP_DEBUG')) {
  93. logger()->channel($channel)->info("【{$date} 支付回调】验证结果:" . json_encode(['data' => $data,'product_id' => $productId],256));
  94. }
  95. // 回调处理
  96. if ($result = PaymentService::make()->catchPayNotify($productId, $data)) {
  97. RedisService::set("caches:payments:payNotify_{$productId}:success_" . $key, ['msg'=>PaymentService::make()->getError(),'all' => request()->all(), 'product_id' => $productId, 'data' => $data,'result'=>$result], 7200);
  98. logger()->channel($channel)->info("【{$date} 支付回调】回调成功:" . json_encode(['data' => $data,'product_id' => $productId],256));
  99. return response('success');
  100. } else {
  101. $msg = PaymentService::make()->getError();
  102. RedisService::set("caches:payments:payNotify_{$productId}:failed_" . $key, ['msg'=>$msg,'all' => request()->all(), 'product_id' => $productId, 'data' => $data], 7200);
  103. logger()->channel($channel)->info("【{$date} 支付回调】回调失败:" . json_encode(['data' => $data,'product_id' => $productId, 'error' => lang($msg)], 256));
  104. return message($msg ? $msg : 2635, false);
  105. }
  106. } catch (\Exception $exception) {
  107. RedisService::set("caches:payments:payNotify_{$productId}:error_" . $key, ['all' => request()->all(), 'product_id' => $productId, 'error' => $exception->getMessage(), 'data' => $result], 7200);
  108. logger()->channel($channel)->error("【{$date} 支付回调】回调错误:" . json_encode(['data' => $result,'product_id' => $productId, 'error' => $exception->getMessage()], 256));
  109. return message(2635, false, ['error' => $exception->getMessage()]);
  110. }
  111. }
  112. /**
  113. * 退款回调
  114. * @param string $scene 场景,refund-退款
  115. * @param int $payType 支付方式:10-微信,20-支付宝
  116. * @return array|false
  117. * @throws \Yansongda\Pay\Exception\ContainerException
  118. * @throws \Yansongda\Pay\Exception\InvalidParamsException
  119. */
  120. public function refund($payType = 10)
  121. {
  122. $scene = 'depositRefund';
  123. $date = date('Y-m-d H:i:s');
  124. $result = request()->post();
  125. $pay = PaymentService::make()->createPay('refund', $payType);
  126. $channel = $payType == 10? 'wechat':'alipay';
  127. if (empty($pay) || empty($scene) || empty($payType)) {
  128. if (env('APP_DEBUG')) {
  129. logger()->channel($channel)->error("【{$date} 退款回调】验证错误:" . json_encode(['scene' => $scene, 'payType' => $payType, 'data' => $result], 256));
  130. }
  131. RedisService::set("caches:refunds:notify_error_" . date('YmdH'), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'data' => $result], 7200);
  132. return false;
  133. }
  134. // 验签和重组参数
  135. $data = $pay->callback();
  136. RedisService::set("caches:refunds:notify_data_" . date('YmdH'), ['all' => request()->all(), 'scene' => $scene, 'payType' => $payType, 'data' => $result], 7200);
  137. if (env('APP_DEBUG')) {
  138. logger()->channel($channel)->info("【{$date} 退款回调】验证结果:" . json_encode($data, 256));
  139. }
  140. // 回调处理
  141. if (PaymentService::make()->catchNotify($scene, $payType, $data)) {
  142. logger()->channel($channel)->info("【{$date} 退款回调】回调成功:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType],256));
  143. return response('success');
  144. } else {
  145. $msg = PaymentService::make()->getError();
  146. logger()->channel($channel)->info("【{$date} 退款回调】回调失败:" . json_encode(['data' => $data,'scene'=>$scene,'payType'=>$payType, 'error' => lang($msg)], 256));
  147. return message($msg ? $msg : 2635, false);
  148. }
  149. }
  150. }