Skip to content

Commit f3a8e5c

Browse files
committed
Cleanup
1 parent 007d2ab commit f3a8e5c

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

SI_Paystack.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,89 @@ private function set_error_messages( $message, $display = true ) {
405405
do_action( 'si_error', __CLASS__ . '::' . __FUNCTION__ . ' - error message from paystack', $message );
406406
}
407407
}
408+
/**
409+
* Process Webhook
410+
*/
411+
public function process_webhooks() {
412+
413+
if ( ( strtoupper( $_SERVER['REQUEST_METHOD'] ) != 'POST' ) || ! array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
414+
exit;
415+
}
416+
417+
$json = file_get_contents( "php://input" );
418+
419+
// validate event do all at once to avoid timing attack
420+
if ( $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac( 'sha512', $json, $this->secret_key ) ) {
421+
exit;
422+
}
423+
424+
$event = json_decode( $json );
425+
426+
if ( 'charge.success' == $event->event ) {
427+
428+
http_response_code( 200 );
429+
430+
$order_details = explode( '_', $event->data->reference );
431+
432+
$order_id = (int) $order_details[0];
433+
434+
$order = wc_get_order($order_id);
435+
436+
$paystack_txn_ref = get_post_meta( $order_id, '_paystack_txn_ref', true );
437+
438+
if ( $event->data->reference != $paystack_txn_ref ) {
439+
exit;
440+
}
441+
442+
if ( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
443+
exit;
444+
}
445+
446+
$order_total = $order->get_total();
447+
448+
$amount_paid = $event->data->amount / 100;
449+
450+
$paystack_ref = $event->data->reference;
451+
452+
// check if the amount paid is equal to the order amount.
453+
if ( $order_total != $amount_paid ) {
454+
455+
$order->update_status( 'on-hold', '' );
456+
457+
add_post_meta( $order_id, '_transaction_id', $paystack_ref, true );
458+
459+
$notice = 'Thank you for shopping with us.<br />Your payment transaction was successful, but the amount paid is not the same as the total order amount.<br />Your order is currently on-hold.<br />Kindly contact us for more information regarding your order and payment status.';
460+
$notice_type = 'notice';
461+
462+
// Add Customer Order Note
463+
$order->add_order_note( $notice, 1 );
464+
465+
// Add Admin Order Note
466+
$order->add_order_note('<strong>Look into this order</strong><br />This order is currently on hold.<br />Reason: Amount paid is less than the total order amount.<br />Amount Paid was <strong>&#8358;'.$amount_paid.'</strong> while the total order amount is <strong>&#8358;'.$order_total.'</strong><br />Paystack Transaction Reference: '.$paystack_ref );
467+
468+
$order->reduce_order_stock();
469+
470+
wc_add_notice( $notice, $notice_type );
471+
472+
wc_empty_cart();
473+
474+
} else {
475+
476+
$order->payment_complete( $paystack_ref );
477+
478+
$order->add_order_note( sprintf( 'Payment via Paystack successful (Transaction Reference: %s)', $paystack_ref ) );
479+
480+
wc_empty_cart();
481+
482+
}
483+
484+
$this->save_card_details( $event, $order->get_user_id(), $order_id );
485+
486+
exit;
487+
}
488+
489+
exit;
490+
491+
}
408492
}
409493
SI_Paystack::register();

0 commit comments

Comments
 (0)