PayPal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if(!$response['paypal_link']){
  57. Log::error(var_export($response, TRUE));
  58. return Response::json(['status' => 'fail', 'message' => '创建订单失败,请使用其他方式或通知管理员!']);
  59. }
  60. Payment::whereId($payment->id)->update(['url' => $response['paypal_link']]);
  61. return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
  62. }catch(Exception $e){
  63. Log::error("【PayPal】错误: ".$e->getMessage());
  64. exit;
  65. }
  66. }
  67. protected function getCheckoutData($sn, $amount)
  68. {
  69. $amount = 0.3+ceil($amount/$this->exChange*100)/100;
  70. return [
  71. 'invoice_id' => $sn,
  72. 'items' => [
  73. [
  74. 'name' => self::$systemConfig['subject_name']? : self::$systemConfig['website_name'],
  75. 'price' => $amount,
  76. 'desc' => 'Description for'.(self::$systemConfig['subject_name']? : self::$systemConfig['website_name']),
  77. 'qty' => 1
  78. ]
  79. ],
  80. 'invoice_description' => $sn,
  81. 'return_url' => self::$systemConfig['website_url'].'/callback/checkout',
  82. 'cancel_url' => self::$systemConfig['website_url'].'/invoices',
  83. 'total' => $amount,
  84. ];
  85. }
  86. public function getCheckout(Request $request)
  87. {
  88. $token = $request->get('token');
  89. $PayerID = $request->get('PayerID');
  90. // Verify Express Checkout Token
  91. $response = $this->provider->getExpressCheckoutDetails($token);
  92. if(in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])){
  93. $payment = Payment::whereSn($response['INVNUM'])->first();
  94. $data = $this->getCheckoutData($payment->sn, $payment->amount);
  95. // Perform transaction on PayPal
  96. $payment_status = $this->provider->doExpressCheckoutPayment($data, $token, $PayerID);
  97. $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
  98. if(!strcasecmp($status, 'Completed') || !strcasecmp($status, 'Processed')){
  99. Log::info("Order $payment->oid has been paid successfully!");
  100. Order::whereOid($payment->oid)->update(['status' => 1]);
  101. }else{
  102. Log::error("Error processing PayPal payment for Order $payment->id!");
  103. }
  104. }
  105. return redirect('/invoices');
  106. }
  107. public function notify(Request $request)
  108. {
  109. $request->merge(['cmd' => '_notify-validate']);
  110. $post = $request->all();
  111. $response = (string)$this->provider->verifyIPN($post);
  112. if($response === 'VERIFIED' && $request['mp_desc']){
  113. if(Payment::whereSn($request['mp_desc'])->first()->status == 0){
  114. self::postPayment($request['mp_desc'], 'PayPal');
  115. }
  116. exit("success");
  117. }
  118. exit("fail");
  119. }
  120. public function getReturnHTML(Request $request)
  121. {
  122. // TODO: Implement getReturnHTML() method.
  123. }
  124. public function getPurchaseHTML()
  125. {
  126. // TODO: Implement getPurchaseHTML() method.
  127. }
  128. }