562 lines
22 KiB
PHP
562 lines
22 KiB
PHP
<?php
|
|
/**
|
|
* Classe de passerelle de paiement B-PAY Bankily
|
|
*
|
|
* @package Bankily_BPay
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
// Empêcher l'accès direct
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Classe principale de la passerelle de paiement B-PAY Bankily
|
|
*/
|
|
class WC_Bankily_BPay_Gateway extends WC_Payment_Gateway {
|
|
|
|
public function __construct() {
|
|
$this->id = 'bankily_bpay';
|
|
$this->icon = '';
|
|
$this->has_fields = true;
|
|
$this->method_title = __('B-PAY Bankily', 'bankily-bpay');
|
|
$this->method_description = __('Paiement mobile via B-PAY Bankily avec gestion complète des transactions', 'bankily-bpay');
|
|
|
|
// Charger les paramètres
|
|
$this->init_form_fields();
|
|
$this->init_settings();
|
|
|
|
// Définir les propriétés utilisateur
|
|
$this->title = $this->get_option('title');
|
|
$this->description = $this->get_option('description');
|
|
$this->enabled = $this->get_option('enabled');
|
|
$this->testmode = 'yes' === $this->get_option('testmode');
|
|
$this->username = $this->get_option('username');
|
|
$this->password = $this->get_option('password');
|
|
$this->client_id = $this->get_option('client_id');
|
|
|
|
// URLs de l'API
|
|
$this->api_url = $this->testmode ? 'https://ebankily-tst.appspot.com' : 'https://ebankily.appspot.com';
|
|
|
|
// Actions WordPress
|
|
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
|
|
add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));
|
|
add_action('woocommerce_api_' . strtolower(get_class($this)), array($this, 'webhook'));
|
|
}
|
|
|
|
/**
|
|
* Initialiser les champs du formulaire de configuration
|
|
*/
|
|
public function init_form_fields() {
|
|
$this->form_fields = array(
|
|
'enabled' => array(
|
|
'title' => __('Activer/Désactiver', 'bankily-bpay'),
|
|
'type' => 'checkbox',
|
|
'label' => __('Activer B-PAY Bankily', 'bankily-bpay'),
|
|
'default' => 'yes'
|
|
),
|
|
'title' => array(
|
|
'title' => __('Titre', 'bankily-bpay'),
|
|
'type' => 'text',
|
|
'description' => __('Ceci contrôle le titre que l\'utilisateur voit lors du checkout.', 'bankily-bpay'),
|
|
'default' => __('Paiement Mobile B-PAY', 'bankily-bpay'),
|
|
'desc_tip' => true,
|
|
),
|
|
'description' => array(
|
|
'title' => __('Description', 'bankily-bpay'),
|
|
'type' => 'textarea',
|
|
'description' => __('Description de la méthode de paiement que le client verra sur votre site.', 'bankily-bpay'),
|
|
'default' => __('Payez avec votre mobile via B-PAY Bankily. Vous recevrez un SMS de confirmation.', 'bankily-bpay'),
|
|
),
|
|
'testmode' => array(
|
|
'title' => __('Mode Test', 'bankily-bpay'),
|
|
'type' => 'checkbox',
|
|
'label' => __('Activer le mode test', 'bankily-bpay'),
|
|
'default' => 'yes',
|
|
'description' => __('Placez la passerelle de paiement en mode test en utilisant les serveurs de test.', 'bankily-bpay'),
|
|
),
|
|
'username' => array(
|
|
'title' => __('Nom d\'utilisateur', 'bankily-bpay'),
|
|
'type' => 'text',
|
|
'description' => __('Votre nom d\'utilisateur B-PAY Bankily.', 'bankily-bpay'),
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
),
|
|
'password' => array(
|
|
'title' => __('Mot de passe', 'bankily-bpay'),
|
|
'type' => 'password',
|
|
'description' => __('Votre mot de passe B-PAY Bankily.', 'bankily-bpay'),
|
|
'default' => '',
|
|
'desc_tip' => true,
|
|
),
|
|
'client_id' => array(
|
|
'title' => __('Client ID', 'bankily-bpay'),
|
|
'type' => 'text',
|
|
'description' => __('Votre Client ID B-PAY Bankily.', 'bankily-bpay'),
|
|
'default' => 'e-bankily',
|
|
'desc_tip' => true,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Interface d'administration personnalisée
|
|
*/
|
|
public function admin_options() {
|
|
?>
|
|
<h2><?php echo esc_html($this->get_method_title()); ?></h2>
|
|
<p><?php echo wp_kses_post($this->get_method_description()); ?></p>
|
|
|
|
<?php if ($this->testmode): ?>
|
|
<div class="notice notice-warning">
|
|
<p><strong><?php _e('Mode Test Actif', 'bankily-bpay'); ?></strong> -
|
|
<?php _e('Le plugin utilise l\'environnement de test Bankily.', 'bankily-bpay'); ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<table class="form-table">
|
|
<?php $this->generate_settings_html(); ?>
|
|
</table>
|
|
|
|
<hr>
|
|
|
|
<h3><?php _e('Actions rapides', 'bankily-bpay'); ?></h3>
|
|
<p>
|
|
<a href="<?php echo admin_url('admin.php?page=bankily-transactions'); ?>" class="button button-primary">
|
|
<?php _e('Gérer les transactions B-PAY', 'bankily-bpay'); ?>
|
|
</a>
|
|
<a href="<?php echo admin_url('admin.php?page=bankily-pending'); ?>" class="button button-secondary">
|
|
<?php _e('Transactions en attente', 'bankily-bpay'); ?>
|
|
</a>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Charger les scripts JavaScript pour le frontend
|
|
*/
|
|
public function payment_scripts() {
|
|
if (!is_admin() && !is_cart() && !is_checkout() && !isset($_GET['pay_for_order'])) {
|
|
return;
|
|
}
|
|
|
|
if ('no' === $this->enabled) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_script('bankily_bpay_js', BANKILY_BPAY_PLUGIN_URL . 'assets/bankily-bpay.js', array('jquery'), BANKILY_BPAY_VERSION, true);
|
|
wp_localize_script('bankily_bpay_js', 'bankily_bpay_params', array(
|
|
'ajax_url' => admin_url('admin-ajax.php'),
|
|
'nonce' => wp_create_nonce('bankily_bpay_nonce'),
|
|
'testmode' => $this->testmode ? '1' : '0',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Afficher les champs de paiement sur le checkout
|
|
*/
|
|
public function payment_fields() {
|
|
if ($this->description) {
|
|
echo wpautop(wp_kses_post($this->description));
|
|
}
|
|
|
|
if ($this->testmode) {
|
|
echo '<p class="bankily-test-notice" style="background: #fff3cd; padding: 10px; border-radius: 4px; border-left: 4px solid #ffc107;">';
|
|
echo '<strong>' . __('Mode Test:', 'bankily-bpay') . '</strong> ';
|
|
echo __('Utilisez le numéro 22123456 et le code PIN 1234 pour tester.', 'bankily-bpay');
|
|
echo '</p>';
|
|
}
|
|
?>
|
|
<fieldset id="wc-<?php echo esc_attr($this->id); ?>-form" class="wc-credit-card-form wc-payment-form" style="background:transparent;">
|
|
<div class="form-row form-row-wide">
|
|
<label><?php echo __('Numéro de téléphone', 'bankily-bpay'); ?> <span class="required">*</span></label>
|
|
<input id="bankily_phone" name="bankily_phone" type="tel" autocomplete="tel"
|
|
placeholder="<?php _e('Exemple: 22123456', 'bankily-bpay'); ?>"
|
|
value="<?php echo $this->testmode ? '22123456' : ''; ?>" />
|
|
<small><?php _e('Format: 8 chiffres (sans indicatif pays)', 'bankily-bpay'); ?></small>
|
|
</div>
|
|
<div class="form-row form-row-wide">
|
|
<label><?php echo __('Code de vérification', 'bankily-bpay'); ?> <span class="required">*</span></label>
|
|
<input id="bankily_passcode" name="bankily_passcode" type="password" autocomplete="off"
|
|
placeholder="<?php _e('Code PIN', 'bankily-bpay'); ?>"
|
|
value="<?php echo $this->testmode ? '1234' : ''; ?>" />
|
|
<small><?php _e('Votre code PIN B-PAY (4 à 6 chiffres)', 'bankily-bpay'); ?></small>
|
|
</div>
|
|
<div class="clear"></div>
|
|
</fieldset>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Valider les champs de paiement
|
|
*/
|
|
public function validate_fields() {
|
|
if (empty($_POST['bankily_phone'])) {
|
|
wc_add_notice(__('Le numéro de téléphone est requis!', 'bankily-bpay'), 'error');
|
|
return false;
|
|
}
|
|
|
|
if (empty($_POST['bankily_passcode'])) {
|
|
wc_add_notice(__('Le code de vérification est requis!', 'bankily-bpay'), 'error');
|
|
return false;
|
|
}
|
|
|
|
// Validation du format du numéro de téléphone (Mauritanie)
|
|
$phone = sanitize_text_field($_POST['bankily_phone']);
|
|
if (!preg_match('/^[0-9]{8}$/', $phone)) {
|
|
wc_add_notice(__('Format de numéro de téléphone invalide. Utilisez 8 chiffres.', 'bankily-bpay'), 'error');
|
|
return false;
|
|
}
|
|
|
|
// Validation du code PIN
|
|
$passcode = sanitize_text_field($_POST['bankily_passcode']);
|
|
if (strlen($passcode) < 4 || strlen($passcode) > 6) {
|
|
wc_add_notice(__('Le code PIN doit contenir entre 4 et 6 chiffres.', 'bankily-bpay'), 'error');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Traiter le paiement
|
|
*/
|
|
public function process_payment($order_id) {
|
|
$order = wc_get_order($order_id);
|
|
|
|
// Log du début du processus
|
|
$this->log('Début du processus de paiement pour la commande #' . $order_id);
|
|
|
|
// Obtenir le token d'accès
|
|
$access_token = $this->get_access_token();
|
|
|
|
if (!$access_token) {
|
|
$this->log('Erreur d\'authentification - impossible d\'obtenir le token');
|
|
wc_add_notice(__('Erreur d\'authentification. Veuillez réessayer.', 'bankily-bpay'), 'error');
|
|
return array(
|
|
'result' => 'fail',
|
|
'redirect' => '',
|
|
);
|
|
}
|
|
|
|
// Données de paiement
|
|
$payment_data = array(
|
|
'clientPhone' => sanitize_text_field($_POST['bankily_phone']),
|
|
'passcode' => sanitize_text_field($_POST['bankily_passcode']),
|
|
'operationId' => $order->get_id() . '_' . time() . '_' . wp_rand(1000, 9999),
|
|
'amount' => (string) $order->get_total(),
|
|
'language' => 'FR'
|
|
);
|
|
|
|
$this->log('Données de paiement préparées', $payment_data);
|
|
|
|
// Effectuer le paiement
|
|
$response = $this->make_payment($access_token, $payment_data);
|
|
|
|
// Toujours enregistrer la transaction dans notre base de données
|
|
$this->save_transaction($order_id, $payment_data, $response);
|
|
|
|
if ($response && isset($response['errorCode']) && $response['errorCode'] == '0') {
|
|
// Vérifier si nous avons un ID de transaction (paiement immédiatement confirmé)
|
|
if (isset($response['transactionId']) && !empty($response['transactionId'])) {
|
|
// Paiement réussi immédiatement
|
|
$order->payment_complete($response['transactionId']);
|
|
$order->add_order_note(sprintf(
|
|
__('Paiement B-PAY réussi immédiatement. ID Transaction: %s, ID Opération: %s', 'bankily-bpay'),
|
|
$response['transactionId'],
|
|
$payment_data['operationId']
|
|
));
|
|
|
|
$this->log('Paiement réussi immédiatement', $response);
|
|
|
|
// Vider le panier
|
|
WC()->cart->empty_cart();
|
|
|
|
return array(
|
|
'result' => 'success',
|
|
'redirect' => $this->get_return_url($order),
|
|
);
|
|
} else {
|
|
// Paiement initié mais en attente de confirmation (statut TA)
|
|
$order->update_status('on-hold', __('Paiement B-PAY en cours de vérification. Le client doit confirmer sur son mobile.', 'bankily-bpay'));
|
|
$order->add_order_note(sprintf(
|
|
__('Paiement B-PAY initié et en attente de confirmation. ID Opération: %s. Le système vérifiera automatiquement le statut.', 'bankily-bpay'),
|
|
$payment_data['operationId']
|
|
));
|
|
|
|
$this->log('Paiement en attente de confirmation', $response);
|
|
|
|
// Vider le panier
|
|
WC()->cart->empty_cart();
|
|
|
|
// Afficher un message informatif au client
|
|
wc_add_notice(sprintf(
|
|
__('Votre paiement de %s MRU est en cours de traitement. Confirmez la transaction sur votre mobile B-PAY. Vous recevrez une notification par SMS.', 'bankily-bpay'),
|
|
number_format($order->get_total(), 0, ',', ' ')
|
|
), 'notice');
|
|
|
|
return array(
|
|
'result' => 'success',
|
|
'redirect' => $this->get_return_url($order),
|
|
);
|
|
}
|
|
} else {
|
|
// Paiement échoué
|
|
$error_code = isset($response['errorCode']) ? $response['errorCode'] : 'unknown';
|
|
$error_message = isset($response['errorMessage']) ? $response['errorMessage'] : __('Erreur de paiement inconnue', 'bankily-bpay');
|
|
|
|
$this->log('Paiement échoué', array('error_code' => $error_code, 'error_message' => $error_message));
|
|
|
|
// Messages d'erreur personnalisés selon le code
|
|
switch ($error_code) {
|
|
case '2':
|
|
$user_message = __('Erreur d\'authentification. Veuillez vérifier vos identifiants et réessayer.', 'bankily-bpay');
|
|
break;
|
|
case '4':
|
|
$user_message = __('Erreur technique temporaire. Veuillez réessayer dans quelques instants.', 'bankily-bpay');
|
|
break;
|
|
default:
|
|
$user_message = $error_message;
|
|
}
|
|
|
|
wc_add_notice($user_message, 'error');
|
|
$order->add_order_note(sprintf(
|
|
__('Paiement B-PAY échoué - Code: %s, Message: %s, ID Opération: %s', 'bankily-bpay'),
|
|
$error_code,
|
|
$error_message,
|
|
$payment_data['operationId']
|
|
));
|
|
|
|
return array(
|
|
'result' => 'fail',
|
|
'redirect' => '',
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enregistrer la transaction dans la base de données
|
|
*/
|
|
private function save_transaction($order_id, $payment_data, $response) {
|
|
global $wpdb;
|
|
|
|
$table_name = $wpdb->prefix . 'bankily_transactions';
|
|
|
|
// Déterminer le statut initial
|
|
$status = 'TA'; // Par défaut en attente
|
|
if ($response && isset($response['errorCode'])) {
|
|
if ($response['errorCode'] == '0') {
|
|
// Si nous avons un ID de transaction, c'est confirmé immédiatement
|
|
if (isset($response['transactionId']) && !empty($response['transactionId'])) {
|
|
$status = 'TS';
|
|
}
|
|
// Sinon reste en 'TA' (en attente)
|
|
} else {
|
|
$status = 'TF'; // Échec confirmé
|
|
}
|
|
}
|
|
|
|
$data = array(
|
|
'order_id' => $order_id,
|
|
'operation_id' => $payment_data['operationId'],
|
|
'transaction_id' => isset($response['transactionId']) ? $response['transactionId'] : '',
|
|
'client_phone' => $payment_data['clientPhone'],
|
|
'amount' => floatval($payment_data['amount']),
|
|
'status' => $status,
|
|
'error_code' => isset($response['errorCode']) ? $response['errorCode'] : '',
|
|
'error_message' => isset($response['errorMessage']) ? $response['errorMessage'] : '',
|
|
'created_at' => current_time('mysql'),
|
|
'last_checked' => current_time('mysql'),
|
|
'check_count' => 0
|
|
);
|
|
|
|
$result = $wpdb->insert($table_name, $data);
|
|
|
|
if ($result === false) {
|
|
$this->log('Erreur lors de l\'enregistrement de la transaction en base de données', $wpdb->last_error);
|
|
} else {
|
|
$this->log('Transaction enregistrée avec succès en base de données', $data);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Obtenir un token d'accès pour l'API
|
|
*/
|
|
private function get_access_token() {
|
|
// Vérifier si nous avons un token valide en cache
|
|
$cached_token = get_transient('bankily_bpay_access_token');
|
|
if ($cached_token) {
|
|
$this->log('Token d\'accès récupéré depuis le cache');
|
|
return $cached_token;
|
|
}
|
|
|
|
$url = $this->api_url . '/authentification';
|
|
|
|
$body = array(
|
|
'grant_type' => 'password',
|
|
'username' => $this->username,
|
|
'password' => $this->password,
|
|
'client_id' => $this->client_id
|
|
);
|
|
|
|
$args = array(
|
|
'body' => $body,
|
|
'headers' => array(
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
),
|
|
'method' => 'POST',
|
|
'timeout' => 45,
|
|
);
|
|
|
|
$this->log('Tentative d\'authentification API', array('url' => $url, 'username' => $this->username));
|
|
|
|
$response = wp_remote_post($url, $args);
|
|
|
|
if (is_wp_error($response)) {
|
|
$this->log('Erreur de connexion lors de l\'authentification', $response->get_error_message());
|
|
return false;
|
|
}
|
|
|
|
$http_code = wp_remote_retrieve_response_code($response);
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
$this->log('Réponse d\'authentification', array('http_code' => $http_code, 'response' => $data));
|
|
|
|
if ($http_code === 200 && isset($data['access_token'])) {
|
|
// Mettre en cache le token (durée d'expiration - 60 secondes de marge de sécurité)
|
|
$expires_in = isset($data['expires_in']) ? intval($data['expires_in']) - 60 : 3540;
|
|
set_transient('bankily_bpay_access_token', $data['access_token'], $expires_in);
|
|
|
|
// Stocker aussi le refresh token si présent
|
|
if (isset($data['refresh_token'])) {
|
|
$refresh_expires = isset($data['refresh_expires_in']) ? intval($data['refresh_expires_in']) - 60 : 86340;
|
|
set_transient('bankily_bpay_refresh_token', $data['refresh_token'], $refresh_expires);
|
|
}
|
|
|
|
$this->log('Token d\'accès obtenu et mis en cache avec succès');
|
|
return $data['access_token'];
|
|
}
|
|
|
|
$this->log('Erreur lors de l\'obtention du token d\'accès', $body);
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Effectuer un paiement via l'API
|
|
*/
|
|
private function make_payment($access_token, $payment_data) {
|
|
$url = $this->api_url . '/payment';
|
|
|
|
$args = array(
|
|
'body' => json_encode($payment_data),
|
|
'headers' => array(
|
|
'Content-Type' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $access_token
|
|
),
|
|
'method' => 'POST',
|
|
'timeout' => 60, // Timeout plus long pour les paiements
|
|
);
|
|
|
|
$this->log('Tentative de paiement API', array('url' => $url, 'data' => $payment_data));
|
|
|
|
$response = wp_remote_post($url, $args);
|
|
|
|
if (is_wp_error($response)) {
|
|
$this->log('Erreur de connexion lors du paiement', $response->get_error_message());
|
|
return false;
|
|
}
|
|
|
|
$http_code = wp_remote_retrieve_response_code($response);
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
$this->log('Réponse de paiement', array('http_code' => $http_code, 'response' => $data));
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Vérifier le statut d'une transaction
|
|
*/
|
|
public function check_transaction($operation_id) {
|
|
$access_token = $this->get_access_token();
|
|
|
|
if (!$access_token) {
|
|
$this->log('Impossible de vérifier la transaction - pas de token d\'accès', $operation_id);
|
|
return false;
|
|
}
|
|
|
|
$url = $this->api_url . '/checkTransaction';
|
|
|
|
$body = array(
|
|
'operationID' => $operation_id
|
|
);
|
|
|
|
$args = array(
|
|
'body' => json_encode($body),
|
|
'headers' => array(
|
|
'Content-Type' => 'application/json',
|
|
'Authorization' => 'Bearer ' . $access_token
|
|
),
|
|
'method' => 'POST',
|
|
'timeout' => 45,
|
|
);
|
|
|
|
$this->log('Vérification du statut de la transaction', array('url' => $url, 'operation_id' => $operation_id));
|
|
|
|
$response = wp_remote_post($url, $args);
|
|
|
|
if (is_wp_error($response)) {
|
|
$this->log('Erreur de connexion lors de la vérification', $response->get_error_message());
|
|
return false;
|
|
}
|
|
|
|
$http_code = wp_remote_retrieve_response_code($response);
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
$this->log('Réponse de vérification', array('http_code' => $http_code, 'response' => $data));
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Logger les messages (actif en mode test et debug)
|
|
*/
|
|
private function log($message, $data = null) {
|
|
if ($this->testmode || (defined('WP_DEBUG') && WP_DEBUG)) {
|
|
$log_entry = '[B-PAY] ' . $message;
|
|
if ($data) {
|
|
$log_entry .= ' | Data: ' . json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if (class_exists('WC_Logger')) {
|
|
$logger = new WC_Logger();
|
|
$logger->add('bankily-bpay', $log_entry);
|
|
}
|
|
|
|
// Aussi dans les logs WordPress si le debug est activé
|
|
if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
|
|
error_log($log_entry);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gestion des webhooks (pour usage futur)
|
|
*/
|
|
public function webhook() {
|
|
$raw_body = file_get_contents('php://input');
|
|
$this->log('Webhook reçu', $raw_body);
|
|
|
|
http_response_code(200);
|
|
echo "OK";
|
|
exit;
|
|
}
|
|
}
|
|
|
|
?>
|