PayPlusController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $orderSn = $post['data']['platform_order_id'] ?? '';
  44. if ($orderSn === '') {
  45. return 'success';
  46. }
  47. $lockKey = 'pay_notify_PayPlus_' . $orderSn;
  48. if (!SetNXLock::getExclusiveLock($lockKey, 60)) {
  49. return 'success';
  50. }
  51. try {
  52. return (new PayPlusLogic())->notify($post);
  53. } catch (\Exception $exception) {
  54. Redis::set('PayErro_PayPlus', 1);
  55. Redis::expire('PayErro_PayPlus', 600);
  56. TelegramBot::getDefault()->sendProgramNotify('PayPlus notify exception', json_encode($post), $exception);
  57. Util::WriteLog('PayPlus_error', $exception->getMessage());
  58. return 'success';
  59. } finally {
  60. SetNXLock::release($lockKey);
  61. }
  62. }
  63. public function sync_notify(Request $request)
  64. {
  65. Log::info('PayPlus sync notify');
  66. return 'PayPlus sync notify';
  67. }
  68. public function cash_notify(Request $request)
  69. {
  70. $post = $request->all();
  71. $content = (string) $request->getContent();
  72. $post = json_decode($content, true) ?: $post;
  73. Util::WriteLog('PayPlus', 'cash notify: ' . $content);
  74. Util::WriteLog('PayPlus', 'cash notify header: ' . json_encode($request->headers->all()));
  75. $signature = $request->header('Authorization', '');
  76. $service = (new PayPlus())->getPayoutService();
  77. if (!$service->verifyPayoutSignature($post, $signature)) {
  78. Util::WriteLog('PayPlus', 'cash notify verify failed');
  79. return 'success';
  80. }
  81. try {
  82. (new PayPlusCashierLogic($service))->notify($post);
  83. } catch (\Exception $exception) {
  84. TelegramBot::getDefault()->sendProgramNotify('PayPlus cash notify exception', json_encode($post), $exception);
  85. Util::WriteLog('PayPlus_error', $exception->getMessage());
  86. }
  87. return 'success';
  88. }
  89. }