PayPal.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected $provider;
  14. protected $exChange;
  15. public function __construct() {
  16. parent::__construct();
  17. $this->provider = new ExpressCheckout();
  18. $config = [
  19. 'mode' => 'live',
  20. 'live' => [
  21. 'username' => self::$systemConfig['paypal_username'],
  22. 'password' => self::$systemConfig['paypal_password'],
  23. 'secret' => self::$systemConfig['paypal_secret'],
  24. 'certificate' => self::$systemConfig['paypal_certificate'],
  25. 'app_id' => self::$systemConfig['paypal_app_id'],
  26. ],
  27. 'payment_action' => 'Sale',
  28. 'currency' => 'USD',
  29. 'billing_type' => 'MerchantInitiatedBilling',
  30. 'notify_url' => (self::$systemConfig['website_callback_url']?: self::$systemConfig['website_url']).'/callback/notify?method=paypal',
  31. 'locale' => 'zh_CN',
  32. 'validate_ssl' => true,
  33. ];
  34. $this->provider->setApiCredentials($config);
  35. $this->exChange = 7;
  36. $exChangeRate = json_decode(Curl::send('http://api.k780.com/?app=finance.rate&scur=USD&tcur=CNY&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4'),
  37. true);
  38. if($exChangeRate){
  39. if($exChangeRate['success']){
  40. $this->exChange = $exChangeRate['result']['rate'];
  41. }
  42. }
  43. }
  44. public function purchase(Request $request) {
  45. $payment = new Payment();
  46. $payment->sn = self::generateGuid();
  47. $payment->user_id = Auth::user()->id;
  48. $payment->oid = $request->input('oid');
  49. $payment->amount = $request->input('amount');
  50. $payment->save();
  51. $data = $this->getCheckoutData($payment->sn, $payment->amount);
  52. try{
  53. $response = $this->provider->setExpressCheckout($data);
  54. if(!$response['paypal_link']){
  55. Log::error(var_export($response, true));
  56. return Response::json(['status' => 'fail', 'message' => '创建订单失败,请使用其他方式或通知管理员!']);
  57. }
  58. Payment::whereId($payment->id)->update(['url' => $response['paypal_link']]);
  59. return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
  60. }catch(Exception $e){
  61. Log::error("【PayPal】错误: ".$e->getMessage());
  62. exit;
  63. }
  64. }
  65. protected function getCheckoutData($sn, $amount) {
  66. $amount = 0.3 + ceil($amount / $this->exChange * 100) / 100;
  67. return [
  68. 'invoice_id' => $sn,
  69. 'items' => [
  70. [
  71. 'name' => self::$systemConfig['subject_name']?: self::$systemConfig['website_name'],
  72. 'price' => $amount,
  73. 'desc' => 'Description for'.(self::$systemConfig['subject_name']?: self::$systemConfig['website_name']),
  74. 'qty' => 1
  75. ]
  76. ],
  77. 'invoice_description' => $sn,
  78. 'return_url' => self::$systemConfig['website_url'].'/callback/checkout',
  79. 'cancel_url' => self::$systemConfig['website_url'].'/invoices',
  80. 'total' => $amount,
  81. ];
  82. }
  83. public function getCheckout(Request $request) {
  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. $request->merge(['cmd' => '_notify-validate']);
  105. foreach($request->input() as $key => $value){
  106. if($value == null){
  107. $request->request->set($key, '');
  108. }
  109. }
  110. $post = $request->all();
  111. $response = (string) $this->provider->verifyIPN($post);
  112. if($response === 'VERIFIED' && $request['invoice']){
  113. if(Payment::whereSn($request['invoice'])->first()->status == 0){
  114. self::postPayment($request['invoice'], 'PayPal');
  115. }
  116. exit("success");
  117. }
  118. exit("fail");
  119. }
  120. public function getReturnHTML(Request $request) {
  121. // TODO: Implement getReturnHTML() method.
  122. }
  123. public function getPurchaseHTML() {
  124. // TODO: Implement getPurchaseHTML() method.
  125. }
  126. }