PayPal.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Payments;
  3. use App\Components\CurrencyExchange;
  4. use App\Models\Payment;
  5. use App\Payments\Library\Gateway;
  6. use Auth;
  7. use Exception;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. use Log;
  11. use Response;
  12. use Srmklive\PayPal\Services\ExpressCheckout;
  13. class PayPal extends Gateway
  14. {
  15. protected $provider;
  16. public function __construct()
  17. {
  18. $this->provider = new ExpressCheckout();
  19. $config = [
  20. 'mode' => 'live',
  21. 'live' => [
  22. 'username' => sysConfig('paypal_username'),
  23. 'password' => sysConfig('paypal_password'),
  24. 'secret' => sysConfig('paypal_secret'),
  25. 'certificate' => sysConfig('paypal_certificate'),
  26. 'app_id' => sysConfig('paypal_app_id'),
  27. ],
  28. 'payment_action' => 'Sale',
  29. 'currency' => 'USD',
  30. 'billing_type' => 'MerchantInitiatedBilling',
  31. 'notify_url' => route('payment.notify', ['method' => 'paypal']),
  32. 'locale' => 'zh_CN',
  33. 'validate_ssl' => true,
  34. ];
  35. $this->provider->setApiCredentials($config);
  36. }
  37. public function purchase($request): JsonResponse
  38. {
  39. $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
  40. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  41. try {
  42. $response = $this->provider->setExpressCheckout($data);
  43. if (! $response['paypal_link']) {
  44. Log::error('【Paypal】处理错误:'.var_export($response, true));
  45. return Response::json(['status' => 'fail', 'message' => '创建订单失败,请使用其他方式或通知管理员!']);
  46. }
  47. $payment->update(['url' => $response['paypal_link']]);
  48. return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
  49. } catch (Exception $e) {
  50. $payment->failed();
  51. Log::error('【PayPal】错误: '.$e->getMessage());
  52. exit;
  53. }
  54. }
  55. public function getCheckout(Request $request)
  56. {
  57. $token = $request->get('token');
  58. $PayerID = $request->get('PayerID');
  59. // Verify Express Checkout Token
  60. $response = $this->provider->getExpressCheckoutDetails($token);
  61. if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
  62. $payment = Payment::whereTradeNo($response['INVNUM'])->firstOrFail();
  63. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  64. // Perform transaction on PayPal
  65. $payment_status = $this->provider->doExpressCheckoutPayment($data, $token, $PayerID);
  66. $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
  67. if (! strcasecmp($status, 'Completed') || ! strcasecmp($status, 'Processed')) {
  68. Log::notice("【Paypal】Order $payment->order_id has been paid successfully!");
  69. $payment->order->paid();
  70. } else {
  71. Log::alert("【PayPal】Error processing PayPal payment for Order $payment->id!");
  72. }
  73. }
  74. return redirect(route('invoice'));
  75. }
  76. public function notify($request): void
  77. {
  78. $request->merge(['cmd' => '_notify-validate']);
  79. foreach ($request->input() as $key => $value) {
  80. if ($value === null) {
  81. $request->request->set($key, '');
  82. }
  83. }
  84. $post = $request->all();
  85. $response = (string) $this->provider->verifyIPN($post);
  86. if ($response === 'VERIFIED' && $request['invoice']) {
  87. if ($this->paymentReceived($request['invoice'])) {
  88. exit('success');
  89. }
  90. } else {
  91. Log::error('【Paypal】交易失败');
  92. }
  93. exit('fail');
  94. }
  95. protected function getCheckoutData($trade_no, $amount): array
  96. {
  97. $converted = CurrencyExchange::convert('USD', $amount);
  98. if ($converted === false) {
  99. $converted = $amount / 7;
  100. }
  101. $amount = 0.3 + $converted;
  102. return [
  103. 'invoice_id' => $trade_no,
  104. 'items' => [
  105. [
  106. 'name' => sysConfig('subject_name') ?: sysConfig('website_name'),
  107. 'price' => $amount,
  108. 'desc' => 'Description for'.(sysConfig('subject_name') ?: sysConfig('website_name')),
  109. 'qty' => 1,
  110. ],
  111. ],
  112. 'invoice_description' => $trade_no,
  113. 'return_url' => route('paypal.checkout'),
  114. 'cancel_url' => route('invoice'),
  115. 'total' => $amount,
  116. ];
  117. }
  118. }