- wip payment
- already solved success and failure messages and redirection - solved increase date multiple times - users redirection works if error AND partner to posts instead of project
This commit is contained in:
@@ -42,36 +42,36 @@ add_action('template_redirect', 'check_paypal_request');
|
||||
/**
|
||||
* call to paypal_shortcode_content()
|
||||
*/
|
||||
function fipf_paypal_shortcode_content()
|
||||
function paypal_shortcode_content_FIPF()
|
||||
{
|
||||
$fipfcard_paypal = new PLGNTLS_class();
|
||||
|
||||
$pp_sdk_currency = "EUR";
|
||||
$pp_sdk_debug = "true";
|
||||
$pp_sdk_base_url="https://sandbox.paypal.com";
|
||||
$pp_sdk_base_url="https://www.paypal.com";
|
||||
// $pp_sdk_client_token="abc123xyz==";
|
||||
$pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID . "¤cy=$pp_sdk_currency&debug=$pp_sdk_debug";
|
||||
$pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID ;
|
||||
// $pp_sdk_attributes="src='$pp_sdk_src' data-client-token='$pp_sdk_client_token'";
|
||||
// $pp_sdk_attributes="src='$pp_sdk_src'";
|
||||
// $pp_sdk_html_script="<script $pp_sdk_attributes></script>";
|
||||
$pp_sdk_base_url = "https://www.paypal.com";
|
||||
$pp_sdk_url = "$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID . "¤cy=$pp_sdk_currency";
|
||||
|
||||
$paypal_redirection_success = PAYPAL_REDIRECTION_SUCCESS;
|
||||
$paypal_redirection_failure = PAYPAL_REDIRECTION_FAILURE;
|
||||
$paypal_message_success = PAYPAL_MESSAGE_SUCCESS;
|
||||
$paypal_message_failure = PAYPAL_MESSAGE_FAILURE;
|
||||
|
||||
$added_to_front = $fipfcard_paypal->add_to_front(
|
||||
array(
|
||||
$pp_sdk_src,
|
||||
// 'js/paypal/result_message.js',
|
||||
// 'js/paypal/create_order.js',
|
||||
// 'js/paypal/on_approve.js',
|
||||
//"js/paypal/paypal.js",
|
||||
$pp_sdk_url,
|
||||
array("js/paypal/paypal.js", 'type'=>'module'),
|
||||
"html/paypal/paypal.html",
|
||||
),
|
||||
compact (
|
||||
'paypal_redirection_success',
|
||||
'paypal_redirection_failure',
|
||||
'paypal_message_success',
|
||||
'paypal_message_failure',
|
||||
),
|
||||
);
|
||||
|
||||
return $added_to_front;
|
||||
}
|
||||
add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content');
|
||||
add_shortcode('fipf_paypal_shortcode', 'paypal_shortcode_content_FIPF');
|
||||
|
||||
|
||||
|
||||
@@ -79,32 +79,32 @@ add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content');
|
||||
/**
|
||||
* 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/
|
||||
function fipf_add_id_to_script( $tag, $handle, $src ) {
|
||||
function add_id_to_script_FIPF( $tag, $handle, $src ) {
|
||||
if ( $handle === 'PLGNTLS_paypal_js' ) {
|
||||
$tag = '<script type="module" src="' . esc_url( $src ) . '" ></script>';
|
||||
}
|
||||
return $tag;
|
||||
}
|
||||
add_filter( 'script_loader_tag', 'fipf_add_id_to_script', 10, 3 );
|
||||
add_filter( 'script_loader_tag', 'add_id_to_script_FIPF', 10, 3 );
|
||||
*/
|
||||
|
||||
|
||||
// handling routes and endpoints
|
||||
// diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573
|
||||
function fipf_routes_endpoints()
|
||||
function routes_endpoints_FIPF()
|
||||
{
|
||||
$base_rest_route = "fipf_plugin/api/v1";
|
||||
register_rest_route($base_rest_route, '/orders', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => 'fipf_handle_orders_request',
|
||||
'callback' => 'handle_orders_request_FIPF',
|
||||
));
|
||||
// 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(
|
||||
'methods' => 'POST',
|
||||
'callback' => 'fipf_handle_orders_capture_request',
|
||||
'callback' => 'handle_orders_capture_request_FIPF',
|
||||
));
|
||||
};
|
||||
add_action('rest_api_init', 'fipf_routes_endpoints');
|
||||
add_action('rest_api_init', 'routes_endpoints_FIPF');
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -15,17 +15,17 @@ include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php');
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
function fipf_handle_orders_request($request_data) {
|
||||
function handle_orders_request_FIPF($request_data) {
|
||||
try {
|
||||
// Extract cart information from request body
|
||||
$cart = $request_data['cart'];
|
||||
|
||||
// Process the order and get the response
|
||||
$order_response = fipf_create_order($cart);
|
||||
$order_response = create_order_FIPF($cart);
|
||||
$json_response = $order_response['json_response'];
|
||||
$http_status_code = $order_response['http_status_code'];
|
||||
|
||||
fipf_update_user_payment($json_response, 'start');
|
||||
update_user_payment_FIPF($json_response, 'start');
|
||||
|
||||
// Return response
|
||||
return new WP_REST_Response($json_response, $http_status_code);
|
||||
@@ -43,11 +43,11 @@ function fipf_handle_orders_request($request_data) {
|
||||
* Create an order to start the transaction.
|
||||
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
|
||||
*/
|
||||
function fipf_create_order( $cart )
|
||||
function create_order_FIPF( $cart )
|
||||
{
|
||||
// use the cart information passed from the front-end to calculate the purchase unit details
|
||||
|
||||
$access_token = fipf_generate_access_token();
|
||||
$access_token = generate_access_token_FIPF();
|
||||
|
||||
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders';
|
||||
$payload = array(
|
||||
@@ -55,8 +55,8 @@ function fipf_create_order( $cart )
|
||||
'purchase_units' => array(
|
||||
array(
|
||||
'amount' => array(
|
||||
'currency_code' => "USD",
|
||||
'value' => "100.00",
|
||||
'currency_code' => "EUR",
|
||||
'value' => "1.00",
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -89,7 +89,7 @@ function fipf_create_order( $cart )
|
||||
curl_close($ch);
|
||||
|
||||
// in utils
|
||||
return fipf_handle_response($response);
|
||||
return handle_response_FIPF($response);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -11,18 +11,18 @@ if (!defined('ABSPATH')) {
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php');
|
||||
|
||||
function fipf_handle_orders_capture_request($request) {
|
||||
function handle_orders_capture_request_FIPF($request) {
|
||||
$order_id = $request['orderID'];
|
||||
|
||||
try {
|
||||
// Implement captureOrder function logic here
|
||||
// Make sure you implement captureOrder function similar to the Node.js code
|
||||
|
||||
$response_data = fipf_capture_order($order_id);
|
||||
$response_data = capture_order_FIPF($order_id);
|
||||
$http_status_code = $response_data['http_status_code'];
|
||||
$json_response = $response_data['json_response'];
|
||||
|
||||
fipf_update_user_payment($json_response, 'end');
|
||||
update_user_payment_FIPF($json_response, 'end');
|
||||
|
||||
return new WP_REST_Response($json_response, $http_status_code);
|
||||
}
|
||||
@@ -37,8 +37,8 @@ function fipf_handle_orders_capture_request($request) {
|
||||
* Capture payment for the created order to complete the transaction.
|
||||
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
|
||||
*/
|
||||
function fipf_capture_order($orderID) {
|
||||
$access_token = fipf_generate_access_token();
|
||||
function capture_order_FIPF($orderID) {
|
||||
$access_token = generate_access_token_FIPF();
|
||||
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture';
|
||||
|
||||
$headers = array(
|
||||
@@ -67,7 +67,7 @@ function fipf_capture_order($orderID) {
|
||||
curl_close($ch);
|
||||
|
||||
// in utils
|
||||
return fipf_handle_response($response);
|
||||
return handle_response_FIPF($response);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ if (!defined('ABSPATH')) {
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
function fipf_handle_response($response) {
|
||||
function handle_response_FIPF($response) {
|
||||
try
|
||||
{
|
||||
// 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/api/rest/authentication/
|
||||
*/
|
||||
function fipf_generate_access_token()
|
||||
function generate_access_token_FIPF()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
/*
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* see documentation in private 'paypal.md'
|
||||
* basically it check if the user who initiate the transaction
|
||||
* is the same that finish it
|
||||
*
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'aaaaaa');
|
||||
* ['aaaaaa']
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa', 'bbbbbb']
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa', 'bbbbbb', 'bbbbbb']
|
||||
* get_user_meta('user_id', 'fipf_order_id');
|
||||
* ['aaaaaa', 'bbbbbb', 'bbbbbb']
|
||||
* $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa'] - $del_ret === true
|
||||
* $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa'] - $del_ret === false
|
||||
*
|
||||
*/
|
||||
function fipf_update_user_payment($message, $step)
|
||||
/*
|
||||
* see documentation in private 'paypal.md'
|
||||
* basically it check if the user who initiate the transaction
|
||||
* is the same that finish it
|
||||
*
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'aaaaaa');
|
||||
* ['aaaaaa']
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa', 'bbbbbb']
|
||||
* add_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa', 'bbbbbb', 'bbbbbb']
|
||||
* get_user_meta('user_id', 'fipf_order_id');
|
||||
* ['aaaaaa', 'bbbbbb', 'bbbbbb']
|
||||
* $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa'] - $del_ret === true
|
||||
* $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb');
|
||||
* ['aaaaaa'] - $del_ret === false
|
||||
*
|
||||
*/
|
||||
function update_user_payment_FIPF($message, $step)
|
||||
{
|
||||
|
||||
$order_id = $message->id;
|
||||
@@ -64,16 +64,14 @@ function fipf_update_user_payment($message, $step)
|
||||
// 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 a schedule event to delete this order_id
|
||||
fipf_schedule_delete_orderid($user_id, $order_id);
|
||||
fipf_validate_payment_for_user($user_id, $order_id);
|
||||
schedule_delete_orderid_FIPF($user_id, $order_id);
|
||||
|
||||
// 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
|
||||
$user_id_to_update = fipf_delete_order_id_on_success($user_id, $order_id);
|
||||
$user_id_to_update = delete_order_id_on_success_FIPF($user_id, $order_id);
|
||||
// proceed to validate payment for user
|
||||
fipf_validate_payment_for_user($user_id_to_update, $order_id);
|
||||
validate_payment_for_user_FIPF($user_id_to_update, $order_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -88,56 +86,65 @@ function fipf_update_user_payment($message, $step)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* change acf field [carte_est_valide](validite) to true
|
||||
* change acf field [date_d_achat](achat) to new current date
|
||||
* change acf field [date_fin_validite](echance) to previous date + 1 year
|
||||
*
|
||||
* create scheduled emails to inform of end of validity
|
||||
*
|
||||
* acf uses 'Y-m-d H:i:s' format :
|
||||
* -> https://www.advancedcustomfields.com/resources/date-time-picker/
|
||||
*/
|
||||
function fipf_validate_payment_for_user($user_id, $order_id)
|
||||
{
|
||||
/*
|
||||
*
|
||||
* const CARD_IS_VALID : acf field 'true false' [carte_est_valide](validite)
|
||||
* const CARD_DATE_PURCHASE : acf field 'date picker' [date_d_achat](achat)
|
||||
* const CARD_DATE_VALIDITY : acf field 'date picker' [date_fin_validite](echance)
|
||||
*
|
||||
* - change CARD_IS_VALID to true
|
||||
* - change CARD_DATE_PURCHASE to now
|
||||
* - change CARD_DATE_VALIDITY to previous value + 1 year
|
||||
*
|
||||
* - create scheduled emails to inform of end of validity
|
||||
*
|
||||
* acf uses 'Y-m-d H:i:s' format :
|
||||
* -> https://www.advancedcustomfields.com/resources/date-time-picker/
|
||||
*/
|
||||
function validate_payment_for_user_FIPF($user_id, $order_id) {
|
||||
$acf_date_format = 'Y-m-d H:i:s';
|
||||
|
||||
/**
|
||||
* update card validity to true
|
||||
*/
|
||||
/*
|
||||
* update card validity to true
|
||||
*/
|
||||
$post_id = 'user_'.$user_id;
|
||||
update_field('carte_est_valide', true, $post_id);
|
||||
update_field(CARD_IS_VALID, true, $post_id);
|
||||
|
||||
/**
|
||||
* update purchase date to now
|
||||
*/
|
||||
/*
|
||||
* update purchase date to now
|
||||
*/
|
||||
$date_now = date($acf_date_format);
|
||||
update_field('date_d_achat', $date_now, $post_id);
|
||||
update_field(CARD_DATE_PURCHASE, $date_now, $post_id);
|
||||
|
||||
/**
|
||||
* update date limit validity to add 1 year
|
||||
*/
|
||||
$current_date_limit = get_field('date_fin_validite', $post_id);
|
||||
/*
|
||||
* get current date limit
|
||||
* if no date, use now
|
||||
* if paste date, use now
|
||||
*/
|
||||
$current_date_limit = get_field(CARD_DATE_VALIDITY, $post_id);
|
||||
// output is in format 'dd/mm/yyyy' which is not understood by php dates functions
|
||||
// so i clean it
|
||||
// -> not a reliable solution, someone can change the ouput format in dashboard
|
||||
$current_date_limit = str_replace('/', '-', $current_date_limit);
|
||||
// id current date limit is not in the futur, use now date
|
||||
if ($current_date_limit === null)
|
||||
|
||||
if ($current_date_limit === null) {
|
||||
$current_date_limit = $date_now;
|
||||
}
|
||||
else
|
||||
{
|
||||
// compare 2 dates : https://stackoverflow.com/q/8722806/9497573
|
||||
// also I dont use strtotime to compare 2 ints,
|
||||
// because i don't know if it will fail one day (2000 bug like)
|
||||
// because i don't know if it will fail one day (2000 bug alike)
|
||||
$comp_current_date_limit = date_create($current_date_limit);
|
||||
$comp_date_now = date_create($date_now);
|
||||
$date_is_in_past = date_diff($comp_date_now, $comp_current_date_limit)->format("%R%a") < 0;
|
||||
if ($date_is_in_past)
|
||||
$current_date_limit = $date_now;
|
||||
}
|
||||
// add one year to current date limit
|
||||
|
||||
/*
|
||||
* update date limit validity to add 1 year
|
||||
*/
|
||||
$time_plus_one_year = strtotime('+1 year', strtotime($current_date_limit));
|
||||
$new_date_limit = date('Y-m-d H:i:s', $time_plus_one_year);
|
||||
update_field('date_fin_validite', $new_date_limit, $post_id);
|
||||
@@ -158,21 +165,21 @@ function fipf_validate_payment_for_user($user_id, $order_id)
|
||||
* -> 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)
|
||||
function schedule_delete_orderid_FIPF($user_id, $order_id)
|
||||
{
|
||||
$delay = time() + MINUTE_IN_SECONDS;
|
||||
wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id));
|
||||
wp_schedule_single_event($delay, 'orderid_deletion_event_FIPF', array($user_id, $order_id));
|
||||
}
|
||||
/**
|
||||
* action hook for the scheduled event
|
||||
* TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre
|
||||
*/
|
||||
function fipf_delete_order_id_later($user_id, $order_id)
|
||||
function delete_order_id_later_FIPF($user_id, $order_id)
|
||||
{
|
||||
error_log("delete order_id[$order_id] from user_id[$user_id]");
|
||||
delete_user_meta($user_id, 'fipf_order_id', $order_id);
|
||||
}
|
||||
add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id_later', 5, 2);
|
||||
add_action('orderid_deletion_event_FIPF', 'delete_order_id_later_FIPF', 5, 2);
|
||||
|
||||
|
||||
|
||||
@@ -183,7 +190,7 @@ add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id_later', 5, 2);
|
||||
* @return mixed num - user_id
|
||||
* bool false - if no match found
|
||||
*/
|
||||
function fipf_delete_order_id_on_success($current_user_id, $order_id)
|
||||
function delete_order_id_on_success_FIPF($current_user_id, $order_id)
|
||||
{
|
||||
$del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id);
|
||||
if ($del_ret === true)
|
||||
|
||||
Reference in New Issue
Block a user