我已经按照checkout doc link设置了贝宝付款。交易成功地在前端完成,但是当我尝试使用php后端获取订单详细信息时,它会抛出
BraintreeHttp \ HttpException:RESOURCE_NOT_FOUND
源代码:
<?php
// App\Http\Controllers\PayPalController.php;
namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\GetOrder;
class PayPalController extends Controller
{
public function process(Request $request)
{
$data = file_get_contents("php://input");
$json = json_decode($data);
//echo $json->orderID;exit();
GetOrder::getOrder($json->orderID);
}
}
<?php
// App/GetOrder.php
namespace App;
//1. Import the PayPal SDK client that was created in `Set up the Server SDK`.
use App\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
/**
*Enable the following line to print complete response as JSON.
*/
//print json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
}
<?php
// App/PayPalClient.php
namespace App;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
class PayPalClient
{
/**
* Returns PayPal HTTP client instance with environment that has access
* credentials context. Use this instance to invoke PayPal APIs, provided the
* credentials have access.
*/
public static function client()
{
return new PayPalHttpClient(self::environment());
}
/**
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
* This sample uses SandboxEnvironment. In production, use ProductionEnvironment.
*/
public static function environment()
{
$clientId = getenv("CLIENT_ID") ?: "AfCdYSiPET8EB0dBkdbzM5IB6isoUXEJ7XY4aTHwWJ3ReF0nhS8eTabE6LMDCMatF_9AUJB3-6yJRoCe";
$clientSecret = getenv("CLIENT_SECRET") ?: "EI3GTTzOqV4LlTBxJoQZ58-yP-QeKWlFv7DIbyh2rAHj80ALzPTIFrF_AQaJ4-RNZDgv_c5DbhxYmJ4m";
return new SandboxEnvironment($clientId, $clientSecret);
}
}