prefixed all paypal related fonctions with fipf

This commit is contained in:
asus
2024-02-25 15:00:21 +01:00
parent 4e094889c7
commit ec18dc60b4
6 changed files with 59 additions and 60 deletions

View File

@@ -98,7 +98,7 @@ add_shortcode('custom_frontend_posting_form', 'custom_frontend_posting_form');
* call to paypal_shortcode_content() * call to paypal_shortcode_content()
*/ */
include_once(PLGNTLS_class::get_path() . '/php/paypal/paypal.php'); include_once(PLGNTLS_class::get_path() . '/php/paypal/paypal.php');
add_shortcode('fipf_paypal_shortcode', 'paypal_shortcode_content'); add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content');

View File

@@ -38,7 +38,7 @@ add_action('template_redirect', 'check_paypal_request');
function paypal_shortcode_content() function fipf_paypal_shortcode_content()
{ {
$fipfcard_paypal = new PLGNTLS_class(); $fipfcard_paypal = new PLGNTLS_class();
@@ -69,13 +69,13 @@ function paypal_shortcode_content()
* the js file paypal.js needs to be imported as a module to use import * the js file paypal.js needs to be imported as a module to use import
* @see https://developer.wordpress.org/reference/hooks/script_loader_tag/ * @see https://developer.wordpress.org/reference/hooks/script_loader_tag/
*/ */
add_filter( 'script_loader_tag', 'add_id_to_script', 10, 3 ); function fipf_add_id_to_script( $tag, $handle, $src ) {
function add_id_to_script( $tag, $handle, $src ) {
if ( $handle === 'PLGNTLS_paypal_js' ) { if ( $handle === 'PLGNTLS_paypal_js' ) {
$tag = '<script type="module" src="' . esc_url( $src ) . '" ></script>'; $tag = '<script type="module" src="' . esc_url( $src ) . '" ></script>';
} }
return $tag; return $tag;
} }
add_filter( 'script_loader_tag', 'fipf_add_id_to_script', 10, 3 );
// handling routes and endpoints // handling routes and endpoints
@@ -85,12 +85,12 @@ function fipf_routes_endpoints()
$base_rest_route = "fipf_plugin/api/v1"; $base_rest_route = "fipf_plugin/api/v1";
register_rest_route($base_rest_route, '/orders', array( register_rest_route($base_rest_route, '/orders', array(
'methods' => 'POST', 'methods' => 'POST',
'callback' => 'handle_orders_request', 'callback' => 'fipf_handle_orders_request',
)); ));
// https://local_fipfcard_plugin.com/wp-json/fipf_plugin/api/v1/orders/21T129305J264761D/capture // https://local_fipfcard_plugin.com/wp-json/fipf_plugin/api/v1/orders/21T129305J264761D/capture
register_rest_route($base_rest_route, '/orders/(?P<orderID>[a-zA-Z0-9]+)/capture', array( register_rest_route($base_rest_route, '/orders/(?P<orderID>[a-zA-Z0-9]+)/capture', array(
'methods' => 'POST', 'methods' => 'POST',
'callback' => 'handle_orders_capture_request', 'callback' => 'fipf_handle_orders_capture_request',
)); ));
}; };
add_action('rest_api_init', 'fipf_routes_endpoints'); add_action('rest_api_init', 'fipf_routes_endpoints');

View File

@@ -15,17 +15,17 @@ if (!defined('ABSPATH')) {
/** /**
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
*/ */
function handle_orders_request($request_data) { function fipf_handle_orders_request($request_data) {
try { try {
// Extract cart information from request body // Extract cart information from request body
$cart = $request_data['cart']; $cart = $request_data['cart'];
// Process the order and get the response // Process the order and get the response
$order_response = create_order($cart); $order_response = fipf_create_order($cart);
$json_response = $order_response['json_response']; $json_response = $order_response['json_response'];
$http_status_code = $order_response['http_status_code']; $http_status_code = $order_response['http_status_code'];
update_user_payment($json_response, 'start'); fipf_update_user_payment($json_response, 'start');
// Return response // Return response
return new WP_REST_Response($json_response, $http_status_code); return new WP_REST_Response($json_response, $http_status_code);
@@ -43,11 +43,11 @@ function handle_orders_request($request_data) {
* Create an order to start the transaction. * Create an order to start the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
*/ */
function create_order( $cart ) function fipf_create_order( $cart )
{ {
// use the cart information passed from the front-end to calculate the purchase unit details // use the cart information passed from the front-end to calculate the purchase unit details
$access_token = generate_access_token(); $access_token = fipf_generate_access_token();
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders'; $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders';
$payload = array( $payload = array(
@@ -89,7 +89,7 @@ function create_order( $cart )
curl_close($ch); curl_close($ch);
// in utils // in utils
return handle_response($response); return fipf_handle_response($response);
}; };

View File

@@ -10,18 +10,18 @@ if (!defined('ABSPATH')) {
die('You can not access this file!'); die('You can not access this file!');
} }
function handle_orders_capture_request($request) { function fipf_handle_orders_capture_request($request) {
$order_id = $request['orderID']; $order_id = $request['orderID'];
try { try {
// Implement captureOrder function logic here // Implement captureOrder function logic here
// Make sure you implement captureOrder function similar to the Node.js code // Make sure you implement captureOrder function similar to the Node.js code
$response_data = capture_order($order_id); $response_data = fipf_capture_order($order_id);
$http_status_code = $response_data['http_status_code']; $http_status_code = $response_data['http_status_code'];
$json_response = $response_data['json_response']; $json_response = $response_data['json_response'];
update_user_payment($json_response, 'end'); fipf_update_user_payment($json_response, 'end');
return new WP_REST_Response($json_response, $http_status_code); return new WP_REST_Response($json_response, $http_status_code);
} }
@@ -36,8 +36,8 @@ function handle_orders_capture_request($request) {
* Capture payment for the created order to complete the transaction. * Capture payment for the created order to complete the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture * @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
*/ */
function capture_order($orderID) { function fipf_capture_order($orderID) {
$access_token = generate_access_token(); $access_token = fipf_generate_access_token();
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture'; $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture';
$headers = array( $headers = array(
@@ -66,7 +66,7 @@ function capture_order($orderID) {
curl_close($ch); curl_close($ch);
// in utils // in utils
return handle_response($response); return fipf_handle_response($response);
}; };

View File

@@ -12,7 +12,7 @@ if (!defined('ABSPATH')) {
/** /**
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
*/ */
function handle_response($response) { function fipf_handle_response($response) {
try try
{ {
// Decode JSON response // Decode JSON response
@@ -54,7 +54,7 @@ async function handleResponse(response) {
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
* @see https://developer.paypal.com/api/rest/authentication/ * @see https://developer.paypal.com/api/rest/authentication/
*/ */
function generate_access_token() function fipf_generate_access_token()
{ {
try try
{ {

View File

@@ -10,22 +10,6 @@ if (!defined('ABSPATH')) {
// function check_for_paypal_order_id($user_login, $user)
// {
// $user_id = $user->ID;
// //error_log("--- in check_for_paypal_order_id");
// //error_log("user_id:");
// //error_log($user_id);
//
// // // Save a value in the database
// // update_option('my_custom_option', 'my_value');
// // // Retrieve the value from the database
// // $my_value = get_option('my_custom_option');
//
// }
// add_action('wp_login', 'check_for_paypal_order_id', 10, 2);
/** /**
* see documentation in private 'paypal.md' * see documentation in private 'paypal.md'
* basically it check if the user who initiate the transaction * basically it check if the user who initiate the transaction
@@ -45,7 +29,7 @@ if (!defined('ABSPATH')) {
* ['aaaaaa'] - $del_ret === false * ['aaaaaa'] - $del_ret === false
* *
*/ */
function update_user_payment($message, $step) function fipf_update_user_payment($message, $step)
{ {
$order_id = $message->id; $order_id = $message->id;
@@ -80,34 +64,17 @@ error_log("... update user");
// it can duplicate, it's not a problem : delete_user_meta will delete all // it can duplicate, it's not a problem : delete_user_meta will delete all
add_user_meta($user_id, 'fipf_order_id', $order_id); add_user_meta($user_id, 'fipf_order_id', $order_id);
// add a schedule event to delete this order_id // add a schedule event to delete this order_id
// after 3 days ? fipf_schedule_delete_orderid($user_id, $order_id);
// time() + 60 = one minute from now
// time() + MINUTE_IN_SECONDS = one minute from now
// -> https://codex.wordpress.org/Easier_Expression_of_Time_Constants
// -> also strtotime : https://www.php.net/manual/en/function.strtotime.php
$delay = time() + MINUTE_IN_SECONDS;
wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id));
// if transaction is COMPLETED, then delete order_id and update user // if transaction is COMPLETED, then delete order_id and update user
if ($status === 'COMPLETED') if ($status === 'COMPLETED')
{ {
// find the user containing the order_id and delete this order_id // find the user containing the order_id and delete this order_id
$user_id_to_update = delete_order_id($order_id, $user_id); $user_id_to_update = fipf_delete_order_id_on_success($user_id, $order_id);
// proceed to validate payment for user // proceed to validate payment for user
fipf_validate_payment_for_user($user_id_to_update, $order_id);
} }
// if ($step === 'start')
// {
// add_user_meta($user_id, 'fipf_order_id', $order_id);
// // add a schedule event to delete this order_id after 3 days ?
// }
// else if ($step === 'end')
// {
// // find the user containing the order_id and delete this order_id
// $user_id_to_update = delete_order_id($order_id, $user_id);
// // if purchase is success, proceed to validate on user :
// }
$user_meta_order_id = get_user_meta($user_id, 'fipf_order_id'); $user_meta_order_id = get_user_meta($user_id, 'fipf_order_id');
error_log("user_meta->fipf_order_id :"); error_log("user_meta->fipf_order_id :");
@@ -116,16 +83,48 @@ error_log("--- out update_user_payment");
} }
/**
*
*/
function fipf_validate_payment_for_user($user_id, $order_id)
{
}
/**
* add a schedule event to delete this order_id
* after 3 days ?
* time() + 60 = one minute from now
* time() + MINUTE_IN_SECONDS = one minute from now
* -> https://codex.wordpress.org/Easier_Expression_of_Time_Constants
* -> also strtotime : https://www.php.net/manual/en/function.strtotime.php
*/
function fipf_schedule_delete_orderid($user_id, $order_id)
{
$delay = time() + MINUTE_IN_SECONDS;
wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id));
}
/** /**
* action hook for the scheduled event * action hook for the scheduled event
* TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre * TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre
*/ */
function fipf_delete_order_id($user_id, $order_id) function fipf_delete_order_id_later($user_id, $order_id)
{ {
error_log("delete order_id[$order_id] from user_id[$user_id]"); error_log("delete order_id[$order_id] from user_id[$user_id]");
delete_user_meta($user_id, 'fipf_order_id', $order_id); delete_user_meta($user_id, 'fipf_order_id', $order_id);
} }
add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id', 5, 2); add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id_later', 5, 2);
@@ -133,7 +132,7 @@ add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id', 5, 2);
* @return mixed num - user_id * @return mixed num - user_id
* bool false - if no match found * bool false - if no match found
*/ */
function delete_order_id($order_id, $current_user_id) function fipf_delete_order_id_on_success($current_user_id, $order_id)
{ {
$del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id); $del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id);
if ($del_ret === true) if ($del_ret === true)