PayPal.php 4.3 KB

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