wip payment kind of works with bugs
This commit is contained in:
@@ -15,18 +15,17 @@ export async function createOrder() {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
//"X-WP-Nonce": PLGNTLS_data.rest_nonce,
|
||||
},
|
||||
// use the "body" param to optionally pass additional order information
|
||||
// like product ids and quantities
|
||||
body: JSON.stringify({
|
||||
cart: [
|
||||
{
|
||||
id: "1234",
|
||||
quantity: "1",
|
||||
},
|
||||
],
|
||||
}),
|
||||
//// use the "body" param to optionally pass additional order information
|
||||
//// like product ids and quantities
|
||||
//body: JSON.stringify({
|
||||
// cart: [
|
||||
// {
|
||||
// id: "1234",
|
||||
// quantity: "1",
|
||||
// },
|
||||
// ],
|
||||
//}),
|
||||
});
|
||||
|
||||
const orderData = await response.json();
|
||||
|
||||
14
plugins/fipfcard_plugin/php/can_user_pay.php
Normal file
14
plugins/fipfcard_plugin/php/can_user_pay.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -1,15 +1,22 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/user_can_pay.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/shortcode.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_orders.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_orders_capture.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/routes.php');
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -39,72 +46,5 @@ add_action('template_redirect', 'check_paypal_request');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* call to paypal_shortcode_content()
|
||||
*/
|
||||
function paypal_shortcode_content_FIPF()
|
||||
{
|
||||
$fipfcard_paypal = new PLGNTLS_class();
|
||||
|
||||
$pp_sdk_currency = "EUR";
|
||||
$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_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', 'paypal_shortcode_content_FIPF');
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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 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', 'add_id_to_script_FIPF', 10, 3 );
|
||||
*/
|
||||
|
||||
|
||||
// handling routes and endpoints
|
||||
// diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573
|
||||
function routes_endpoints_FIPF()
|
||||
{
|
||||
$base_rest_route = "fipf_plugin/api/v1";
|
||||
register_rest_route($base_rest_route, '/orders', array(
|
||||
'methods' => 'POST',
|
||||
'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' => 'handle_orders_capture_request_FIPF',
|
||||
));
|
||||
};
|
||||
add_action('rest_api_init', 'routes_endpoints_FIPF');
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php');
|
||||
include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php');
|
||||
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
@@ -18,10 +15,17 @@ include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php');
|
||||
function handle_orders_request_FIPF($request_data) {
|
||||
try {
|
||||
// Extract cart information from request body
|
||||
$cart = $request_data['cart'];
|
||||
//$cart = $request_data['cart'];
|
||||
|
||||
$can_pay = can_pay_now_CIPF();
|
||||
if ($can_pay['success'] === false)
|
||||
throw new Exception($can_pay['message']);
|
||||
error_log("can_pay:");
|
||||
error_log($can_pay);
|
||||
|
||||
// Process the order and get the response
|
||||
$order_response = create_order_FIPF($cart);
|
||||
//$order_response = create_order_FIPF($cart);
|
||||
$order_response = create_order_FIPF();
|
||||
$json_response = $order_response['json_response'];
|
||||
$http_status_code = $order_response['http_status_code'];
|
||||
|
||||
@@ -29,10 +33,11 @@ function handle_orders_request_FIPF($request_data) {
|
||||
|
||||
// Return response
|
||||
return new WP_REST_Response($json_response, $http_status_code);
|
||||
} catch (Exception $e) {
|
||||
} catch (Exception $error) {
|
||||
// Handle errors
|
||||
error_log('Failed to create order: ' . $e->getMessage());
|
||||
return new WP_Error('500', 'Failed to create order.', array('status' => 500));
|
||||
error_log('Failed to create order: ');
|
||||
error_log(json_encode($error));
|
||||
return new WP_Error('500', 'Failed to create order :' . $error->getMessage(), array('status' => 500));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,12 +48,17 @@ function handle_orders_request_FIPF($request_data) {
|
||||
* Create an order to start the transaction.
|
||||
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
|
||||
*/
|
||||
function create_order_FIPF( $cart )
|
||||
//function create_order_FIPF($cart)
|
||||
function create_order_FIPF()
|
||||
{
|
||||
// use the cart information passed from the front-end to calculate the purchase unit details
|
||||
|
||||
$access_token = generate_access_token_FIPF();
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
$acf_id = 'user_' . $user_id;
|
||||
$price = get_field('somme_a_regler', $acf_id);
|
||||
|
||||
$url = PAYPAL_API_BASE_URL . '/v2/checkout/orders';
|
||||
$payload = array(
|
||||
'intent' => "CAPTURE",
|
||||
@@ -56,7 +66,7 @@ function create_order_FIPF( $cart )
|
||||
array(
|
||||
'amount' => array(
|
||||
'currency_code' => "EUR",
|
||||
'value' => "1.00",
|
||||
'value' => $price,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
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 handle_orders_capture_request_FIPF($request) {
|
||||
$order_id = $request['orderID'];
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
32
plugins/fipfcard_plugin/php/paypal/routes.php
Normal file
32
plugins/fipfcard_plugin/php/paypal/routes.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// handling routes and endpoints
|
||||
// diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573
|
||||
function routes_endpoints_FIPF()
|
||||
{
|
||||
$base_rest_route = "fipf_plugin/api/v1";
|
||||
register_rest_route($base_rest_route, '/orders', array(
|
||||
'methods' => 'POST',
|
||||
'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' => 'handle_orders_capture_request_FIPF',
|
||||
));
|
||||
};
|
||||
add_action('rest_api_init', 'routes_endpoints_FIPF');
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
59
plugins/fipfcard_plugin/php/paypal/shortcode.php
Normal file
59
plugins/fipfcard_plugin/php/paypal/shortcode.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* it means someone outside wp is accessing the file, in this case kill it.
|
||||
*/
|
||||
if (!defined('ABSPATH')) {
|
||||
die('You can not access this file!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* call to paypal_shortcode_content()
|
||||
*/
|
||||
function paypal_shortcode_content_FIPF()
|
||||
{
|
||||
// if (!can_pay_now_CIPF())
|
||||
// return no_payment_CIPF();
|
||||
$fipfcard_paypal = new PLGNTLS_class();
|
||||
|
||||
$pp_client_id = PAYPAL_CLIENT_ID;
|
||||
$pp_sdk_currency = "EUR";
|
||||
$pp_sdk_base_url = "https://www.paypal.com";
|
||||
$pp_sdk_url = "{$pp_sdk_base_url}/sdk/js?client-id={$pp_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_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', 'paypal_shortcode_content_FIPF');
|
||||
|
||||
|
||||
function no_payment_CIPF() {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -87,7 +87,6 @@ function update_user_payment_FIPF($message, $step)
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* 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)
|
||||
@@ -102,26 +101,30 @@ function update_user_payment_FIPF($message, $step)
|
||||
* -> https://www.advancedcustomfields.com/resources/date-time-picker/
|
||||
*/
|
||||
function validate_payment_for_user_FIPF($user_id, $order_id) {
|
||||
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
|
||||
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;
|
||||
|
||||
$acf_date_format = 'Y-m-d H:i:s';
|
||||
$acf_id = 'user_'.$user_id;
|
||||
|
||||
/*
|
||||
* update card validity to true
|
||||
update_field(CARD_IS_VALID, true, $acf_id);
|
||||
*/
|
||||
$post_id = 'user_'.$user_id;
|
||||
update_field(CARD_IS_VALID, true, $post_id);
|
||||
update_field($acf_card_state, 'Renouvellement', $acf_id);
|
||||
|
||||
$date_now = date($acf_date_format);
|
||||
/*
|
||||
* update purchase date to now
|
||||
update_field(CARD_DATE_PURCHASE, $date_now, $acf_id);
|
||||
*/
|
||||
$date_now = date($acf_date_format);
|
||||
update_field(CARD_DATE_PURCHASE, $date_now, $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);
|
||||
$current_date_limit = get_field($acf_card_expiration, $acf_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
|
||||
@@ -147,7 +150,7 @@ function validate_payment_for_user_FIPF($user_id, $order_id) {
|
||||
*/
|
||||
$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);
|
||||
update_field($acf_card_expiration, $new_date_limit, $acf_id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
94
plugins/fipfcard_plugin/php/paypal/user_can_pay.php
Normal file
94
plugins/fipfcard_plugin/php/paypal/user_can_pay.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* can pay in certain conditions
|
||||
* default true
|
||||
*/
|
||||
function can_pay_now_CIPF() {
|
||||
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
|
||||
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
|
||||
$acf_card_price_choice = PLGNTLS_class::ACF_CARD_PRICE_CHOICE;
|
||||
$acf_card_price_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY;
|
||||
$acf_card_price_total = PLGNTLS_class::ACF_CARD_PRICE_TOTAL;
|
||||
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;
|
||||
$card_renew_period = PLGNTLS_class::CARD_RENEW_PERIOD;
|
||||
|
||||
$current_user = wp_get_current_user();
|
||||
$user_id = get_current_user_id();
|
||||
$acf_id = 'user_' . $user_id;
|
||||
|
||||
|
||||
/*
|
||||
* check if payment is virement or immediat
|
||||
*
|
||||
$payement = get_field($acf_card_payment_method, $acf_id);
|
||||
if (strtolower($payement) === 'virement') {
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
* calculate price
|
||||
* update the price even if form builder already did it
|
||||
* in case it was changed from admin pannel
|
||||
*
|
||||
*/
|
||||
$tarif = (int)get_field($acf_card_price_choice, $acf_id);
|
||||
$livraison = (int)get_field($acf_card_price_delivery, $acf_id);
|
||||
$price = $tarif + $livraison;
|
||||
update_field($acf_card_price_total, $price, $acf_id);
|
||||
|
||||
/*
|
||||
* price is not empty or 0
|
||||
*
|
||||
*/
|
||||
$price = get_field($acf_card_price_total, $acf_id);
|
||||
if (empty($price)) {
|
||||
return array('success' => false, 'message' => "error: no price selected");
|
||||
}
|
||||
if ($price === 0) {
|
||||
return array('success' => false, 'message' => "error: price is 0, nothing to purchase");
|
||||
}
|
||||
|
||||
/*
|
||||
* date validity is empty
|
||||
* or is paste
|
||||
* or is less than 1 month
|
||||
*
|
||||
*/
|
||||
$validity_field = get_field_object($acf_card_expiration, $acf_id);
|
||||
$validity = $validity_field['value'];
|
||||
$format_field = $validity_field['return_format'];
|
||||
$format_acf = 'Y-m-d H:i:s';
|
||||
|
||||
if (empty($validity))
|
||||
return array('success' => true);
|
||||
|
||||
$date_now = date_create('now');
|
||||
$date_validity = date_create_from_format($format_field, $validity);
|
||||
$diff = date_diff($date_now, $date_validity)->format('%R%a');
|
||||
error_log("diff");
|
||||
error_log($diff);
|
||||
if ((int)$diff <= 0) {
|
||||
// date end of validity in the past
|
||||
return array('success' => true);
|
||||
}
|
||||
else if ((int)$diff <= $card_renew_period) {
|
||||
// date expiration is in less that renew period time (ex: 30 days)
|
||||
return array('success' => true);
|
||||
}
|
||||
else {
|
||||
// date end of validity is in more than renew perdio (ex: 3 month)
|
||||
return array('success' => false, 'message' => "error: it's too soon to renew your card");
|
||||
}
|
||||
|
||||
return array('success' => true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
@@ -12,7 +12,7 @@ if (!defined('ABSPATH')) {
|
||||
/*
|
||||
* redirect users to profil
|
||||
* if prof -> author page
|
||||
* if partenaire -> post page / project page / home page
|
||||
* if partenaire -> post page || home page
|
||||
*/
|
||||
function redirection_page_CIPF(){
|
||||
if (!is_page(PROF_PARTENAIRE_REDIRECTION_PAGE))
|
||||
@@ -29,7 +29,7 @@ function redirection_page_CIPF(){
|
||||
}
|
||||
else if (current_user_can('partenaire')) {
|
||||
$args = array(
|
||||
'post_type' => 'post,project',
|
||||
'post_type' => 'post',
|
||||
'author' => $current_user_id,
|
||||
'posts_per_page' => 1,
|
||||
);
|
||||
|
||||
@@ -3,17 +3,14 @@
|
||||
|
||||
/*
|
||||
function reset_some_fields_CIPF($form_id, $user_id, $post_array) {
|
||||
*/
|
||||
function reset_some_fields_CIPF() {
|
||||
$user_id = get_current_user_id();
|
||||
update_field('cgv', array(""), 'user_'.$user_id);
|
||||
update_field('paiement', array(""), 'user_'.$user_id);
|
||||
update_field('livraison', array(""), 'user_'.$user_id);
|
||||
}
|
||||
add_shortcode('test_reset_acf', 'reset_some_fields_CIPF');
|
||||
*/
|
||||
/*
|
||||
add_action('df_after_insert_user', 'reinit_some_fields_CIPF', 10, 3);
|
||||
*/
|
||||
add_action('df_after_insert_user', 'reset_some_fields_CIPF');
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -56,6 +56,14 @@ if (!defined('ABSPATH')) {
|
||||
|
||||
class PLGNTLS_class
|
||||
{
|
||||
const ACF_CARD_STATE = 'etat_carte';
|
||||
const ACF_CARD_PAYMENT_METHOD = 'paiement';
|
||||
const ACF_CARD_PRICE_CHOICE = 'tarif';
|
||||
const ACF_CARD_PRICE_DELIVERY = 'livraison';
|
||||
const ACF_CARD_PRICE_TOTAL = 'somme_a_regler';
|
||||
const ACF_CARD_EXPIRATION = 'fin_de_validite';
|
||||
const CARD_RENEW_PERIOD = 31; // number of days before expiration when renew card start to be possible
|
||||
|
||||
private static $_root_path;
|
||||
private static $_root_url;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user