PayPal.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Utils\Payments;
  3. use App\Services\PaymentService;
  4. use App\Utils\CurrencyExchange;
  5. use App\Utils\Library\Templates\Gateway;
  6. use Auth;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Log;
  10. use Response;
  11. class PayPal extends PaymentService implements Gateway
  12. {
  13. protected static \Srmklive\PayPal\Services\PayPal $provider;
  14. public function __construct()
  15. {
  16. self::$provider = \PayPal::setProvider();
  17. $config = [
  18. 'mode' => 'live',
  19. 'live' => [
  20. 'client_id' => sysConfig('paypal_client_id'),
  21. 'client_secret' => sysConfig('paypal_client_secret'),
  22. 'app_id' => sysConfig('paypal_app_id'),
  23. ],
  24. 'payment_action' => 'Sale',
  25. 'currency' => 'USD',
  26. 'notify_url' => route('payment.notify', ['method' => 'paypal']),
  27. 'locale' => app()->getLocale(),
  28. 'validate_ssl' => true,
  29. ];
  30. $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'];
  31. self::$provider->setApiCredentials($config);
  32. self::$provider->getAccessToken();
  33. }
  34. public function purchase($request): JsonResponse
  35. {
  36. $payment = $this->createPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  37. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  38. $response = self::$provider->createOrder([
  39. 'intent' => 'CAPTURE',
  40. 'application_context' => [
  41. 'return_url' => route('payment.notify', ['method' => 'paypal']),
  42. 'cancel_url' => route('invoice'),
  43. ],
  44. 'purchase_units' => [
  45. 0 => [
  46. 'amount' => [
  47. 'currency_code' => 'USD',
  48. 'value' => '100.00',
  49. ],
  50. ],
  51. ],
  52. ]);
  53. if (isset($response['id']) && $response['id'] != null) {
  54. Log::error('【Paypal】处理错误:'.var_export($response, true));
  55. return Response::json(['status' => 'fail', 'message' => '创建订单失败,请使用其他方式或通知管理员!']);
  56. }
  57. $payment->update(['url' => $response['paypal_link']]);
  58. foreach ($response['links'] as $links) {
  59. if ($links['rel'] === 'approve') {
  60. return Response::json(['status' => 'success', 'url' => $links['href'], 'message' => '创建订单成功!']);
  61. }
  62. }
  63. $payment->failed();
  64. Log::error('【PayPal】错误: ');
  65. exit;
  66. }
  67. protected function getCheckoutData($trade_no, $amount): array
  68. {
  69. $converted = CurrencyExchange::convert('USD', $amount);
  70. if ($converted === false) {
  71. $converted = $amount / 7;
  72. }
  73. $amount = 0.3 + $converted;
  74. return [
  75. 'intent' => 'CAPTURE',
  76. 'invoice_id' => $trade_no,
  77. 'items' => [
  78. [
  79. 'name' => sysConfig('subject_name') ?: sysConfig('website_name'),
  80. 'price' => $amount,
  81. 'desc' => 'Description for'.(sysConfig('subject_name') ?: sysConfig('website_name')),
  82. 'qty' => 1,
  83. ],
  84. ],
  85. 'invoice_description' => $trade_no,
  86. 'return_url' => route('payment.notify', ['method' => 'paypal']),
  87. 'cancel_url' => route('invoice'),
  88. 'total' => $amount,
  89. ];
  90. }
  91. public function notify(Request $request): void
  92. {
  93. $response = self::$provider->capturePaymentOrder($request['token']);
  94. if (isset($response['status']) && $response['status'] === 'COMPLETED') {
  95. if ($this->paymentReceived($request['invoice'])) {
  96. exit('success');
  97. }
  98. } else {
  99. Log::error('【Paypal】交易失败');
  100. }
  101. exit('fail');
  102. }
  103. }