Commit 86e7bb34 authored by mr.amirka's avatar mr.amirka

refactored

parent 0cf28e60
......@@ -6,9 +6,10 @@
* Version: 1.0
* Author: lunu.io
* Author URI: https://lunu.io/plugins/
* Text Domain: lunu-payment
* Text Domain: lunu-pay
*/
DEFINE('LUNUPAYMENT_SERVER_NAME', $_SERVER['SERVER_NAME']);
DEFINE('LUNUPAYMENT_WIDGET_VERSION_SANDBOX', 'rc');
DEFINE('LUNUPAYMENT_WIDGET_PROCESSING_SANDBOX', 'rc');
......@@ -31,10 +32,11 @@ DEFINE('LUNUPAYMENT_PROCESSING_VERSION', (
)
));
DEFINE('LUNUPAYMENT_CALLBACK_ENDPOINT', 'https://' . LUNUPAYMENT_SERVER_NAME . '/wp-json/lunu/payment/v1/notify');
DEFINE('LUNUPAYMENT_STATUS_AWAITING_CONFIRMATION', 'awaiting_payment_confirmation');
DEFINE('LUNUPAYMENT_WC_STATUS_AWAITING_CONFIRMATION', 'wc-lunu-awaiting');
if (!defined('ABSPATH')) exit; // Exit if accessed directly
if (
!function_exists('lunupayment_wc_gateway_load')
......@@ -50,6 +52,45 @@ if (
add_filter('plugin_row_meta', 'lunupayment_wc_plugin_meta', 10, 2);
}
function lunupayment_wc_new_order_statuses() {
register_post_status(LUNUPAYMENT_WC_STATUS_AWAITING_CONFIRMATION, array(
'label' => 'Awaiting payment confirmation',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Awaiting payment confirmation', 'Awaiting payment confirmation', 'woocommerce')
));
}
function lunupayment_wc_order_statuses($order_statuses) {
$order_statuses = array_merge($order_statuses, array());
$order_statuses[LUNUPAYMENT_WC_STATUS_AWAITING_CONFIRMATION] = 'Awaiting payment confirmation';
return $order_statuses;
}
// New order status AFTER woo 2.2
add_action('init', 'lunupayment_wc_new_order_statuses');
add_filter('wc_order_statuses', 'lunupayment_wc_order_statuses');
function lunu_payment_awaiting($order) {
$order_id = $order->get_id();
$order->add_order_note('Payment awaiting blockchain confirmation via Lunu service<br>');
do_action('woocommerce_pre_payment_complete', $order_id);
$session = WC()->session;
if ($session) {
$session->set('order_awaiting_payment', false);
}
$order->set_date_paid(time());
$order->set_status('wc-lunu-awaiting');
$order->save();
do_action('woocommerce_payment_complete', $order_id);
}
function getUrlEndpoint($version) {
return 'https://' . $version . '.lunu.io/api/v1/payments/';
}
......@@ -289,7 +330,7 @@ if (
// WC Order Checkout Page
public function cryptocoin_payment($order_id) {
$order = new WC_Order($order_id);
$order = new WC_Order($order_id);
$order_id = (true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')) ? $order->id : $order->get_id();
$order_status = (true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')) ? $order->status : $order->get_status();
......@@ -300,7 +341,6 @@ if (
$success_url = trim($this->success_url);
$cancel_url = trim($this->cancel_url);
if ($order_status == "cancelled" || $post_status == "wc-cancelled") {
if (!empty($cancel_url)) {
echo "<script>window.location.href = '" . $cancel_url . "';</script>";
......@@ -311,12 +351,12 @@ if (
}
if (true === version_compare(WOOCOMMERCE_VERSION, '2.1', '<')) {
echo '<br><h2>' . __('Information', LUNUPAYMENTWC) . '</h2>' . PHP_EOL;
echo "<div class='woocommerce-error'>"
echo "<div class='woocommerce-error'>"
.sprintf(__(
'Sorry, but there was an error processing your order. Please try a different payment method or contact us if you need assistance (Lunu Cryptocurrencies Gateway Plugin v0.0.0+ not configured / %s not activated).',
LUNUPAYMENTWC), $this->title)
."</div><br>";
return true;
return true;
}
if ($amount <= 0) {
echo '<br><h2>' . __('Information', LUNUPAYMENTWC) . '</h2>' . PHP_EOL;
......@@ -324,6 +364,11 @@ if (
return true;
}
// lunu-awaiting
if (!($order_status === "pending" || $post_status == "wc-pending")) {
return true;
}
$app_id = $this->app_id;
$api_secret = $this->api_secret;
......@@ -456,10 +501,19 @@ if (
// Lunu Cryptocurrencies Gateway - Instant Payment Notification
public function lunupayment_callback($payment) {
$payment_status = strtolower($payment['status']);
/*
global $_GET;
if (intval($_GET['test']) == 1) {
$order = new WC_Order(6529);
if ($order === false) return false;
lunu_payment_awaiting($order);
return true;
}
*/
$payment_id = $payment['id'];
if (!$payment_id || $payment_status !== 'paid') return false;
if (!$payment_id) return false;
$posts = get_posts(array(
'meta_key' => '_lunupayment_id',
......@@ -474,6 +528,11 @@ if (
$order = new WC_Order($order_id);
if ($order === false) return false;
$order_status = $order->status;
if ($order_status !== 'pending' && $order_status !== 'awaiting_payment_confirmation') {
return false;
}
$payment = lunu_payment_check(array(
'app_id' => $this->app_id,
......@@ -483,27 +542,38 @@ if (
: LUNUPAYMENT_PROCESSING_VERSION
), $payment_id);
$currency = true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')
? $order->order_currency
: $order->get_currency();
$amount = true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')
? $order->order_total
: $order->get_total();
$order_currency = strtoupper(
true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')
? $order->order_currency
: $order->get_currency()
);
$order_amount = floatval(
true === version_compare(WOOCOMMERCE_VERSION, '3.0', '<')
? $order->order_total
: $order->get_total()
);
$payment_status = strtolower($payment['status']);
if (
floatval($payment['amount']) === floatval($amount)
&& strtoupper($payment['currency']) === strtoupper($currency)
&& strtolower($payment['status']) === $payment_status
floatval($payment['amount']) !== $order_amount
|| strtoupper($payment['currency']) !== $order_currency
) {
update_post_meta($order_id, '_lunupayment_status', $payment_status);
return false;
}
$order->add_order_note('Payment Received via Lunu service<br>');
if ($payment_status === 'paid') {
update_post_meta($order_id, '_lunupayment_status', $payment_status);
$order->add_order_note('Payment Received via Lunu service<br>');
$order->payment_complete();
return true;
$order->payment_complete();
return true;
}
} elseif ($payment_status === 'awaiting_payment_confirmation') {
update_post_meta($order_id, '_lunupayment_status', $payment_status);
lunu_payment_awaiting($order);
return true;
}
return false;
return false;
}
// scheduled_subscription_payment function.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment