PayPlusController.php 3.5 KB

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