Files
2024_WEBSITE_fipf/plugins/fipfcard_plugin/php/paypal/paypal.php
asus b7685cbbc1 - struggled to make ajax works, but now is ok
- starting to make serverside works
2024-02-23 19:36:03 +01:00

169 lines
3.4 KiB
PHP

<?php
/**
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
*/
/**
*
*/
function fipfcard_paypal_order()
{
// json decode from JSON.stringify : https://stackoverflow.com/q/15986235/9497573
$data_received = json_decode( html_entity_decode( stripslashes( $_POST['data'] ) ) );
// error_log( "data_received" );
// error_log( json_encode( $data_received ) );
// use the cart information passed from the front-end to calculate the order amount detals
$cart = $data_received->cart;
error_log( "cart" );
error_log( json_encode( $cart ) );
create_order($cart);
wp_send_json_success(
array(
'from paypal_order',
"data_received" => $data_received,
),
200
);
}
add_action( 'wp_ajax_paypal_orders', 'fipfcard_paypal_order' );
/**
* Generate an OAuth 2.0 access token for authenticating with PayPal REST APIs.
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
* @see https://developer.paypal.com/api/rest/authentication/
*/
function generate_access_token( )
{
$base = "https://api-m.sandbox.paypal.com";
try {
if ( !PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET ) {
throw new Error( "MISSING_API_CREDENTIALS" );
}
$credentials = PAYPAL_CLIENT_ID . ":" . PAYPAL_CLIENT_SECRET;
$auth = base64_encode($credentials);
$url = $base . '/v1/oauth2/token';
$body = http_build_query(array('grant_type' => 'client_credentials'));
// Initialize curl
$ch = curl_init();
// Set curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . $auth,
));
// Execute curl and get the response
$data_json = curl_exec($ch);
if ( $data_json === false)
throw new Error('cURL error: ' . curl_error($ch));
// Close curl
curl_close($ch);
$data = json_decode($data_json);
return $data->access_token;
}
catch (Throwable $error) {
error_log("Failed to generate Access Token:");
error_log($error->getMessage());
}
};
/*
*/
/**
* Create an order to start the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
*/
function create_order( $cart )
{
// use the cart information passed from the front-end to calculate the purchase unit details
$access_token = generate_access_token();
error_log("access_token:");
error_log($access_token);
/*
const url = `${base}/v2/checkout/orders`;
const payload = {
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "100.00",
},
},
],
};
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
// Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
// https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
// "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
// "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
// "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
},
method: "POST",
body: JSON.stringify(payload),
});
return handleResponse(response);
*/
};
?>