' . $this->get_option('success_url') . '
', self::ID),
],
'description' => [
'title' => __('Description', self::ID),
'type' => 'textarea',
'description' => __('Payment method description, visible by customers on your checkout page', self::ID),
'default' => __('Pay safely using Orange Money, MTN Mobile Money, VISA, MasterCard or Payxaf Wallet', self::ID),
'desc_tip' => true,
],
'public_key' => [
'title' => __('Public key', self::ID),
'type' => 'text',
'description' => __('Copy and paste the public key from your mechant account on Payxaf', self::ID),
'default' => '',
'desc_tip' => true,
],
'secret_key' => [
'title' => __('Secret key', self::ID),
'type' => 'text',
'description' => __('Copy and paste the secret key from your mechant account on Payxaf', self::ID),
'default' => '',
'desc_tip' => true,
],
'payment_methods' => [
'title' => __('Enabled payment methods', self::ID),
'type' => 'select',
'description' => __('This will change operators logos displayed on your checkout page', self::ID),
'default' => "all",
'options' => [
"all" => __("All", self::ID),
"mobile" => "Mobile Money (OM + MoMo)",
"credit_card" => __("Credit Card (VISA + MasterCard)", self::ID),
],
'desc_tip' => true,
],
'order_button_text' => [
'title' => __('Payment button text', self::ID),
'type' => 'text',
'description' => __('Text of the payment button on which customers click to make the payment', self::ID),
'default' => __('Pay with Payxaf', self::ID),
'desc_tip' => true,
],
'currency' => [
'title' => __('Payxaf currency', self::ID),
'type' => 'select',
'description' => __('This is the currency that your customers will see on payment page. If you choose Euro, only card payment will be available on payment page', self::ID),
'default' => "default",
'options' => [
"default" => __("Same as on WooCommerce", self::ID),
"XAF" => "CFA Franc (FCFA)",
"EUR" => __("Euro (€)", self::ID),
"USD" => __("Dollar ($)", self::ID),
],
'desc_tip' => true,
],
'autocomplete_orders' => array(
'title' => __('Autocomplete orders', self::ID),
'label' => __('Autocomplete orders on payment success', self::ID),
'type' => 'checkbox',
'description' => __('If enabled, orders statuses will go directly to complete after successful payment', self::ID),
'default' => 'no',
'desc_tip' => true,
),
]);
}
public function get_public_key(): string
{
return $this->public_key;
}
public function get_secret_key(): string
{
return $this->secret_key;
}
public function get_success_url(): string
{
return $this->success_url;
}
public function get_autocomplete_orders(): bool
{
return $this->autocomplete_orders;
}
/**
* Get order amount and currency for Payxaf
* @throws Exception
*/
private function get_order_amount_currency(WC_Order $order): array
{
$woocommerce_currency = get_woocommerce_currency();
// Throws an exception when currency is not defined in PAYXAF_CURRENCIES
if (!isset(self::SUPPORTED_CURRENCIES[$woocommerce_currency]))
throw new Exception("Currency '$woocommerce_currency' is not currently supported. Please, try using one of the following: " . implode(', ', array_keys(self::SUPPORTED_CURRENCIES)));
$currency = $this->get_option('currency');
if (!in_array($currency, self::CURRENCIES))
$currency = $woocommerce_currency;
$amount = $order->get_total();
if ($currency !== $woocommerce_currency)
$amount *= self::SUPPORTED_CURRENCIES[$woocommerce_currency] / self::SUPPORTED_CURRENCIES[$currency];
return compact('amount', 'currency');
}
/**
* Checks if billing country is CM and billing phone is a valid Mobile Money phone
*/
private function is_order_from_cameroon(WC_Order $order): bool
{
return $order->get_billing_country() === 'CM'
&& preg_match(
'/^((\+|00)?237)?6[5789][0-9]{7}$/',
preg_replace('/[^0-9]/', '', $order->get_billing_phone()) // Ignore non numeric
);
}
/**
* Returns user's locale
*/
private function get_locale(): string
{
return strpos('fr_FR', get_locale()) != false ? 'fr' : 'en';
}
/**
* This function handles the processing of the order, telling WooCommerce
* what status the order should have and where customers go after it’s used.
*/
public function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));
//$order = wc_get_order($order_id);
/* try {
$order_desc = implode(
', ',
array_map(
function (WC_Order_Item $item) {
return $item->get_name();
},
$order->get_items()
)
); */
$amount_currency = $this->get_order_amount_currency($order);
$body = [
"transaction_amount" => $amount_currency['amount'],
"transaction_currency" => $amount_currency['currency'],
"transaction_reason" => substr($order_desc, 0, 255), // Get first 255 chars
"app_transaction_ref" => $order->get_order_key(),
"customer_name" => $order->get_formatted_billing_full_name(),
"customer_email" => $order->get_billing_email(),
"customer_lang" => $this->get_locale()
];
if ($this->is_order_from_cameroon($order))
$body['customer_phone_number'] = preg_replace('/[^0-9]/', '', $order->get_billing_phone()); // Ignore non numeric
$mode = get_test_mode(); //<------------ Change to TEST for test server, PROD for production-->
/* $parameters = [
'identifier' => $identifier,
'currency' => $currency,
'amount' => $amount,
'details' => $details,
'ipn_url' => $ipn_url,
'cancel_url' => $cancel_url,
'success_url' => $success_url,
'payxaf_public_key' => $payxaf_public_key,
'site_logo' => $site_logo,
'checkout_theme' => $checkout_theme,
'customer_name' => $customer_name,
'customer_email' => $customer_email,
];
*/
$url = get_test_mode();
// Get payment link from Payxaf
$result = wp_remote_post($url, array(
/* str_replace('{public_key}', $this->public_key, self::API_URL),
["body" => $body, "sslverify" => false] */ //byme
// Passing variables from form page
$identifier = uniqid(),
$payxaf_public_key = "get_public_key()",
$site_logo= "https://app.payxaf.com/assets/images/logoIcon/logo.png",
$checkout_theme = "get_checkout_theme()",
$currency = $this->get_option('currency'),
$amount = $this->get_option('amount'),
$details = "testing",
$ipn_url = "www.one.com",
$cancel_url = "www.two.com",
$success_url = "www.three.com",
$customer_name = $order->get_formatted_billing_full_name(),
$customer_email = $order->get_billing_email(),
));
if(!is_wp_error($result))
{
$response_body = wp_remote_retrieve_body($result);
$resp_array = json_decode($response_body);
if(isset($resp_array->status))
return $resp_array->status;
if(!isset($resp_array->status) && isset($resp_array->message))
return; //wc_add_notice( $resp_array->message, 'error' ); by me
}
else
wc_add_notice( 'Failed to initiate transaction please try again later', 'error' );
}
/*
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch); */
//$result contains the response back.
//echo $result;
/*$_SESSION['result'] = $result;
$data = json_decode($result, false);
$success = $data->success;
$_SESSION['success'] = $success;
$_SESSION['details'] = $details;
$_SESSION['identifier'] = $identifier;
$_SESSION['currency'] = $currency;
$_SESSION['amount'] = $amount; */
//echo $success;
//echo $details;
// $data = json_decode( $resul, false);
// $success = $data->success;
// header("Location: $data->url");
// echo $result;
// Get raw response
/// $raw_response = wp_remote_retrieve_body($result);
// Parse response
/* $response = json_decode($raw_response, true);
if (!(isset($response["success"]) && $response["success"] === "ok")) {
throw new Exception($response["message"] ?? "An error has occurred. Please try again later");
} else { /*
$order->set_transaction_id($response['message']);
$order->add_order_note('Payxaf payment initiated with reference: ' . $response['message']);
// Clear cart
WC()->cart->empty_cart();
return [
'result' => 'success',
'redirect' => $response['payment_url']
]; */
} /* catch (Exception $ex) {
$order->add_order_note("Payxaf payment init failed with message: " . $ex->getMessage());
wc_add_notice(__('Payment error : ', 'woothemes') . $ex->getMessage(), 'error');
if (isset($result)) {
payxaf_log_data('Request <-----');
payxaf_log_data($result);
}
if (isset($raw_response)) {
payxaf_log_data('Raw response <-----');
payxaf_log_data($raw_response);
}
if (isset($response)) {
payxaf_log_data('Response <-----');
payxaf_log_data($response);
}
return;
} */
}
// }
/* ============================================ INCLUDE OTHER FILE =========================================== */
// including the callback
include 'include/payxaf_callback.php';
// including the order_key in wc admin order list
include 'include/payxaf_hooks.php';
/*=========================================================================================================== */
//}
add_action('plugins_loaded', 'payxaf_gateway_init', 0);