PayPal.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. protected static \Srmklive\PayPal\Services\PayPal $provider;
  12. public function __construct()
  13. {
  14. self::$provider = \PayPal::setProvider();
  15. $config = [
  16. 'mode' => 'live',
  17. 'live' => [
  18. 'client_id' => sysConfig('paypal_client_id'),
  19. 'client_secret' => sysConfig('paypal_client_secret'),
  20. 'app_id' => sysConfig('paypal_app_id'),
  21. ],
  22. 'payment_action' => 'Sale',
  23. 'currency' => 'USD',
  24. 'notify_url' => route('payment.notify', ['method' => 'paypal']),
  25. 'locale' => app()->getLocale(),
  26. 'validate_ssl' => true,
  27. ];
  28. $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'];
  29. self::$provider->setApiCredentials($config);
  30. self::$provider->getAccessToken();
  31. }
  32. public static function metadata(): array
  33. {
  34. return [
  35. 'key' => 'paypal',
  36. 'method' => ['other'],
  37. 'settings' => [
  38. 'paypal_client_id' => null,
  39. 'paypal_client_secret' => null,
  40. 'paypal_app_id' => null,
  41. ],
  42. ];
  43. }
  44. public function purchase(Request $request): JsonResponse
  45. {
  46. $payment = PaymentHelper::createPayment(auth()->id(), $request->input('id'), $request->input('amount'));
  47. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  48. $response = self::$provider->createOrder($data);
  49. if (isset($response['id']) && $response['id'] != null) {
  50. Log::error('【Paypal】处理错误:'.var_export($response, true));
  51. return response()->json(['status' => 'fail', 'message' => trans('user.payment.order_creation.failed')]);
  52. }
  53. $payment->update(['url' => $response['paypal_link']]);
  54. foreach ($response['links'] as $links) {
  55. if ($links['rel'] === 'approve') {
  56. return response()->json(['status' => 'success', 'url' => $links['href'], 'message' => trans('user.payment.order_creation.success')]);
  57. }
  58. }
  59. $payment->failed();
  60. Log::error('【PayPal】错误: ');
  61. exit;
  62. }
  63. protected function getCheckoutData(string $trade_no, float|int $amount): array
  64. {
  65. $converted = CurrencyExchange::convert('USD', $amount);
  66. if ($converted === false) {
  67. $converted = $amount / 7;
  68. }
  69. $amount = 0.3 + $converted;
  70. return [
  71. 'intent' => 'CAPTURE',
  72. 'invoice_id' => $trade_no,
  73. 'items' => [
  74. [
  75. 'name' => sysConfig('subject_name') ?: sysConfig('website_name'),
  76. 'price' => $amount,
  77. 'desc' => 'Description for'.(sysConfig('subject_name') ?: sysConfig('website_name')),
  78. 'qty' => 1,
  79. ],
  80. ],
  81. 'invoice_description' => $trade_no,
  82. 'return_url' => route('payment.notify', ['method' => 'paypal']),
  83. 'cancel_url' => route('invoice.index'),
  84. 'total' => $amount,
  85. ];
  86. }
  87. public function notify(Request $request): void
  88. {
  89. $response = self::$provider->capturePaymentOrder($request['token']);
  90. if (isset($response['status']) && $response['status'] === 'COMPLETED') {
  91. if (PaymentHelper::paymentReceived($request['invoice'])) {
  92. exit('success');
  93. }
  94. } else {
  95. Log::error('【Paypal】交易失败');
  96. }
  97. exit('fail');
  98. }
  99. }