<?php namespace App\Http\Controllers\Api\Paypal; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Jobs\CheckOrderPaypal; use App\Http\Controllers\Api\Order\Order; class Paypal extends Controller { protected $useSandle = true; protected $sandleClintId = '沙盒模式下的clintId'; protected $sandleSecret = '沙盒模式下的secret'; protected $clientId = '上线的'; protected $secret = '上线的'; protected function getUrl() { return $this->useSandle?'https://www.sandbox.paypal.com':'https://www.paypal.com'; } protected function getApiUrl() { return $this->useSandle?'https://api.sandbox.paypal.com':'https://api.paypal.com'; } public function getClientId() { return $this->useSandle?$this->sandleClintId:$this->clientId; } protected function getSecret() { return $this->useSandle?$this->sandleSecret:$this->secret; } protected function getWebscrUrl() { return $this->getUrl().'/cgi-bin/webscr'; } protected function getTokenUrl() { return $this->getApiUrl().'/v1/oauth2/token'; } protected function getOrderUrl() { return $this->getApiUrl().'/v2/checkout/orders'; // $URL = "https://api.paypal.com/v1/checkout/orders/$orderid"; // $URL = "https://api.paypal.com/v1/payments/orders/$orderid"; // $URL = "https://api.paypal.com/v2/payments/payment/$orderid"; // $URL = "https://api.sandbox.paypal.com/v2/checkout/orders/$orderid"; // $URL = "https://api.sandbox.paypal.com/v2/payments/orders/$orderid"; // $URL = "https://api.sandbox.paypal.com/v2/payments/payment/$orderid"; } public function index() { $raw_post_data = file_get_contents('php://input'); info("PayPal支付开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",[$raw_post_data]); $raw_post_array = explode('&', $raw_post_data); $myPost = array(); foreach ($raw_post_array as $keyval) { $keyval = explode ('=', $keyval); if (count($keyval) == 2) $myPost[$keyval[0]] = urldecode($keyval[1]); } $req = 'cmd=_notify-validate'; if (function_exists('get_magic_quotes_gpc')) { $get_magic_quotes_exists = true; } foreach ($myPost as $key => $value) { if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { $value = urlencode(stripslashes($value)); } else { $value = urlencode($value); } $req .= "&$key=$value"; } $res = app("common")->http_request($this->getWebscrUrl(),$req, array('Connection: Close')); info("支付验证",[$res]); if (strcmp ($res, "VERIFIED") == 0) { $verify = $this->verifyNotify($raw_post_data); $result = $verify?'success':'fail'; echo 'success'; exit; } else if (strcmp ($res, "INVALID") == 0) { echo 'fail'; exit; } } private function verifyNotify($post) { if(empty($post)){ return false; } $post2 = explode('&',$post); $post_arr = []; foreach ($post2 as $key => $value) { $tmp_value = explode("=",$value); $tmp = []; $post_arr[$tmp_value[0]] = $tmp_value[1]; } //completed 表示订单状态ok. if($post_arr['payment_status'] !='Completed'){ return false; } $order_sn = $post_arr['invoice']; // 收取订单号 $payment_gross = $post_arr['payment_gross']; // 支付金额 // 以order_sn在paypal订单表中查询该订单数据 $order_paypal = app("order-paypal")->where("order_sn",$order_sn)->first(); if (empty($order_paypal->orderID)) { $order = app("order")->where("order_sn",$order_sn)->first(); // 抛入队列 直到orderID绑定好 dispatch(new CheckOrderPaypal($order->order_id))->delay(now()->addMinutes(1)); exit; } // 查询订单信息获取,token $tokens= $this->access_token(); $token = $tokens['access_token']; // 查询订单 $order = $this->get_curlOrder($order_paypal->orderID, $token); info('当前订单支付信息',[$order]); if ($order['status'] != 'COMPLETED') { return false; } info("PayPal支付结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",['orderID' => $order_paypal->orderID]); // 更新订单状态 $rt = app("common")->update(['order_status' => 20],app("order")->find($order_paypal->order_id)); // 更新用户会员信息 $rs = Order::finishOrder($order_paypal->order_id,$payment_gross); if (! $rt || ! $rs) { info("更新订单状态 及 更新用户信息异常",['order_id' => $order_paypal->order_id , 'user_id' => $order_paypal->user_id]); info("交易单",[$order_paypal->orderID]); return false; } info("订单结算完成>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",['order_id' => $order_paypal->order_id]); return $rt; } // 获取token public function access_token() { $url = $this->getTokenUrl(); $clientId = $this->getClientId(); $secret = $this->getSecret(); $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_HEADER, false ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt ( $ch, CURLOPT_POST, true ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ( $ch, CURLOPT_USERPWD, $clientId . ":" . $secret ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials" ); $result = curl_exec ( $ch ); curl_close ( $ch ); if (empty ( $result )) { return false; } else { return json_decode ( $result, true ); } } public function get_curlOrder($orderid, $token) { if (empty ( $orderid ) || empty ( $token )) { return false; } $URL = $this->getOrderUrl()."/$orderid"; $curl = curl_init (); curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, '0' ); curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, '0' ); curl_setopt_array ( $curl, array ( CURLOPT_URL => $URL, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array ( "Authorization: Bearer {$token}", "Content-Type:application/json" ) ) ); $response = curl_exec ( $curl ); curl_close ( $curl ); return json_decode ( $response, true ); } }
讯享网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90795.html