From 0f15b67e8b89a3f79e3e5bc3ac3c18c1ea75b7c6 Mon Sep 17 00:00:00 2001 From: asus Date: Fri, 23 Feb 2024 21:03:34 +0100 Subject: [PATCH] wip route api_order --- plugins/fipfcard_plugin/js/paypal/paypal.js | 2 +- .../php/paypal/route_api_orders.php | 162 ++++++++---------- .../php/paypal/route_api_utils.php | 44 +++++ plugins/fipfcard_plugin/php/utils/globals.php | 18 +- 4 files changed, 127 insertions(+), 99 deletions(-) create mode 100644 plugins/fipfcard_plugin/php/paypal/route_api_utils.php diff --git a/plugins/fipfcard_plugin/js/paypal/paypal.js b/plugins/fipfcard_plugin/js/paypal/paypal.js index 3ead6df..0ce5d9c 100644 --- a/plugins/fipfcard_plugin/js/paypal/paypal.js +++ b/plugins/fipfcard_plugin/js/paypal/paypal.js @@ -68,7 +68,7 @@ paypal.Buttons({ window.paypal.Buttons({ //createOrder: function(data, actions) { createOrder: function() { - PLGNTLS_fetch("paypal_orders", { + PLGNTLS_fetch("paypal_api_orders", { //method: "POST", //headers: {"Content-Type": "application/json"}, //body: { diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index 326abe3..1cc6188 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -1,15 +1,12 @@ "CAPTURE", + 'purchase_units' => array( + array( + 'amount' => array( + 'currency_code' => "USD", + 'value' => "100.00", + ), + ), + ), + ); + + // Initialize cURL session + $ch = curl_init(); + // Set cURL options + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'Authorization: Bearer ' . $access_token, + // 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"}' + )); + + // Execute cURL session and get the response + $response = curl_exec($ch); + + if ($response === false) + throw new Exception('cURL error: ' . curl_error($ch)); + // Close cURL session + curl_close($ch); + + // in utils + return handle_response($response); +}; + + + /** * 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( ) +function generate_access_token() { - $base = "https://api-m.sandbox.paypal.com"; - - try { + try + { if ( !PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET ) { - throw new Error( "MISSING_API_CREDENTIALS" ); + throw new Exception( "MISSING_API_CREDENTIALS" ); } $credentials = PAYPAL_CLIENT_ID . ":" . PAYPAL_CLIENT_SECRET; $auth = base64_encode($credentials); - $url = $base . '/v1/oauth2/token'; + $url = PAYPAL_API_BASE_URL . '/v1/oauth2/token'; $body = http_build_query(array('grant_type' => 'client_credentials')); // Initialize curl $ch = curl_init(); @@ -69,7 +121,7 @@ function generate_access_token( ) $data_json = curl_exec($ch); if ( $data_json === false) - throw new Error('cURL error: ' . curl_error($ch)); + throw new Exception('cURL error: ' . curl_error($ch)); // Close curl curl_close($ch); @@ -77,90 +129,12 @@ function generate_access_token( ) return $data->access_token; } - catch (Throwable $error) { + catch (Exception $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); - -*/ -}; - ?> diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_utils.php b/plugins/fipfcard_plugin/php/paypal/route_api_utils.php new file mode 100644 index 0000000..ecf0404 --- /dev/null +++ b/plugins/fipfcard_plugin/php/paypal/route_api_utils.php @@ -0,0 +1,44 @@ + $json_response, + 'http_status_code' => http_response_code() + ); + } + catch (Exception $err) + { + // Get error message from response + $error_message = $response->text(); + throw new Exception($error_message); + } +} + +/* + +async function handleResponse(response) { + try { + const jsonResponse = await response.json(); + return { + jsonResponse, + httpStatusCode: response.status, + }; + } catch (err) { + const errorMessage = await response.text(); + throw new Error(errorMessage); + } +} + +*/ + + +?> diff --git a/plugins/fipfcard_plugin/php/utils/globals.php b/plugins/fipfcard_plugin/php/utils/globals.php index faafd95..397f756 100644 --- a/plugins/fipfcard_plugin/php/utils/globals.php +++ b/plugins/fipfcard_plugin/php/utils/globals.php @@ -24,15 +24,25 @@ $fipfcard_ajax_file = "utils/ajax.js"; /** + * paypal credentials * * LIVE : -const PAYPAL_CLIENT_ID = "Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO"; -const PAYPAL_CLIENT_SECRET = "EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra"; + * + * const PAYPAL_CLIENT_ID = "Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO"; + * const PAYPAL_CLIENT_SECRET = "EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra"; + * * SANBOX : -const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; -const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-"; + * + * const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; + * const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-"; + * */ const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-"; +/** + * paypal api base url + */ +const PAYPAL_API_BASE_URL = "https://api-m.sandbox.paypal.com"; + ?>