PayPal.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Http\Models\Payment;
  4. use Auth;
  5. use Exception;
  6. use Illuminate\Http\Request;
  7. use Log;
  8. use Response;
  9. use Srmklive\PayPal\Services\ExpressCheckout;
  10. class PayPal extends AbstractPayment
  11. {
  12. protected $provider;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->provider = new ExpressCheckout();
  17. $config = [
  18. 'mode' => 'live',
  19. 'live' => [
  20. 'username' => self::$systemConfig['paypal_username'],
  21. 'password' => self::$systemConfig['paypal_password'],
  22. 'secret' => self::$systemConfig['paypal_secret'],
  23. 'certificate' => self::$systemConfig['paypal_certificate'],
  24. 'app_id' => self::$systemConfig['paypal_app_id'],
  25. ],
  26. 'payment_action' => 'Sale',
  27. 'currency' => env('PAYPAL_CURRENCY', 'USD'),
  28. 'billing_type' => 'MerchantInitiatedBilling',
  29. 'notify_url' => (self::$systemConfig['website_callback_url']? : self::$systemConfig['website_url']).'/callback/notify?method=paypal',
  30. 'locale' => 'zh-CN',
  31. 'validate_ssl' => TRUE,
  32. ];
  33. $this->provider->setApiCredentials($config);
  34. }
  35. public function purchase(Request $request)
  36. {
  37. $payment = new Payment();
  38. $payment->sn = self::generateGuid();
  39. $payment->user_id = Auth::user()->id;
  40. $payment->oid = $request->input('oid');
  41. $payment->amount = $request->input('amount');
  42. $payment->save();
  43. $data = $this->getCheckoutData($payment->sn, $payment->amount);
  44. try{
  45. $response = $this->provider->setExpressCheckout($data);
  46. return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
  47. }catch(Exception $e){
  48. Log::error("【PayPal】错误: ".$e->getMessage());
  49. exit;
  50. }
  51. }
  52. protected function getCheckoutData($sn, $amount)
  53. {
  54. return [
  55. 'invoice_id' => $sn,
  56. 'items' => [
  57. [
  58. 'name' => self::$systemConfig['subject_name']? : self::$systemConfig['website_name'],
  59. 'price' => $amount,
  60. 'desc' => 'Description for'.(self::$systemConfig['subject_name']? : self::$systemConfig['website_name']),
  61. 'qty' => 1
  62. ]
  63. ],
  64. 'invoice_description' => $sn,
  65. 'return_url' => self::$systemConfig['website_url'].'/callback/checkout',
  66. 'cancel_url' => self::$systemConfig['website_url'].'/invoices',
  67. 'total' => $amount,
  68. ];
  69. }
  70. public function getCheckout(Request $request)
  71. {
  72. $token = $request->get('token');
  73. $PayerID = $request->get('PayerID');
  74. // Verify Express Checkout Token
  75. $response = $this->provider->getExpressCheckoutDetails($token);
  76. if(in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])){
  77. $payment = Payment::whereSn($response['INVNUM'])->first();
  78. $data = $this->getCheckoutData($payment->sn, $payment->amount);
  79. // Perform transaction on PayPal
  80. $payment_status = $this->provider->doExpressCheckoutPayment($data, $token, $PayerID);
  81. $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
  82. if(!strcasecmp($status, 'Completed') || !strcasecmp($status, 'Processed')){
  83. Log::info("Order $payment->id has been paid successfully!");
  84. }else{
  85. Log::error("Error processing PayPal payment for Order $payment->id!");
  86. }
  87. }
  88. return redirect('/invoices');
  89. }
  90. public function notify(Request $request)
  91. {
  92. $request->merge(['cmd' => '_notify-validate']);
  93. $post = $request->all();
  94. $response = (string)$this->provider->verifyIPN($post);
  95. if($response === 'VERIFIED' && $request['mp_desc']){
  96. if(Payment::whereSn($request['mp_desc'])->first()->status == 0){
  97. self::postPayment($request['mp_desc'], 'PayPal');
  98. }
  99. exit("success");
  100. }
  101. exit("fail");
  102. }
  103. public function getReturnHTML(Request $request)
  104. {
  105. // TODO: Implement getReturnHTML() method.
  106. }
  107. public function getPurchaseHTML()
  108. {
  109. // TODO: Implement getPurchaseHTML() method.
  110. }
  111. }