Skip to content

Commit

Permalink
Merge pull request #58 from aait/master
Browse files Browse the repository at this point in the history
Solves callback problems
  • Loading branch information
markusbrunke authored Dec 20, 2019
2 parents 8e520b3 + 48f7575 commit b043a6a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
44 changes: 42 additions & 2 deletions includes/class-wc-gateway-reepay-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,47 @@ public function return_handler() {
return;
}

// Get Invoice data
try {
$invoice_data = $result = $this->request( 'GET', 'https://api.reepay.com/v1/invoice/' . $invoice );
} catch ( Exception $e ) {
$invoice_data = array();
}

// Check if order status was applied
if ( in_array( $data['event_type'], [
'invoice_created',
'invoice_authorized',
'invoice_settled',
'invoice_cancelled',
'invoice_refund'
] ) ) {
switch ( $invoice_data['state'] ) {
case 'authorized':
if ( $order->get_status() === REEPAY_STATUS_AUTHORIZED ) {
$this->log( sprintf( 'WebHook: Event type: %s success. Order status: %s', $data['event_type'], $order->get_status() ) );
http_response_code(200);
return;
}
break;
case 'settled':
if ( $order->get_status() === REEPAY_STATUS_SETTLED ) {
$this->log( sprintf( 'WebHook: Event type: %s success. Order status: %s', $data['event_type'], $order->get_status() ) );
http_response_code(200);
return;
}
break;
case 'cancelled':
if ( $order->has_status( 'cancelled' )) {
$this->log( sprintf( 'WebHook: Event type: %s success. Order status: %s', $data['event_type'], $order->get_status() ) );
http_response_code(200);
return;
}
break;
}
}

// Check invoice state
switch ( $data['event_type'] ) {
case 'invoice_authorized':
// Reduce stock
Expand Down Expand Up @@ -988,8 +1029,7 @@ public function return_handler() {
$this->log( sprintf( 'WebHook: Success event type: %s', $data['event_type'] ) );
break;
case 'invoice_refund':
$result = $this->request( 'GET', 'https://api.reepay.com/v1/invoice/' . $invoice );
$credit_notes = $result['credit_notes'];
$credit_notes = $invoice_data['credit_notes'];
foreach ($credit_notes as $credit_note) {
// Get registered credit notes
$credit_note_ids = get_post_meta( $order->get_id(), '_reepay_credit_note_ids', TRUE );
Expand Down
22 changes: 22 additions & 0 deletions includes/class-wc-reepay-order-statuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function __construct() {
$this,
'is_paid'
), 10, 2 );

add_filter( 'woocommerce_cancel_unpaid_order', array(
$this,
'cancel_unpaid_order'
), 10, 3 );
}

/**
Expand Down Expand Up @@ -409,6 +414,23 @@ public function is_paid( $is_paid, $order ) {
return $is_paid;
}

/**
* Disable pending cancellation for Reepay Orders
*
* @param bool $is_cancel
* @param bool $is_checkout
* @param WC_Order $order
*
* @return bool
*/
public function cancel_unpaid_order( $is_cancel, $is_checkout, $order ) {
if ( in_array( $order->get_payment_method(), WC_ReepayCheckout::PAYMENT_METHODS ) ) {
$is_cancel = false;
}

return $is_cancel;
}

}

new WC_Reepay_Order_Statuses();

0 comments on commit b043a6a

Please sign in to comment.