PayPal.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Utils\Payments;
  3. use App\Utils\CurrencyExchange;
  4. use App\Utils\Library\PaymentHelper;
  5. use App\Utils\Library\Templates\Gateway;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. use Log;
  9. class PayPal implements Gateway
  10. {
  11. public static array $methodDetails = [
  12. 'key' => 'paypal',
  13. 'settings' => ['paypal_client_id', 'paypal_client_secret', 'paypal_app_id'],
  14. ];
  15. protected static \Srmklive\PayPal\Services\PayPal $provider;
  16. public function __construct()
  17. {
  18. self::$provider = \PayPal::setProvider();
  19. $config = [
  20. 'mode' => 'live',
  21. 'live' => [
  22. 'client_id' => sysConfig('paypal_client_id'),
  23. 'client_secret' => sysConfig('paypal_client_secret'),
  24. 'app_id' => sysConfig('paypal_app_id'),
  25. ],
  26. 'payment_action' => 'Sale',
  27. 'currency' => 'USD',
  28. 'notify_url' => route('payment.notify', ['method' => 'paypal']),
  29. 'locale' => app()->getLocale(),
  30. 'validate_ssl' => true,
  31. ];
  32. $allowedCurrencies = ['AUD', 'BRL', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'INR', 'JPY', 'MYR', 'MXN', 'NOK', 'NZD', 'PHP', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'TWD', 'THB', 'USD', 'RUB', 'CNY'];
  33. self::$provider->setApiCredentials($config);
  34. self::$provider->getAccessToken();
  35. }
  36. public function purchase(Request $request): JsonResponse
  37. {
  38. $payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
  39. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  40. $response = self::$provider->createOrder($data);
  41. if (isset($response['id']) && $response['id'] != null) {
  42. Log::error('【Paypal】处理错误:'.var_export($response, true));
  43. return response()->json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
  44. }
  45. $payment->update(['url' => $response['paypal_link']]);
  46. foreach ($response['links'] as $links) {
  47. if ($links['rel'] === 'approve') {
  48. return response()->json(['status' => 'success', 'url' => $links['href'], 'message' => trans('user.payment.order_creation.success')]);
  49. }
  50. }
  51. $payment->failed();
  52. Log::error('【PayPal】错误: ');
  53. exit;
  54. }
  55. protected function getCheckoutData(string $trade_no, float|int $amount): array
  56. {
  57. $converted = CurrencyExchange::convert('USD', $amount);
  58. if ($converted === false) {
  59. $converted = $amount / 7;
  60. }
  61. $amount = 0.3 + $converted;
  62. return [
  63. 'intent' => 'CAPTURE',
  64. 'invoice_id' => $trade_no,
  65. 'items' => [
  66. [
  67. 'name' => sysConfig('subject_name') ?: sysConfig('website_name'),
  68. 'price' => $amount,
  69. 'desc' => 'Description for'.(sysConfig('subject_name') ?: sysConfig('website_name')),
  70. 'qty' => 1,
  71. ],
  72. ],
  73. 'invoice_description' => $trade_no,
  74. 'return_url' => route('payment.notify', ['method' => 'paypal']),
  75. 'cancel_url' => route('invoice.index'),
  76. 'total' => $amount,
  77. ];
  78. }
  79. public function notify(Request $request): void
  80. {
  81. $response = self::$provider->capturePaymentOrder($request['token']);
  82. if (isset($response['status']) && $response['status'] === 'COMPLETED') {
  83. if (PaymentHelper::paymentReceived($request['invoice'])) {
  84. exit('success');
  85. }
  86. } else {
  87. Log::error('【Paypal】交易失败');
  88. }
  89. exit('fail');
  90. }
  91. }