PHP新手30天實戰金流
, Laravel6
官網提及 Please note that if you are integrating with PayPal Checkout, this SDK and corresponding API v1/payments are in the process of being deprecated.
因此我們決定要做版本更新,
官網有一份文件 Migrating from v1 to v2 供參考,以下程式碼皆出自於此。
"2.0 includes all of the existing APIs (except payouts), and includes the new Orders API (Disputes and Marketplace coming soon)."
且
"We've simplified the interface to only provide HTTPRequest that can easily be called via our HttpClient."
修改 composer.json
中 paypal/rest-api-sdk-php
的版本為 dev-2.0-beta
然後 composer update
先簡介付款流程:
更新到 version 2,要修改的地方為付款的第一步驟:
原本用 apicontext 來記錄商家(client)資訊,現在改用 SandboxEnvironment 和 PayPalHttpClient 類別來處理。
原本:
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'client-id',
'client-secret'
)
);
$apiContext->setConfig(
array('mode' => 'live')
);
現在:
use PayPal\Core\PayPalHttpClient;
use PayPal\Core\SandboxEnvironment;
$environment = new SandboxEnvironment('client-id', 'client-secret'); // Use `ProductionEnvironment` for production
$client = new PayPalHttpClient($environment);
原本的body用物件串起, 現在直接寫在 body上:
原本:
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount();
$amount->setTotal('1.00');
$amount->setCurrency('USD');
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl("https://example.com/your_redirect_url.html")
->setCancelUrl("https://example.com/your_cancel_url.html");
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions(array($transaction))
->setRedirectUrls($redirectUrls);
try {
$payment->create($apiContext);
echo $payment;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getData();
}
現在:
$body = [
"intent" => "sale",
"transactions" => [
[
"amount" => [
"total" => "10",
"currency" => "USD"
]
]
],
"redirect_urls" => [
"cancel_url" => "http://paypal.com/cancel",
"return_url" => "http://paypal.com/return"
],
"payer" => [
"payment_method" => "paypal"
]
];
$request = new PaymentCreateRequest();
$request->body = $body;
try {
return $client->execute($request);
} catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
}
會得到
{
"statusCode": 201,
"result": {
"id": "PAYID-LWQZ4SQ78W31241DC6035226",
"intent": "sale",
"state": "created",
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "470.00",
"currency": "THB"
},
"related_resources": []
}
],
"create_time": "2019-10-12T09:35:05Z",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LWQZ4SQ78W31241DC6035226",
"rel": "self",
"method": "GET"
},
{
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8FE75461FU181391W",
"rel": "approval_url",
"method": "REDIRECT"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LWQZ4SQ78W31241DC6035226/execute",
"rel": "execute",
"method": "POST"
}
]
},
"headers": {
"": "",
"Date": "Sat, 12 Oct 2019 09",
"Server": "Apache",
"paypal-debug-id": "4e44cce1a690b",
"Content-Language": "*",
"HTTP_X_PP_AZ_LOCATOR": "sandbox.slc",
"Paypal-Debug-Id": "4e44cce1a690b",
"Set-Cookie": "X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00",
"Vary": "Authorization",
"Content-Length": "644",
"Connection": "close",
"Content-Type": "application/json"
}
}