Skip to content

Commit

Permalink
subscriptions cron issues and retry count added.
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihtoprak committed Oct 25, 2022
1 parent b47a943 commit 97a18e8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
1 change: 1 addition & 0 deletions core/library/Moka_Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ public function receipt_page( $orderId )
'subscription_status' => '0',
'subscription_period' => data_get($subscriptionPeriod, 'period_string'),
'subscription_next_try' => data_get($subscriptionPeriod, 'next_try'),
'try_count' => 0,
'user_id' => $userId,
'optimist_id' => data_get($orderDetails,'OtherTrxCode'),
'created_at' => current_datetime()->format('Y-m-d H:i:s'),
Expand Down
2 changes: 1 addition & 1 deletion core/library/Moka_Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exit;
}

define('OPTIMISTHUB_MOKA_PAY_VERSION', '3.0.3');
define('OPTIMISTHUB_MOKA_PAY_VERSION', '3.0.4');

global $mokaVersion;
$mokaVersion = OPTIMISTHUB_MOKA_PAY_VERSION;
Expand Down
77 changes: 53 additions & 24 deletions core/library/Moka_Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function __construct()
add_action( 'init', [$this, 'triggerSubscriptionPayments'] );
add_action( 'moka_subscriptions_recurring_payments_cron_job', [$this, 'runSubscriptionPayments']);

add_action( 'init', [$this, 'mybeAddColumnIfIsNotExists'] );


}

Expand Down Expand Up @@ -483,7 +485,7 @@ public function changePlaceOrderTextForSubscription( $buttonText )
public function triggerSubscriptionPayments()
{
if(!wp_next_scheduled('moka_subscriptions_recurring_payments_cron_job')) {
wp_schedule_event(time(), 'every_minute', 'moka_subscriptions_recurring_payments_cron_job');
wp_schedule_event(time(), 'daily', 'moka_subscriptions_recurring_payments_cron_job');
}
}

Expand All @@ -495,13 +497,31 @@ public function runSubscriptionPayments()
if($records)
{
foreach ($records as $perKey => $perValue) {

$paymentDate = data_get($perValue, 'subscription_next_try');
$currentTime = current_datetime()->format('Y-m-d H:i:s');

$tryCount = (int)data_get($perValue, 'try_count');
$orderId = data_get($perValue, 'order_id');

if(strtotime($paymentDate)<time())
{
$orderId = data_get($perValue, 'order_id');
if($tryCount>=3)
{
$wpdb->update( $wpdb->prefix.$table,
[
'try_count' => ($tryCount+1),
'subscription_status' => 2
],
['order_id' => $orderId]
);
return;
}

if(!data_get($perValue, 'subscription_period'))
{
return;
}

$orderDetails = json_decode(data_get($perValue, 'order_details'));
$payment = new MokaPayment();
$otherTrxCode = data_get($orderDetails, 'OtherTrxCode').'-'.date('His',strtotime($currentTime));
Expand Down Expand Up @@ -541,34 +561,27 @@ public function runSubscriptionPayments()
$subscriptionPeriod = data_get($perValue, 'subscription_period');
$currentTime = Carbon::parse(current_datetime()->format('Y-m-d H:i:s'));

$__data = get_post_meta($productId);
$__per = data_get($__data, '_period_per.0', null);
$__in = data_get($__data, '_period_in.0', null);
$__data = explode(' ',$subscriptionPeriod);

$nextTry = $currentTime::now()->add($__per, $__in);

$nextTry = $currentTime::now()->add($__data[0], $__data[1]);

$period = [
'current_time' => Carbon::parse($currentTime)->format('Y-m-d H:i:s'),
'next_try' => Carbon::parse($nextTry)->format('Y-m-d H:i:s'),
'period_string' => $__per.' '.$__in,
'period_string' => $subscriptionPeriod
];

$wpdb->query(
$wpdb->prepare( "UPDATE $wpdb->prefix$table SET subscription_period = %s WHERE order_id = %d", $period['period_string'], $orderId ),
);

$wpdb->query(
$wpdb->prepare( "UPDATE $wpdb->prefix$table SET updated_at = %s WHERE order_id = %d", current_datetime()->format('Y-m-d H:i:s'), $orderId ),
);

$wpdb->query(
$wpdb->prepare( "UPDATE $wpdb->prefix$table SET subscription_next_try = %s WHERE order_id = %d", $period['next_try'], $orderId ),
);
$wpdb->update( $wpdb->prefix.$table,
[
'subscription_period' => $period['period_string'],
'updated_at' => current_datetime()->format('Y-m-d H:i:s'),
'subscription_next_try' => $period['next_try'],
'optimist_id' => $otherTrxCode,
'try_count' => ($tryCount+1)
],
['order_id' => $orderId]
);

$wpdb->query(
$wpdb->prepare( "UPDATE $wpdb->prefix$table SET optimist_id = %s WHERE order_id = %d", $otherTrxCode, $orderId ),
);
}
}
}
Expand Down Expand Up @@ -614,6 +627,22 @@ private function getOrderCustomerId($orderId)
$userId = $order->get_user_id();
return $userId;
}

/**
* Add Try Count
*
* @return void
*/
public function mybeAddColumnIfIsNotExists()
{
global $wpdb;
$table = $wpdb->prefix.'moka_subscriptions';
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table' AND column_name = 'try_count'" );

if(empty($row)){
$wpdb->query("ALTER TABLE $table ADD try_count INT(1) NOT NULL DEFAULT 0 AFTER `subscription_next_try`");
}
}
}

new MokaSubscription();
26 changes: 20 additions & 6 deletions core/library/Moka_Subscriptions_History.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,31 @@ private function formatTableDataResults($data)
'subscription_period' => data_get($perRow, 'subscription_period', null),
'subscription_status' => $status == 0 ? '<mark class="active_subs">Aktif</mark>' : '<mark class="passive_subs">Pasif</mark>',
'created_at' => date('d.m.Y.H:i:s', strtotime(data_get($perRow, 'created_at', null))),
'actions' =>
($status == 0) ? '
<!--<span class="subscription-payManually">Ödeme</span>-->
<span data-order-id="'.$orderId.'" class="subscription-cancelManually">İptal</span>
' : '
<span data-order-id="'.$orderId.'" class="subscription-noActions">Düzenlenemez</span>'
'actions' => self::statusString($status,$orderId,$perRow)
];
}
}

return $return;

}

private function statusString($status,$orderId,$row)
{

$return = '<span data-order-id="'.$orderId.'" class="subscription-noActions">Düzenlenemez</span>';

if($status==0)
{
$return = '<span data-order-id="'.$orderId.'" class="subscription-cancelManually">İptal</span>';
}

if($status==2)
{
$return = '<span class="subscription-cancelManually" style="background:#000">Ödeme Başarısız</span><br>'.$row['updated_at'];
}

return $return;

}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Moka Payment Gateway for WooCommerce
* Plugin URI: https://github.com/optimisthub/moka-woocommerce
* Description: Moka Payment gateway for woocommerce
* Version: 3.0.3
* Version: 3.0.4
* Author: Optimist Hub
* Author URI: https://optimisthub.com?ref=mokaPayment
* Domain Path: /languages/
Expand Down

0 comments on commit 97a18e8

Please sign in to comment.