以下是通过PayPal Rest API实现授权并捕获PayPal订单的示例代码:
$ch = curl_init();
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $access_token,
);
// Set the url
curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v2/checkout/orders/' . $order_id . '/authorize');
// Set the headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set the request method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// Set the request data
$post_data = array(
'amount' => array(
'currency_code' => 'USD',
'value' => '10.00',
),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
// Return response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
// Close the curl session
curl_close($ch);
// Handle response
if (isset($response)) {
$result = json_decode($response);
$authorization_id = $result->id;
}
$ch = curl_init();
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $access_token,
);
// Set the url
curl_setopt($ch, CURLOPT_URL, 'https://api.paypal.com/v2/checkout/orders/' . $order_id . '/capture');
// Set the headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set the request method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// Set the request data
$post_data = array(
'amount' => array(
'currency_code' => 'USD',
'value' => '10.00',
),
'final_capture' => true,
'note_to_payer' => 'Thank you for your order!',
'invoice_id' => '12345',
'custom_id' => '67890',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json