PayPal.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers\Gateway;
  3. use App\Components\Curl;
  4. use App\Models\Order;
  5. use App\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) {
  45. $payment = $this->creatNewPayment(Auth::id(), $request->input('oid'), $request->input('amount'));
  46. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  47. try{
  48. $response = $this->provider->setExpressCheckout($data);
  49. if(!$response['paypal_link']){
  50. Log::error(var_export($response, true));
  51. return Response::json(['status' => 'fail', 'message' => '创建订单失败,请使用其他方式或通知管理员!']);
  52. }
  53. Payment::whereId($payment->id)->update(['url' => $response['paypal_link']]);
  54. return Response::json(['status' => 'success', 'url' => $response['paypal_link'], 'message' => '创建订单成功!']);
  55. }catch(Exception $e){
  56. Log::error("【PayPal】错误: ".$e->getMessage());
  57. exit;
  58. }
  59. }
  60. protected function getCheckoutData($trade_no, $amount) {
  61. $amount = 0.3 + ceil($amount / $this->exChange * 100) / 100;
  62. return [
  63. 'invoice_id' => $trade_no,
  64. 'items' => [
  65. [
  66. 'name' => self::$systemConfig['subject_name']?: self::$systemConfig['website_name'],
  67. 'price' => $amount,
  68. 'desc' => 'Description for'.(self::$systemConfig['subject_name']?: self::$systemConfig['website_name']),
  69. 'qty' => 1
  70. ]
  71. ],
  72. 'invoice_description' => $trade_no,
  73. 'return_url' => self::$systemConfig['website_url'].'/callback/checkout',
  74. 'cancel_url' => self::$systemConfig['website_url'].'/invoices',
  75. 'total' => $amount,
  76. ];
  77. }
  78. public function getCheckout(Request $request) {
  79. $token = $request->get('token');
  80. $PayerID = $request->get('PayerID');
  81. // Verify Express Checkout Token
  82. $response = $this->provider->getExpressCheckoutDetails($token);
  83. if(in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])){
  84. $payment = Payment::whereTradeNo($response['INVNUM'])->first();
  85. $data = $this->getCheckoutData($payment->trade_no, $payment->amount);
  86. // Perform transaction on PayPal
  87. $payment_status = $this->provider->doExpressCheckoutPayment($data, $token, $PayerID);
  88. $status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
  89. if(!strcasecmp($status, 'Completed') || !strcasecmp($status, 'Processed')){
  90. Log::info("Order $payment->oid has been paid successfully!");
  91. Order::whereOid($payment->oid)->update(['status' => 1]);
  92. }else{
  93. Log::error("Error processing PayPal payment for Order $payment->id!");
  94. }
  95. }
  96. return redirect('/invoices');
  97. }
  98. public function notify($request) {
  99. $request->merge(['cmd' => '_notify-validate']);
  100. foreach($request->input() as $key => $value){
  101. if($value == null){
  102. $request->request->set($key, '');
  103. }
  104. }
  105. $post = $request->all();
  106. $response = (string) $this->provider->verifyIPN($post);
  107. if($response === 'VERIFIED' && $request['invoice']){
  108. if(Payment::whereTradeNo($request['invoice'])->first()->status == 0){
  109. self::postPayment($request['invoice'], 'PayPal');
  110. }
  111. exit("success");
  112. }
  113. exit("fail");
  114. }
  115. }