Skip to content

Commit

Permalink
Merge pull request #6 from andrewlimaza/refund-functionality
Browse files Browse the repository at this point in the history
Added refund functionality
  • Loading branch information
andrewlimaza authored Apr 12, 2024
2 parents 0700369 + e519116 commit d3eedfb
Showing 1 changed file with 107 additions and 2 deletions.
109 changes: 107 additions & 2 deletions class.pmprogateway_paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ static function init()
// custom confirmation page

add_filter('pmpro_pages_shortcode_confirmation', array('PMProGateway_Paystack', 'pmpro_pages_shortcode_confirmation'), 20, 1);

// Refund functionality.
add_filter( 'pmpro_allowed_refunds_gateways', array( 'PMProGateway_Paystack', 'pmpro_allowed_refunds_gateways' ) );
add_filter( 'pmpro_process_refund_paystack', array( 'PMProGateway_Paystack', 'process_refund' ), 10, 2 );


}
}

Expand Down Expand Up @@ -136,6 +142,17 @@ static function pmpro_gateways($gateways)
}
return $gateways;
}

/**
* Enable refund functionality for paystack.
* @since TBD.
*/
static function pmpro_allowed_refunds_gateways( $gateways ) {
$gateways[] = 'paystack';
return $gateways;
}


function kkd_pmprosd_convert_date( $date ) {
// handle lower-cased y/m values.
$set_date = strtoupper($date);
Expand Down Expand Up @@ -848,11 +865,12 @@ static function pmpro_pages_shortcode_confirmation($content,$reference = null)
$request = wp_remote_post($subscription_url, $args);
if (!is_wp_error($request)) {
$paystack_response = json_decode(wp_remote_retrieve_body($request));
if ( isset( $paystack_response->data->status ) && 'success' == $paystack_response->data->status ) {
if ( isset( $paystack_response->data->status ) && 'active' == $paystack_response->data->status ) {
$subscription_code = $paystack_response->data->subscription_code;
$token = $paystack_response->data->email_token;
$morder->subscription_transaction_id = $subscription_code;
$morder->subscription_token = $token;
$morder->saveOrder();
}
}
}
Expand Down Expand Up @@ -1052,7 +1070,10 @@ function cancel(&$order, $update_status = true )
return true;
}

/// Used for updating subscription stuff.
/**
* Allow "Sync" with gateway for subscriptions.
* @since 1.7.2
*/
public function update_subscription_info( $subscription ) {
$subscription_id = $subscription->get_subscription_transaction_id();
$backtrace = self::get_caller_info();
Expand Down Expand Up @@ -1120,6 +1141,90 @@ public function update_subscription_info( $subscription ) {
}
}

/**
* Allow refunds from within Paid Memberships Pro and Paystack.
* @since TBD
*/
public static function process_refund( $success, $order ) {
global $current_user;

//default to using the payment id from the order
if ( !empty( $order->payment_transaction_id ) ) {
$transaction_id = $order->payment_transaction_id;
}

//need a transaction id
if ( empty( $transaction_id ) ) {
return false;
}

// OKAY do the refund now.
// Make the API call to PayStack to refund the order.
$mode = pmpro_getOption("gateway_environment");
if ( $mode == "sandbox" ) {
$key = pmpro_getOption("paystack_tsk");

} else {
$key = pmpro_getOption("paystack_lsk");
}

$paystack_url = 'https://api.paystack.co/refund/';

$headers = array(
'Authorization' => 'Bearer ' . $key,
'Cache-Control' => 'no-cache'
);

// The transaction ID for the refund.
$fields = array(
'transaction' => $transaction_id
);

$args = array(
'headers' => $headers,
'timeout' => 60,
'body' => $fields
);


$success = false;

// Try to make the API call now.
$request = wp_remote_post( $paystack_url, $args );

if ( ! is_wp_error( $request ) ) {

$response = json_decode( wp_remote_retrieve_body( $request ) );

// If not successful throw an error.
if ( ! $response->status ) {
$order->notes = trim( $order->notes.' '.sprintf( __('Admin: Order refund failed on %1$s for transaction ID %2$s by %3$s. Order may have already been refunded.', 'paid-memberships-pro' ), date_i18n('Y-m-d H:i:s'), $transaction_id, $current_user->display_name ) );
$order->saveOrder();
} else {
// Set the order status to refunded and save it and return true
$order->status = 'refunded';

$success = true;

$order->notes = trim( $order->notes.' '.sprintf( __('Admin: Order successfully refunded on %1$s for transaction ID %2$s by %3$s.', 'paid-memberships-pro' ), date_i18n('Y-m-d H:i:s'), $transaction_id, $current_user->display_name ) );

$user = get_user_by( 'id', $order->user_id );
//send an email to the member
$myemail = new PMProEmail();
$myemail->sendRefundedEmail( $user, $order );

//send an email to the admin
$myemail = new PMProEmail();
$myemail->sendRefundedAdminEmail( $user, $order );

$order->saveOrder();
}
}

return $success;

}

/**
* Undocumented function
*
Expand Down

0 comments on commit d3eedfb

Please sign in to comment.