added options in paypal credential and payment

This commit is contained in:
asus
2024-04-09 18:16:38 +02:00
parent 731b69d97d
commit ec9bddaefd
6 changed files with 124 additions and 61 deletions

View File

@@ -136,9 +136,12 @@ class Cipf {
'_name'=>'cipf_paypal_credentials',
'_callback'=>'update_paypal_credentials_CIPF',
'_default' => [
'force_1_cent' =>false,
'is_sandbox' =>true,
'client_id' =>'AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl',
'client_secret'=>'EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-',
'live_client_id' =>'Aedn5e8z__hPBvKirqw5bwlhI9ChG8_N6c1xbgybYyBr4B4oP8uVzmVdH1QVKdPQKf6bWg7orPV4PDrO',
'live_client_secret' =>'EGeGwfHGxHxsjnC-tH8W0IL4nN3_xlc3sXFRPCQOw5uUoWae3eOgghuDKMnZc5DVGTbP6yIjVJ1BaAra',
'sandbox_client_id' =>'AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl',
'sandbox_client_secret'=>'EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-',
],
];
const OPTION_PAYMENT = [

View File

@@ -68,24 +68,39 @@
<!-- https://developer.wordpress.org/reference/hooks/admin_post_action/ -->
<h1>paypal credentials</h1>
<?php echo Plgntls::open_form_option($option_paypal['_name']); ?>
<div class="define_paypal_credentials_cipf define_paypal_client_id_cipf vertical_wrapper_cipf">
<label for="paypal_client_id_cipf">client id : </label>
<input type="text" id="paypal_client_id_cipf" name="client_id" value="<?php echo $option_paypal['client_id']; ?>" />
<div class="define_paypal_credentials_cipf vertical_wrapper_cipf">
<label for="paypal_live_client_id_cipf">live client id : </label>
<input type="text" id="paypal_live_client_id_cipf" name="live_client_id" value="<?php echo $option_paypal['live_client_id']; ?>" />
</div>
<div class="define_paypal_credentials_cipf define_paypal_secret_id_cipf vertical_wrapper_cipf">
<label for="paypal_client_secret_cipf">client secret : </label>
<input type="text" id="paypal_client_secret_cipf" name="client_secret" value="<?php echo $option_paypal['client_secret']; ?>" />
<div class="define_paypal_credentials_cipf vertical_wrapper_cipf">
<label for="paypal_live_client_secret_cipf">live client secret : </label>
<input type="text" id="paypal_live_client_secret_cipf" name="live_client_secret" value="<?php echo $option_paypal['live_client_secret']; ?>" />
</div>
<div class="define_paypal_credentials_cipf vertical_wrapper_cipf">
<label for="paypal_sandbox_client_id_cipf">sandbox client id : </label>
<input type="text" id="paypal_sandbox_client_id_cipf" name="sandbox_client_id" value="<?php echo $option_paypal['sandbox_client_id']; ?>" />
</div>
<div class="define_paypal_credentials_cipf vertical_wrapper_cipf">
<label for="paypal_sandbox_client_secret_cipf">sandbox client secret : </label>
<input type="text" id="paypal_sandbox_client_secret_cipf" name="sandbox_client_secret" value="<?php echo $option_paypal['sandbox_client_secret']; ?>" />
</div>
<div class="choose_sandbox_live_cipf choose_sandbox_cipf">
<input type="radio" id="sandbox" name="sandbox_or_live" value="sandbox" <?php if ($option_paypal['is_sandbox'] === true) {echo 'checked';} ?> />
<label for="sandbox">sandbox</label>
<input type="radio" id="paypal_credentials_sandbox" name="sandbox_or_live" value="sandbox" <?php if ($option_paypal['is_sandbox'] === true) {echo 'checked';} ?> />
<label for="paypal_credentials_sandbox">sandbox</label>
</div>
<div class="choose_sandbox_live_cipf choose_live_cipf">
<input type="radio" id="live" name="sandbox_or_live" value="live" <?php if ($option_paypal['is_sandbox'] === false) {echo 'checked';} ?> />
<label for="live">live</label>
<input type="radio" id="paypal_credentials_live" name="sandbox_or_live" value="live" <?php if ($option_paypal['is_sandbox'] === false) {echo 'checked';} ?> />
<label for="paypal_credentials_live">live</label>
</div>
<div class="">
<input type="checkbox" id="paypal_credentials_force_1_cent" name="force_1_cent" <?php if ($option_paypal['force_1_cent'] === true) {echo 'checked';} ?> />
<label for="paypal_credentials_force_1_cent">forcer le prix de 0,01€ ?</label>
</div>
<input type="submit" value="send"/>

View File

@@ -54,12 +54,30 @@ function get_paypal_options_CIPF() {
function get_paypal_client_id_CIPF() {
Plgntls::debug_infos();
$paypal_credentials = get_paypal_options_CIPF();
return $paypal_credentials['client_id'];
$is_sandbox = $paypal_credentials['is_sandbox'];
$client_id = '';
if ($is_sandbox) {
$client_id = $paypal_credentials['sandbox_client_id'];
}
else {
$client_id = $paypal_credentials['live_client_id'];
}
return $client_id;
}
function get_paypal_client_secret_CIPF() {
Plgntls::debug_infos();
$paypal_credentials = get_paypal_options_CIPF();
return $paypal_credentials['client_secret'];
$is_sandbox = $paypal_credentials['is_sandbox'];
$client_secret = '';
if ($is_sandbox) {
$client_secret = $paypal_credentials['sandbox_client_secret'];
}
else {
$client_secret = $paypal_credentials['live_client_secret'];
}
return $client_secret;
}
function get_paypal_api_base_url_CIPF() {
Plgntls::debug_infos();
@@ -75,7 +93,31 @@ function get_paypal_api_base_url_CIPF() {
}
return $paypal_base_url;
}
function is_paypal_force_1_cent_CIPF() {
Plgntls::debug_infos();
$paypal_options = get_paypal_options_CIPF();
error_log("paypal_options: " . json_encode($paypal_options));
return $paypal_options['force_1_cent'];
}
function get_paypal_price_CIPF($user_id = null) {
Plgntls::debug_infos();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
if (is_paypal_force_1_cent_CIPF($user_id)) {
$price = "0.01";
}
else {
$price = get_card_price_CIPF($user_id);
}
return $price;
}

View File

@@ -44,6 +44,8 @@ if (!defined('ABSPATH')) {
*
* [/] order id ('order_id')
*
* [/] card price ('somme_a_regler')
*
*
*/
@@ -52,6 +54,7 @@ if (!defined('ABSPATH')) {
function get_field_init_CIPF($acf_field_name, $acf_id) {
Plgntls::debug_infos();
$acf_state = get_field($acf_field_name, $acf_id);
if ($acf_state !== null) {
return $acf_state;
}
@@ -63,16 +66,20 @@ function get_field_init_CIPF($acf_field_name, $acf_id) {
* - if no default value, update with first value
*
*/
update_field($acf_field_name, 'temp', $acf_id);
update_field($acf_field_name, '', $acf_id);
$acf_object = get_field_object($acf_field_name, $acf_id);
$default = $acf_object['default_value'];
if (empty($default)) {
$choices = $acf_object['choices'];
if (!is_array($choices)) {
return false;
}
$default = reset($choices);
$default = '';
if (isset($acf_object['default_value'])) {
$default = $acf_object['default_value'];
}
else if (isset($acf_object['choices'])) {
$choices = $acf_object['choices'];
if (!empty($choices)) {
$default = reset($choices);
}
}
update_field($acf_field_name, $default, $acf_id);
$acf_state = get_field($acf_field_name, $acf_id);
@@ -543,18 +550,18 @@ function reset_acf_cgv_CIPF($user_id = null) {
/*
* date_suppression_compte
* card price
*
*/
function isset_acf_prof_account_delete_CIPF($user_id = null) {
function get_card_price_CIPF($user_id = null) {
Plgntls::debug_infos();
$acf_prof_delete = Cipf::ACF_PROF_DELETE_ACCOUNT;
$acf_card_price_total = Cipf::ACF_CARD_PRICE_TOTAL;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
return isset_acf_field_CIPF($acf_prof_delete, $acf_id);
return get_acf_field_CIPF($acf_card_price_total, $acf_id);
}

View File

@@ -35,7 +35,9 @@ function add_plugin_content_CIPF() {
* options
*
*/
//delete_option($option_paypal_object['_name']);
$option_paypal = Plgntls::get_option_safe($option_paypal_object, true);
//delete_option($option_payment_object['_name']);
$option_payment = Plgntls::get_option_safe($option_payment_object, true);
//delete_option($option_emails_object['_name']);
$option_emails = Plgntls::get_option_safe($option_emails_object, true);
@@ -96,50 +98,46 @@ function update_payment_messages_option_CIPF($request, $option_name, $option_dat
function update_paypal_credentials_CIPF($request, $option_name, $option_data, $option_default) {
Plgntls::debug_infos();
if (!isset(
$request['sandbox_or_live'],
$request['live_client_id'],
$request['live_client_secret'],
$request['sandbox_client_id'],
$request['sandbox_client_secret'],
)) {
return;
}
/*
* force price 1 cent ?
*
*/
$force_1_cent = false;
if (isset($request['force_1_cent'])) {
$force_1_cent = true;
}
/*
* is sandbox or live ?
*
*/
$is_sandbox = false;
if (!isset($request['sandbox_or_live'])) {
return;
}
if ($request['sandbox_or_live'] === 'sandbox') {
$is_sandbox = true;
}
else if ($request['sandbox_or_live'] === 'live') {
$is_sandbox = false;
}
else {
return;
}
/*
* client id
* ids
*
*/
$client_id = '';
if (!isset($request['client_id'])) {
return;
}
else {
$client_id = $request['client_id'];
}
/*
* client secret
*
*/
$client_secret = '';
if (!isset($request['client_secret'])) {
return;
}
else {
$client_secret = $request['client_secret'];
}
$live_client_id = $request['live_client_id'];
$live_client_secret = $request['live_client_secret'];
$sandbox_client_id = $request['sandbox_client_id'];
$sandbox_client_secret = $request['sandbox_client_secret'];
/*
@@ -148,9 +146,12 @@ function update_paypal_credentials_CIPF($request, $option_name, $option_data, $o
set_paypal_options_CIPF($is_sandbox, $client_id, $client_secret);
*/
$data = array(
'force_1_cent' => $force_1_cent,
'is_sandbox' => $is_sandbox,
'client_id' => $client_id,
'client_secret' => $client_secret,
'live_client_id' => $live_client_id,
'live_client_secret' => $live_client_secret,
'sandbox_client_id' => $sandbox_client_id,
'sandbox_client_secret' => $sandbox_client_secret,
);
Plgntls::update_option_safe($option_name, $data);
}

View File

@@ -92,14 +92,9 @@ function handle_orders_request_CIPF($request_data) {
function create_order_CIPF() {
Plgntls::debug_infos();
$paypal_api_base_url = get_paypal_api_base_url_CIPF();
$acf_card_price_total = Cipf::ACF_CARD_PRICE_TOTAL;
$access_token = generate_access_token_CIPF();
$user_id = get_current_user_id();
$acf_id = 'user_' . $user_id;
$price = get_field($acf_card_price_total['_name'], $acf_id);
// $price = 0.01;
$price = get_paypal_price_CIPF();
$url = $paypal_api_base_url . '/v2/checkout/orders';
$payload = array(