Skip to content

Commit 5aa3e9a

Browse files
committed
v1.0.1 - cancel Paystack subscription from PMP pro cancellation.
1 parent 824460a commit 5aa3e9a

1 file changed

Lines changed: 81 additions & 2 deletions

File tree

class.pmprogateway_paystack.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static function init() {
5454
add_filter('pmpro_payment_option_fields', array('PMProGateway_paystack', 'pmpro_payment_option_fields'), 10, 2);
5555
add_action( 'wp_ajax_kkd_pmpro_paystack_ipn', array('PMProGateway_paystack', 'kkd_pmpro_paystack_ipn'));
5656
add_action( 'wp_ajax_nopriv_kkd_pmpro_paystack_ipn', array('PMProGateway_paystack', 'kkd_pmpro_paystack_ipn'));
57-
57+
// add_action( 'pmpro_after_change_membership_level', array('PMProGateway_paystack','kkd_pmpro_after_change_membership_level'), 10, 3 );
5858
//code to add at checkout
5959
$gateway = pmpro_getGateway();
6060
if($gateway == "paystack")
@@ -63,6 +63,7 @@ static function init() {
6363
add_filter('pmpro_required_billing_fields', array('PMProGateway_paystack', 'pmpro_required_billing_fields'));
6464
add_filter('pmpro_include_payment_information_fields', '__return_false');
6565
add_filter('pmpro_checkout_before_change_membership_level', array('PMProGateway_paystack', 'pmpro_checkout_before_change_membership_level'), 10, 2);
66+
6667
add_filter('pmpro_gateways_with_pending_status', array('PMProGateway_paystack', 'pmpro_gateways_with_pending_status'));
6768
add_filter('pmpro_pages_shortcode_checkout', array('PMProGateway_paystack', 'pmpro_pages_shortcode_checkout'), 20, 1);
6869
add_filter('pmpro_checkout_default_submit_button', array('PMProGateway_paystack', 'pmpro_checkout_default_submit_button'));
@@ -147,6 +148,12 @@ function kkd_pmpro_paystack_ipn() {
147148
http_response_code(200);
148149
exit();
149150
}
151+
152+
static function kkd_pmpro_paystack_disable_subscription($code) {
153+
global $wpdb;
154+
155+
156+
}
150157
/**
151158
* Get a list of payment options that the Paystack gateway needs/supports.
152159
*/
@@ -294,6 +301,28 @@ static function pmpro_checkout_before_change_membership_level($user_id, $morder)
294301

295302
$morder->Gateway->sendToPaystack($morder);
296303
}
304+
static function pmpro_checkout_after_change_membership_level($user_id, $morder)
305+
{
306+
global $wpdb, $discount_code_id;
307+
308+
//if no order, no need to pay
309+
if(empty($morder)) {
310+
return;
311+
}
312+
if(empty($morder->code))
313+
$morder->code = $morder->getRandomCode();
314+
315+
$morder->payment_type = "paystack";
316+
$morder->status = "pending";
317+
$morder->user_id = $user_id;
318+
$morder->saveOrder();
319+
320+
//save discount code use
321+
if(!empty($discount_code_id))
322+
$wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discount_code_id . "', '" . $user_id . "', '" . $morder->id . "', now())");
323+
324+
$morder->Gateway->sendToPaystack($morder);
325+
}
297326

298327
function sendToPaystack(&$order) {
299328
global $wp;
@@ -525,6 +554,9 @@ static function pmpro_pages_shortcode_confirmation($content,$reference = null) {
525554
$interval = 'annually';
526555

527556
}
557+
558+
// echo $interval.' - '.$pmpro_level->cycle_period;
559+
// die();
528560
$amount = $pmpro_level->billing_amount;
529561
$koboamount = $amount*100;
530562
//Create Plan
@@ -677,10 +709,57 @@ static function pmpro_pages_shortcode_confirmation($content,$reference = null) {
677709
return $content;
678710

679711
}
680-
function delete(&$order) {
712+
function cancel(&$order) {
713+
681714
//no matter what happens below, we're going to cancel the order in our system
682715
$order->updateStatus("cancelled");
716+
$mode = pmpro_getOption("gateway_environment");
717+
$code = $order->subscription_transaction_id;
718+
if ($mode == 'sandbox') {
719+
$key = pmpro_getOption("paystack_tsk");
720+
}else{
721+
$key = pmpro_getOption("paystack_lsk");
683722

723+
}
724+
if ($order->subscription_transaction_id != "") {
725+
# code...
726+
$paystack_url = 'https://api.paystack.co/subscription/' . $code;
727+
$headers = array(
728+
'Authorization' => 'Bearer ' . $key
729+
);
730+
$args = array(
731+
'headers' => $headers,
732+
'timeout' => 60
733+
);
734+
$request = wp_remote_get( $paystack_url, $args );
735+
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
736+
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
737+
if ('active' == $paystack_response->data->status && $code == $paystack_response->data->subscription_code && '1' == $paystack_response->status) {
738+
739+
$paystack_url = 'https://api.paystack.co/subscription/disable';
740+
$headers = array(
741+
'Content-Type' => 'application/json',
742+
'Authorization' => "Bearer ".$key
743+
);
744+
$body = array(
745+
'code' => $paystack_response->data->subscription_code,
746+
'token' => $paystack_response->data->email_token,
747+
748+
);
749+
$args = array(
750+
'body' => json_encode( $body ),
751+
'headers' => $headers,
752+
'timeout' => 60
753+
);
754+
755+
$request = wp_remote_post( $paystack_url, $args );
756+
// print_r($request);
757+
if( ! is_wp_error( $request )) {
758+
$paystack_response = json_decode(wp_remote_retrieve_body($request));
759+
}
760+
}
761+
}
762+
}
684763
global $wpdb;
685764
$wpdb->query("DELETE FROM $wpdb->pmpro_membership_orders WHERE id = '" . $order->id . "'");
686765
}

0 commit comments

Comments
 (0)