Last active
April 23, 2020 15:47
-
-
Save Xsmael/3bc2547dcea9068609ec1b49fe863605 to your computer and use it in GitHub Desktop.
woocommerce plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('ABSPATH') or exit; | |
// Make sure WooCommerce is active | |
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { | |
return; | |
} | |
add_filter('woocommerce_payment_gateways', 'mobipay_add_gateway'); | |
function mobipay_add_gateway($gateways) | |
{ | |
$gateways[] = 'WC_mobipay_Gateway'; | |
return $gateways; | |
} | |
function wc_mobipay_gateway_plugin_links($links) | |
{ | |
$plugin_links = array( | |
'<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=mobipay_gateway') . '">' . __('Configure', 'wc-mobipay-gateway') . '</a>' | |
); | |
return array_merge($plugin_links, $links); | |
} | |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'wc_mobipay_gateway_plugin_links'); | |
/** | |
* Offline Payment Gateway | |
* | |
* We load it later to ensure WC is loaded first since we're extending it. | |
* | |
* @class WC_mobipay_Gateway | |
* @extends WC_Payment_Gateway | |
* @version 1.0.0 | |
* @package WooCommerce/Classes/Payment | |
* @author UC | |
*/ | |
add_action('plugins_loaded', 'mobipay_init_gateway_class', 11); | |
function mobipay_init_gateway_class() | |
{ | |
class WC_mobipay_Gateway extends WC_Payment_Gateway | |
{ | |
/** | |
* Constructor for the gateway. | |
*/ | |
public function __construct() | |
{ | |
$this->id = 'mobipay_gateway'; | |
$this->icon = apply_filters('woocommerce_mobipay_icon', plugins_url('img/upay_badge.png', __FILE__)); | |
$this->has_fields = false; | |
$this->method_title = __('MobiPay', 'wc-mobipay-gateway'); | |
/*$rootUrl = "http://150.206.104.9/upay_portal/template/index.php";*/ | |
$this->method_description = __('Payer en ligne avec Orange money et Mobicash en toute simplicité.', 'wc-mobipay-gateway'); | |
// Load the settings. | |
$this->init_form_fields(); | |
$this->init_settings(); | |
$this->api_key = $this->settings['api_key']; | |
$this->sms = $this->settings['sms']; | |
$this->sms_url = $this->settings['sms_url']; | |
$this->sms_message = $this->settings['sms_message']; | |
/*$this->title = $this->settings['title']; | |
$this->description = $this->settings['description']; | |
$this->api_key = $this->settings['api_key']; | |
$this->order_button_text = __( 'Payer avec Mobipay', 'woocommerce' ); | |
$rootUrl = "http://150.206.104.9/upay_portal/template/index.php"; | |
$this->medthod_title = 'Mobipay'; | |
$rootUrl = "http://150.206.104.9/upay_portal/template/index.php"; | |
if ($this->settings['api_key']) { | |
$this->posturl = $rootUrl.'/pay/v01/redirect/checkout-invoice/create'; | |
$this->geturl = $rootUrl.'/pay/v01/redirect/checkout-invoice/confirm/'; | |
} | |
*/ | |
// Define user set variables | |
$this->title = $this->get_option('title'); | |
$this->description = $this->get_option('description'); | |
$this->instructions = $this->get_option('instructions', $this->description); | |
add_action('check_upay', array($this, 'check_response')); | |
add_action( 'init', 'check_for_woopaypal' ); | |
add_action('woocommerce_checkout_order_review', array($this, 'check_response')); | |
// Actions | |
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); | |
add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page')); | |
// Customer Emails | |
add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 3); | |
} | |
/** | |
* Initialize Gateway Settings Form Fields | |
*/ | |
public function init_form_fields() | |
{ | |
$this->form_fields = apply_filters('wc_mobipay_form_fields', array( | |
'enabled' => array( | |
'title' => __('Activer/Désactiver', 'wc-mobipay-gateway'), | |
'type' => 'checkbox', | |
'label' => __('Activer Mobipay', 'wc-mobipay-gateway'), | |
'default' => 'yes' | |
), | |
'title' => array( | |
'title' => __('Title', 'wc-mobipay-gateway'), | |
'type' => 'text', | |
'description' => __('Le titre que les clients verront lors du paiement.', 'wc-mobipay-gateway'), | |
'default' => __('Mobile Payment', 'wc-mobipay-gateway'), | |
'desc_tip' => true, | |
), | |
'description' => array( | |
'title' => __('Description', 'wc-mobipay-gateway'), | |
'type' => 'textarea', | |
'description' => __('Description de la méthode de paiement.', 'wc-mobipay-gateway'), | |
'default' => __('Paiement mobile Orange money, Mobicash via notre plateforme de paiement U-Pay.', 'wc-mobipay-gateway'), | |
'desc_tip' => true, | |
), | |
'instructions' => array( | |
'title' => __('Instructions', 'wc-mobipay-gateway'), | |
'type' => 'textarea', | |
'description' => __('Instructions qui seront ajoutées sur la page de remerciement et au niveau des emails.', 'wc-mobipay-gateway'), | |
'default' => '', | |
'desc_tip' => true, | |
), | |
'api_key' => array( | |
'title' => __('Clé API', 'wc-mobipay-gateway'), | |
'description' => __('Clé principale générée par U-PAY.', 'wc-mobipay-gateway'), | |
'id' => 'live_publishable_key', | |
'type' => 'text', | |
), | |
'upay_service_code' => array( | |
'title' => __('Code Service', 'wc-mobipay-gateway'), | |
'description' => __('Code service attribué.', 'wc-mobipay-gateway'), | |
'type' => 'text', | |
'default' => '100', | |
), | |
'sms' => array( | |
'title' => __('Notification SMS', 'wc-mobipay-gateway'), | |
'type' => 'checkbox', | |
'default' => 'no', | |
'description' => __("Activer les notification par SMS lorsque le paiement sur U-PAY s'est bien effectué.", 'wc-mobipay-gateway') | |
), | |
'sms_url' => array( | |
'title' => __("SMS URL API REST"), | |
'type' => 'text', | |
//'description' => __('Utilisez {NUMBER} pour indiquer le numéro du client et {MESSAGE} pour le message.') | |
), | |
/* 'sms_message' => array( | |
'title' => __('Contenu du SMS envoyé en cas de succès de paiement'), | |
'type' => 'textarea', | |
'description' => __("Utilisez {ORDER-ID} pour indiquer l'identifiant de commande, {AMOUNT} pour le montant et {CUSTOMER} pour le nom du client."))*/ | |
)); | |
} | |
function sendsms($number, $message) | |
{ | |
$url = $this->sms_url; | |
$url = str_replace("{NUMBER}", urlencode($number), $url); | |
$url = str_replace("{MESSAGE}", urlencode($message), $url); | |
$url = str_replace("amp;", "&", $url); | |
if (trim($url) <> "") { | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url | |
)); | |
curl_exec($curl); | |
curl_close($curl); | |
} | |
} | |
public function get_cart() | |
{ | |
if (!did_action('wp_loaded')) { | |
wc_doing_it_wrong(__FUNCTION__, __('Get cart should not be called before the wp_loaded action.', 'woocommerce'), '2.3'); | |
} | |
if (!did_action('woocommerce_load_cart_from_session')) { | |
$this->session->get_cart_from_session(); | |
} | |
return array_filter($this->get_cart_contents()); | |
} | |
protected function get_mobipay_args($order) | |
{ | |
global $woocommerce; | |
//$order = new WC_Order($order_id); | |
$txnid = $order->id . '_' . date("ymds"); | |
//$redirect_url = $woocommerce->cart->get_checkout_url(); | |
//$redirect_url = $woocommerce->wc_get_payment_gateway_by_order(); | |
$productinfo = "Commande: " . $order->id; | |
$str = "$this->merchant_id|$txnid|$order->order_total|$productinfo|$order->billing_first_name|$order->billing_email|||||||||||$this->salt"; | |
// $hash = hash('sha512', $str); | |
WC()->session->set('mobipay_wc_hash_key', $str); | |
$shipping = 0; | |
if (0 < $woocommerce->cart->get_shipping_total()) { | |
if ('excl' === $woocommerce->cart->tax_display_cart) { | |
$shipping = $woocommerce->cart->shipping_total; | |
} else { | |
$shipping = $woocommerce->cart->shipping_total + $woocommerce->cart->shipping_tax_total; | |
} | |
} | |
$items = $woocommerce->cart->get_cart(); | |
$mobipay_items = array(); | |
//foreach ( WC()->cart->get_cart() as $item ){ | |
foreach ($items as $item) { | |
$mobipay_items[] = array( | |
"name" => $item["data"]->post->post_title, | |
"quantity" => $item["quantity"], | |
"unit_price" => $item["line_total"] / (($item["quantity"] == 0) ? 1 : $item["quantity"]), | |
"total_price" => $item["line_total"] | |
); | |
} | |
if ($shipping > 0) { | |
$mobipay_items[] = array( | |
"name" => "Frais de livraison", | |
"quantity" => 1, | |
"unit_price" => $shipping, | |
"total_price" => $shipping | |
); | |
} | |
$baseUrl = $this->get_return_url($order); | |
if (strpos($baseUrl, '?') !== false) { | |
$baseUrl .= '&'; | |
} else { | |
$baseUrl .= '?'; | |
} | |
$redirect_success= $baseUrl . 'woopaypal=true&order_id=' . $order->id; | |
$redirect_cancel= $baseUrl . 'woopaypal=cancel&order_id=' . $order->id; | |
$mobipay_args = array( | |
"invoice" => array( | |
//"items" =>$woocommerce->cart->cart_contents_count, | |
"items" => $mobipay_items, | |
"essai" => WC()->cart->get_cart_url(), | |
"total_amount" => $order->order_total, | |
"description" => "Paiement de " . $order->order_total . " FCFA pour article(s) achetés sur " . get_bloginfo("name") | |
), "store" => array( | |
"name" => get_bloginfo("name"), | |
"website_url" => get_site_url() | |
), "actions" => array( | |
"cancel_url" => $redirect_cancel, | |
"return_url" => $redirect_success | |
), "custom_data" => array( | |
"order_id" => $order->id, | |
"trans_id" => $txnid, | |
"hash" => $str | |
), | |
"plugin_type" => "wc_data", | |
"service_code" => "100", | |
"returnURL" => $redirect_success //WHATCH OUT! | |
); | |
// "Shipping" => $woocommerce->cart->get_cart_shipping_total() | |
apply_filters('woocommerce_mobipay_args', $mobipay_args, $order); | |
return $mobipay_args; | |
} | |
function post_to_url($url, $data, $order_id) | |
{ | |
$commande = json_encode($data); | |
$ch = curl_init(); | |
$api_key = $this->api_key; | |
$caller = 1; | |
$description = "Mobipay"; | |
curl_setopt_array($ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_NOBODY => false, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => $commande, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_HTTPHEADER => array( | |
"CONTENT-Type:application/json" | |
), | |
"Apikey: $api_key", | |
"Apicaller: $caller", | |
)); | |
$response = curl_exec($ch); | |
$response_decoded = json_decode($response); | |
WC()->session->set('mobipay_wc_oder_id', $order_id); | |
$url = $response_decoded->response_code; | |
return $url; | |
} | |
public function payment_scripts() | |
{ | |
} | |
/* | |
* Fields validation, more in Step 5 | |
*/ | |
public function validate_fields() | |
{ | |
} | |
/** | |
* Process the payment and return the result | |
* | |
* @param int $order_id | |
* @return array | |
*/ | |
public function process_payment($order_id) | |
{ | |
$order = wc_get_order($order_id); | |
$order = new WC_Order($order_id); | |
// Mark as on-hold (we're awaiting the payment) | |
$order->update_status('on-hold', __('Paiement en attente', 'wc-mobipay-gateway')); | |
//$order->payment_complete( $order_id ); | |
$order->add_order_note(sprintf(__('%s : Paiement via U-PAY! Transaction ID: %s', 'woocommerce'), $this->title, $order_id)); | |
// Reduce stock levels | |
$order->reduce_order_stock(); | |
//WC()->cart->empty_cart(); | |
$redirect_url= $this->post_to_url("http://150.206.104.9:3005/v1", $this->get_mobipay_args($order), $order_id); | |
if(isset($redirect_url) && $redirect_url != "") | |
return array( | |
'result' => 'success', | |
'redirect' => $redirect_url | |
); | |
return array( | |
'result' => 'failure', | |
'redirect' => '' | |
); | |
} | |
function showMessage($content) | |
{ | |
return '<div class="box ' . $this->msg['class'] . '-box">' . $this->msg['message'] . '</div>' . $content; | |
} | |
function check_for_woopaypal() { | |
if( isset($_GET['woopaypal'])) { | |
// Start the gateways | |
WC()->payment_gateways(); | |
check_upay(); | |
do_action( 'check_upay' ); | |
} | |
} | |
public function check_response() { | |
global $woocommerce; | |
$order->add_order_note( sprintf( __( '%s Réponse de Paiement en cours de traitement! ID de transaction: %s', 'woocommerce' ), $this->title, $paymentId ) ); | |
if( isset( $_GET['woopaypal'] ) ) { | |
$woopaypal = $_GET['woopaypal']; | |
$order_id = $_GET['order_id']; | |
if( $order_id == 0 || $order_id == '' ) { | |
return; | |
} | |
$order = new WC_Order( $order_id ); | |
if( $order->has_status('completed') || $order->has_status('processing')) { | |
return; | |
} | |
if( $woopaypal == 'true' ) { | |
$this->get_api_context(); | |
$paymentId = $_GET['paymentId']; | |
if(0) { // if payment failed | |
$errormsg= "Blabalbalbalblabl"; | |
wc_add_notice( $errormsg, 'error' ); | |
$order->update_status('failed', sprintf( __( '%s Paiement Ehcoué! ID de transaction: %d', 'woocommerce' ), $this->title, $paymentId ) . ' ' . $ex->getMessage() ); | |
return; | |
} | |
else { | |
// Payment complete | |
$order->payment_complete(); | |
// Add order note | |
$order->add_order_note( sprintf( __( '%s Paiement Accepté! ID de transaction: %s', 'woocommerce' ), $this->title, $paymentId ) ); | |
// Remove cart | |
$woocommerce->cart->empty_cart(); | |
} | |
} | |
if( $woopaypal == 'cancel' ) { | |
$order = new WC_Order( $order_id ); | |
$order->update_status('cancelled', sprintf( __( '%s Paiement annulé ! ID de transaction: %d', 'woocommerce' ), $this->title, $paymentId ) ); | |
} | |
} | |
return; | |
} | |
function mobipay_reponse($api) | |
{ | |
global $woocommerce; | |
if ($api <> "") { | |
$wc_order_id = WC()->session->get('mobipay_wc_oder_id'); | |
$hash = WC()->session->get('mobipay_wc_hash_key'); | |
$order = new WC_Order($wc_order_id); | |
try { | |
$ch = curl_init(); | |
$api_key = $this->api_key; | |
$caller = 1; | |
$url = $this->geturl . "?invoiceToken=" . $api; | |
curl_setopt_array($ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_NOBODY => false, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_HTTPHEADER => array( | |
"Apikey: $api_key", | |
"Apicaller: $caller", | |
), | |
)); | |
$response = curl_exec($ch); | |
$response_decoded = json_decode($response); | |
$respond_code = $response_decoded->response_code; | |
if ($respond_code == "00") { | |
$description = ""; | |
//payment found | |
$status = $response_decoded->status; | |
$custom_data = $response_decoded->custom_data; | |
$order_id = $custom_data->order_id; | |
if ($wc_order_id <> $order_id) { | |
$message = "Votre session de transaction a expiré. Votre numéro de commande est: $order_id"; | |
$message_type = "notice"; | |
$order->add_order_note($message); | |
$redirect_url = $order->get_cancel_order_url(); | |
} | |
if ($status == "completed") { | |
//payment was completely processed | |
$total_amount = strip_tags($woocommerce->cart->get_cart_total()); | |
$message = "Merci pour votre achat. La transaction a été un succès, le paiement a été reçu. Votre commande est en cours de traitement. Votre numéro de commande est $order_id"; | |
$message_type = "success"; | |
$order->payment_complete(); | |
$order->update_status('completed'); | |
$order->add_order_note('Paiement ' . $description . ' effectué avec succès<br/>ID unique reçu de ' . $description . ': ' . $api); | |
$order->add_order_note($this->msg['message']); | |
$woocommerce->cart->empty_cart(); | |
$redirect_url = $this->get_return_url($order); | |
$customer = trim($order->billing_last_name . " " . $order->billing_first_name); | |
if ($this->sms == "yes") { | |
$phone_no = get_user_meta(get_current_user_id(), 'billing_phone', true); | |
$sms = $this->sms_message; | |
$sms = str_replace("{ORDER-ID}", $order_id, $sms); | |
$sms = str_replace("{AMOUNT}", $total_amount, $sms); | |
$sms = str_replace("{CUSTOMER}", $customer, $sms); | |
$this->sendsms($phone_no, $sms); | |
} | |
} else { | |
//payment is still pending, or user cancelled request | |
$message = "La transaction n'a pu être complétée."; | |
$message_type = "error"; | |
$order->add_order_note("La transaction a échoué ou l'utilisateur a eu à faire demande d'annulation de paiement"); | |
$redirect_url = $order->get_cancel_order_url(); | |
} | |
} else { | |
//payment not found | |
$message = "Merci de nous avoir choisi. Malheureusement, la transaction a été refusée."; | |
$message_type = "error"; | |
$redirect_url = $order->get_cancel_order_url(); | |
} | |
$notification_message = array( | |
'message' => $message, | |
'message_type' => $message_type | |
); | |
if (version_compare(WOOCOMMERCE_VERSION, "2.2") >= 0) { | |
add_post_meta($wc_order_id, '_mobipay_hash', $hash, true); | |
} | |
update_post_meta($wc_order_id, '_mobipay_wc_message', $notification_message); | |
WC()->session->__unset('mobipay_wc_hash_key'); | |
WC()->session->__unset('mobipay_wc_order_id'); | |
wp_redirect($redirect_url); | |
exit; | |
} catch (Exception $e) { | |
$order->add_order_note('Erreur: ' . $e->getMessage()); | |
$redirect_url = $order->get_cancel_order_url(); | |
wp_redirect($redirect_url); | |
exit; | |
} | |
} | |
} | |
/* | |
* In case you need a webhook, like PayPal IPN etc | |
*/ | |
public function webhook() | |
{ | |
} | |
/** | |
* Add content to the WC emails. | |
* | |
* @access public | |
* @param WC_Order $order | |
* @param bool $sent_to_admin | |
* @param bool $plain_text | |
*/ | |
public function email_instructions($order, $sent_to_admin, $plain_text = false) | |
{ | |
if ($this->instructions && !$sent_to_admin && $this->id === $order->payment_method && $order->has_status('on-hold')) { | |
echo wpautop(wptexturize($this->instructions)) . PHP_EOL; | |
} | |
} | |
/** | |
* Output for the order received page. | |
*/ | |
public function thankyou_page() | |
{ | |
if ($this->instructions) { | |
echo wpautop(wptexturize($this->instructions)); | |
} | |
} | |
} // end \WC_mobipay_Gateway class | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment