PayPlusController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\logic\api\PayPlusCashierLogic;
  4. use App\Http\logic\api\PayPlusLogic;
  5. use App\Inter\PayMentInterFace;
  6. use App\Notification\TelegramBot;
  7. use App\Services\PayPlus;
  8. use App\Util;
  9. use App\Utility\SetNXLock;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Redis;
  13. class PayPlusController implements PayMentInterFace
  14. {
  15. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  16. {
  17. $logic = new PayPlusLogic();
  18. try {
  19. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
  20. } catch (\Exception $exception) {
  21. Redis::set('PayErro_PayPlus', 1);
  22. Redis::expire('PayErro_PayPlus', 600);
  23. Util::WriteLog('PayPlus_error', $exception->getMessage());
  24. TelegramBot::getDefault()->sendProgramNotify('PayPlus Except ', $exception->getMessage(), $exception);
  25. return apiReturnFail($logic->getError() ?: 'Payment processing error');
  26. }
  27. if (!empty($res) && isset($res['code']) && (int) $res['code'] == 200 && !empty($res['decryptedComponentDelta']['link'])) {
  28. return apiReturnSuc([
  29. 'content' => $res['decryptedComponentDelta']['link'],
  30. 'money' => $payAmt,
  31. 'prdOrdNo' => $res['decryptedComponentDelta']['order_id'] ?? '',
  32. ]);
  33. }
  34. return apiReturnFail($logic->getError() ?: 'Payment processing error');
  35. }
  36. public function notify(Request $request)
  37. {
  38. $post = $request->all();
  39. $content = (string) $request->getContent();
  40. $post = json_decode($content, true) ?: $post;
  41. Util::WriteLog('PayPlus', 'pay notify: ' . $content);
  42. Util::WriteLog('PayPlus', 'pay notify header: ' . json_encode($request->headers->all()));
  43. $OrderId = $post['data']['reference_id'] ?? '';
  44. if (str_starts_with($OrderId, 'tx')) {
  45. return $this->cash_notify($request);
  46. }
  47. $orderSn = $post['data']['platform_order_id'] ?? '';
  48. if ($orderSn === '') {
  49. return 'success';
  50. }
  51. $lockKey = 'pay_notify_PayPlus_' . $orderSn;
  52. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  53. return 'success';
  54. }
  55. try {
  56. return (new PayPlusLogic())->notify($post);
  57. } catch (\Exception $exception) {
  58. Redis::set('PayErro_PayPlus', 1);
  59. Redis::expire('PayErro_PayPlus', 600);
  60. TelegramBot::getDefault()->sendProgramNotify('PayPlus notify exception', json_encode($post), $exception);
  61. Util::WriteLog('PayPlus_error', $exception->getMessage());
  62. return 'success';
  63. } finally {
  64. SetNXLock::release($lockKey);
  65. }
  66. }
  67. public function sync_notify(Request $request)
  68. {
  69. Log::info('PayPlus sync notify');
  70. return 'PayPlus sync notify';
  71. }
  72. public function cash_notify(Request $request)
  73. {
  74. $post = $request->all();
  75. $content = (string) $request->getContent();
  76. $post = json_decode($content, true) ?: $post;
  77. Util::WriteLog('PayPlus', 'cash notify: ' . $content);
  78. Util::WriteLog('PayPlus', 'cash notify header: ' . json_encode($request->headers->all()));
  79. $signature = $request->header('Authorization', '');
  80. $service = (new PayPlus())->getPayoutService();
  81. if (!$service->verifyPayoutSignature($post, $signature)) {
  82. Util::WriteLog('PayPlus', 'cash notify verify failed');
  83. return 'success';
  84. }
  85. try {
  86. (new PayPlusCashierLogic($service))->notify($post);
  87. } catch (\Exception $exception) {
  88. TelegramBot::getDefault()->sendProgramNotify('PayPlus cash notify exception', json_encode($post), $exception);
  89. Util::WriteLog('PayPlus_error', $exception->getMessage());
  90. }
  91. return 'success';
  92. }
  93. }