Skip to content

Commit

Permalink
Merge pull request #150 from antondrob/ddaniel
Browse files Browse the repository at this point in the history
fix add_subscription_card_id
  • Loading branch information
markusbrunke authored Jul 21, 2022
2 parents def5ee2 + 59824a9 commit 0d5f870
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
6 changes: 1 addition & 5 deletions includes/class-wc-reepay-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,7 @@ public static function order_status_changed( $order_id, $from, $to, $order ) {
throw new Exception( $order_data->get_error_message() );
}

if ( $amount <= ( $order_data['authorized_amount'] / 100 ) ) {
$amount_to_capture = $amount;
} else {
$amount_to_capture = $order_data['authorized_amount'] / 100;
}
$amount_to_capture = rp_make_initial_amount($order_data['authorized_amount'] - $order_data['settled_amount'], $order->get_currency());

$gateway->capture_payment( $order, $amount_to_capture );
} catch (Exception $e) {
Expand Down
49 changes: 27 additions & 22 deletions includes/class-wc-reepay-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,22 @@ public function capture_payment( $order, $amount ) {
}

$gateway = rp_get_payment_method( $order );

$order_lines = $gateway->get_order_items( $order );
$settled_lines = WC_Reepay_Instant_Settle::get_settled_items( $order );

foreach ($settled_lines as $settled_line_key => $settled_line) {
foreach ( $order_lines as $order_line_key => $order_line ) {
if( $settled_line['ordertext'] == $order_line['ordertext'] ) {
$amount -= rp_make_initial_amount($order_lines[$order_line_key]['amount'], $order->get_currency());

return $this->settle( $order, $amount, $order_lines );
unset($order_lines[$order_line_key]);
break;
}
}
}

return $this->settle( $order, $amount, array_values($order_lines) );
}

/**
Expand Down Expand Up @@ -420,23 +433,22 @@ public function settle( WC_Order $order, $amount = null, $item_data = false ) {
return new WP_Error( 0, 'Unable to get order handle' );
}

if ( ! $amount ) {
$amount = WC_Reepay_Instant_Settle::calculate_instant_settle( $order );
}
if ( ! $amount || ! $item_data ) {
$settle_data = WC_Reepay_Instant_Settle::calculate_instant_settle( $order );

if ( $item_data ) {
$request_data['order_lines'] = $item_data;
if(floatval($item_data[0]['amount']) <= 0){
return new WP_Error( 100, 'Amount must be lager than zero' );
}
} else {
$request_data['amount'] = rp_prepare_amount( $amount, $order->get_currency() );
if($request_data['amount'] <= 0){
return new WP_Error( 100, 'Amount must be lager than zero' );
}
}
if ( ! $amount ) {
$amount = $settle_data->settle_amount;
}

if ( ! $item_data ) {
$item_data = $settle_data->items;
}
}

$request_data['order_lines'] = $item_data;
if(floatval(current($item_data)['amount']) <= 0){
return new WP_Error( 100, 'Amount must be lager than zero' );
}

$result = $this->request(
'POST',
Expand Down Expand Up @@ -464,15 +476,8 @@ public function settle( WC_Order $order, $amount = null, $item_data = false ) {
);
}


set_transient( 'reepay_api_action_error', $error, MINUTE_IN_SECONDS / 2 );

//$order->add_order_note(
//sprintf( __( 'Failed to settle "%s". Error: %s.', 'reepay-checkout-gateway' ),
//wc_price( $amount ),
//$result->get_error_message()
//) );

return $result;
}

Expand Down
44 changes: 40 additions & 4 deletions includes/class-wc-reepay-instant-settle.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ public function process_instant_settle( $order ) {
* @return stdClass
*/
public static function calculate_instant_settle( $order ) {
/** @var WC_Reepay_Order_Capture $WC_Reepay_Order_Capture*/
global $WC_Reepay_Order_Capture;

if ( is_null( $WC_Reepay_Order_Capture ) ) {
$WC_Reepay_Order_Capture = new WC_Reepay_Order_Capture();
}

$online_virtual = false;
$recurring = false;
$physical = false;
$total = 0;
$debug = [];
$items_data = [];

/** @var array $settle */
$settle = rp_get_payment_method( $order )->settle;
Expand All @@ -119,8 +127,9 @@ public static function calculate_instant_settle( $order ) {
'type' => self::SETTLE_PHYSICAL
);

$physical = true;
$total += $price_incl_tax;
$physical = true;
$total += $price_incl_tax;
$items_data[] = $WC_Reepay_Order_Capture->get_item_data( $item, $order );

continue;
} elseif ( in_array( self::SETTLE_VIRTUAL, $settle, true ) &&
Expand All @@ -136,6 +145,7 @@ public static function calculate_instant_settle( $order ) {

$online_virtual = true;
$total += $price_incl_tax;
$items_data[] = $WC_Reepay_Order_Capture->get_item_data( $item, $order );

continue;
} elseif ( in_array( self::SETTLE_RECURRING, $settle, true ) &&
Expand All @@ -148,8 +158,9 @@ public static function calculate_instant_settle( $order ) {
'type' => self::SETTLE_RECURRING
);

$recurring = true;
$total += $price_incl_tax;
$recurring = true;
$total += $price_incl_tax;
$items_data[] = $WC_Reepay_Order_Capture->get_item_data( $item, $order );

continue;
}
Expand Down Expand Up @@ -207,11 +218,36 @@ public static function calculate_instant_settle( $order ) {
$result = new stdClass();
$result->is_instant_settle = $online_virtual || $physical || $recurring;
$result->settle_amount = $total;
$result->items = $items_data;
$result->debug = $debug;
$result->settings = $settle;

return $result;
}

/**
* @param WC_Order $order
*
* @return array<array>
*/
public static function get_settled_items( $order ) {
/** @var WC_Reepay_Order_Capture $WC_Reepay_Order_Capture */
global $WC_Reepay_Order_Capture;

if ( is_null( $WC_Reepay_Order_Capture ) ) {
$WC_Reepay_Order_Capture = new WC_Reepay_Order_Capture();
}

$settled = [];

foreach ( $order->get_items() as $item ) {
if ( ! empty( $item->get_meta( 'settled' ) ) ) {
$settled[] = $WC_Reepay_Order_Capture->get_item_data( $item, $order );
}
}

return $settled;
}
}

new WC_Reepay_Instant_Settle();
5 changes: 5 additions & 0 deletions includes/class-wc-reepay-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public static function add_subscription_card_id( $order_id ) {
foreach ( $subscriptions as $subscription ) {
/** @var WC_Subscription $subscription */
$gateway = rp_get_payment_method( $subscription );

if ( ! $gateway ) {
continue;
}

$token = $gateway::get_payment_token_order( $subscription );
if ( ! $token ) {
// Copy tokens from parent order
Expand Down

0 comments on commit 0d5f870

Please sign in to comment.