Skip to content

Commit 824460a

Browse files
committed
V1.0.1 Recurring Billings added
1 parent ea4d4b9 commit 824460a

1 file changed

Lines changed: 90 additions & 6 deletions

File tree

class.pmprogateway_paystack.php

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ static function pmpro_gateways($gateways) {
113113
return $gateways;
114114
}
115115
function kkd_pmpro_paystack_ipn() {
116-
if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
117-
exit();
118-
}
116+
global $wpdb;
117+
// if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
118+
// exit();
119+
// }
119120
define( 'SHORTINIT', true );
120121
$input = @file_get_contents("php://input");
121122
$event = json_decode($input);
122-
if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, paystack_recurrent_billing_get_secret_key()))){
123-
exit();
124-
}
123+
// echo "<pre>";
124+
// print_r($event);
125+
// if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, paystack_recurrent_billing_get_secret_key()))){
126+
// exit();
127+
// }
125128
switch($event->event){
126129
case 'subscription.create':
127130

@@ -135,7 +138,10 @@ function kkd_pmpro_paystack_ipn() {
135138
$morder->Gateway->pmpro_pages_shortcode_confirmation('',$event->data->reference);
136139
break;
137140
case 'invoice.create':
141+
self::renewpayment($event);
138142
case 'invoice.update':
143+
self::renewpayment($event);
144+
139145
break;
140146
}
141147
http_response_code(200);
@@ -352,6 +358,84 @@ function sendToPaystack(&$order) {
352358
}
353359
exit;
354360
}
361+
static function renewpayment($event) {
362+
global $wp,$wpdb;
363+
364+
if (isset($event->data->paid) && ($event->data->paid == 1)) {
365+
366+
$amount = $event->data->subscription->amount/100;
367+
$old_order = new MemberOrder();
368+
$subscription_code = $event->data->subscription->subscription_code;
369+
$email = $event->data->customer->email;
370+
$old_order->getLastMemberOrderBySubscriptionTransactionID($subscription_code);
371+
372+
if(empty($old_order)){ exit(); }
373+
$user_id = $old_order->user_id;
374+
$user = get_userdata($user_id);
375+
$user->membership_level = pmpro_getMembershipLevelForUser($user_id);
376+
377+
if(empty($user)) { exit(); }
378+
379+
$morder = new MemberOrder();
380+
$morder->user_id = $old_order->user_id;
381+
$morder->membership_id = $old_order->membership_id;
382+
$morder->InitialPayment = $amount; //not the initial payment, but the order class is expecting this
383+
$morder->PaymentAmount = $amount;
384+
$morder->payment_transaction_id = $event->data->invoice_code;
385+
$morder->subscription_transaction_id = $subscription_code;
386+
387+
$morder->gateway = $old_order->gateway;
388+
$morder->gateway_environment = $old_order->gateway_environment;
389+
390+
$morder->Email = $email;
391+
$pmpro_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . (int)$morder->membership_id . "' LIMIT 1");
392+
$pmpro_level = apply_filters("pmpro_checkout_level", $pmpro_level);
393+
$startdate = apply_filters("pmpro_checkout_start_date", "'" . current_time("mysql") . "'", $morder->user_id, $pmpro_level);
394+
395+
$enddate = "'" . date("Y-m-d", strtotime("+ " . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, current_time("timestamp"))) . "'";
396+
397+
$custom_level = array(
398+
'user_id' => $morder->user_id,
399+
'membership_id' => $pmpro_level->id,
400+
'code_id' => '',
401+
'initial_payment' => $pmpro_level->initial_payment,
402+
'billing_amount' => $pmpro_level->billing_amount,
403+
'cycle_number' => $pmpro_level->cycle_number,
404+
'cycle_period' => $pmpro_level->cycle_period,
405+
'billing_limit' => $pmpro_level->billing_limit,
406+
'trial_amount' => $pmpro_level->trial_amount,
407+
'trial_limit' => $pmpro_level->trial_limit,
408+
'startdate' => $startdate,
409+
'enddate' => $enddate
410+
);
411+
412+
//get CC info that is on file
413+
$morder->expirationmonth = get_user_meta($user_id, "pmpro_ExpirationMonth", true);
414+
$morder->expirationyear = get_user_meta($user_id, "pmpro_ExpirationYear", true);
415+
$morder->ExpirationDate = $morder->expirationmonth . $morder->expirationyear;
416+
$morder->ExpirationDate_YdashM = $morder->expirationyear . "-" . $morder->expirationmonth;
417+
418+
419+
//save
420+
if ($morder->status != 'success') {
421+
422+
if (pmpro_changeMembershipLevel($custom_level, $morder->user_id, 'changed')){
423+
$morder->status = "success";
424+
$morder->saveOrder();
425+
}
426+
427+
}
428+
$morder->getMemberOrderByID($morder->id);
429+
430+
//email the user their invoice
431+
$pmproemail = new PMProEmail();
432+
$pmproemail->sendInvoiceEmail($user, $morder);
433+
434+
do_action('pmpro_subscription_payment_completed', $morder);
435+
exit();
436+
}
437+
438+
}
355439

356440
static function pmpro_pages_shortcode_checkout($content) {
357441
$morder = new MemberOrder();

0 commit comments

Comments
 (0)