Skip to content

Commit

Permalink
Process payment refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaspolohov@yandex.ru authored and dimaspolohov@yandex.ru committed Dec 1, 2023
1 parent a448965 commit 3da9a11
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 88 deletions.
6 changes: 3 additions & 3 deletions includes/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function capture_payment( $order, $amount = null ) {
* @return array|mixed|object|WP_Error
* @see ReepayGateway::payment_methods.
*/
public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false, $payment_text = '' ) {
public function recurring( array $payment_methods, WC_Order $order, array $data, $token = false ) {
$params = array(
'locale' => $data['language'],
'create_customer' => array(
Expand All @@ -582,8 +582,8 @@ public function recurring( array $payment_methods, WC_Order $order, array $data,
'cancel_url' => $order->get_cancel_order_url(),
);

if ( ! empty( $payment_text ) ) {
$params['button_text'] = $payment_text;
if ( ! empty( $data['button_text'] ) ) {
$params['button_text'] = $data['button_text'];
}

if ( ! empty( $token ) ) {
Expand Down
197 changes: 112 additions & 85 deletions includes/Gateways/ReepayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,14 @@ public function payment_fields() {
}

/**
* Process Payment
* Run some conditions for woo blocks using
*
* @param int $order_id Order ID.
* @param object $order Order.
*
* @return array|false
* @return false
* @throws Exception If payment error.
*/
public function process_payment( $order_id ) {
public function woo_blocks_conditions( $order ){
$is_woo_blocks_checkout_request = false;

if ( isset( $_SERVER['CONTENT_TYPE'] ) && 'application/json' === $_SERVER['CONTENT_TYPE'] ) {
Expand All @@ -747,14 +747,11 @@ public function process_payment( $order_id ) {
}
}
} catch ( Exception $e ) {
return array(
'messages' => __( 'Wrong Request. Try again', 'reepay-checkout-gateway' ),
'result' => 'failure',
);
throw new Exception( __('Wrong Request. Try again', 'reepay-checkout-gateway') );
}
}

$order = wc_get_order( $order_id );


if ( $is_woo_blocks_checkout_request ) {
/**
Expand All @@ -763,7 +760,10 @@ public function process_payment( $order_id ) {
$order->calculate_totals();
}

$token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new';
return false;
}

public function get_params( $order, $token_id ){

if ( 'yes' === $this->save_cc
&& 'new' === $token_id
Expand All @@ -775,28 +775,13 @@ public function process_payment( $order_id ) {
$maybe_save_card = wcs_cart_have_subscription();
}

$customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order->get_id() );

$country = WC()->countries->country_exists( $order->get_billing_country() ) ? $order->get_billing_country() : '';
if ( $order->needs_shipping_address() ) {
$country = WC()->countries->country_exists( $order->get_shipping_country() ) ? $order->get_shipping_country() : '';
}

if ( wcs_is_payment_change() ) {
return $this->wcs_change_payment_method( $order, $token_id );
}

$customer_handle = reepay()->api( $this )->get_customer_handle_by_order( $order_id );

$data = array(
'country' => $country,
'customer_handle' => $customer_handle,
'test_mode' => $this->test_mode,
'return_url' => $this->get_return_url( $order ),
'language' => $this->get_language(),
);

$order_handle = rp_get_order_handle( $order );

// Initialize Payment.
$params = array(
'locale' => $this->get_language(),
'recurring' => apply_filters(
Expand All @@ -805,7 +790,7 @@ public function process_payment( $order_id ) {
$order
),
'order' => array(
'handle' => $order_handle,
'handle' => rp_get_order_handle( $order ),
'amount' => 'yes' === $this->skip_order_lines ? rp_prepare_amount( $order->get_total(), $order->get_currency() ) : null,
'order_lines' => 'no' === $this->skip_order_lines ? $this->get_order_items( $order ) : null,
'currency' => $order->get_currency(),
Expand All @@ -815,6 +800,7 @@ public function process_payment( $order_id ) {
'email' => $order->get_billing_email(),
'address' => $order->get_billing_address_1(),
'address2' => $order->get_billing_address_2(),
'country' => ! empty( $country ) ? $country : '',
'city' => $order->get_billing_city(),
'phone' => $order->get_billing_phone(),
'company' => $order->get_billing_company(),
Expand All @@ -828,6 +814,7 @@ public function process_payment( $order_id ) {
'email' => $order->get_billing_email(),
'address' => $order->get_billing_address_1(),
'address2' => $order->get_billing_address_2(),
'country' => ! empty( $country ) ? $country : '',
'city' => $order->get_billing_city(),
'phone' => $order->get_billing_phone(),
'company' => $order->get_billing_company(),
Expand All @@ -846,11 +833,6 @@ public function process_payment( $order_id ) {
$params['button_text'] = reepay()->get_setting( 'payment_button_text' );
}

if ( ! empty( $country ) ) {
$params['order']['customer']['country'] = $country;
$params['order']['billing_address']['country'] = $country;
}

if ( $this->payment_methods && count( $this->payment_methods ) > 0 ) {
$params['payment_methods'] = $this->payment_methods;
}
Expand All @@ -862,6 +844,7 @@ public function process_payment( $order_id ) {
'email' => $order->get_billing_email(),
'address' => $order->get_shipping_address_1(),
'address2' => $order->get_shipping_address_2(),
'country' => ! empty( $country ) ? $country : '',
'city' => $order->get_shipping_city(),
'phone' => $order->get_billing_phone(),
'company' => $order->get_shipping_company(),
Expand All @@ -871,58 +854,86 @@ public function process_payment( $order_id ) {
'postal_code' => $order->get_shipping_postcode(),
'state_or_province' => $order->get_shipping_state(),
);

if ( ! empty( $country ) ) {
$params['order']['shipping_address']['country'] = $country;
}
}

if ( 'reepay_mobilepay_subscriptions' === $order->get_payment_method() ) {
$params['parameters']['mps_ttl'] = 'PT24H';
}

if ( absint( $token_id ) > 0 ) {
$token = new TokenReepay( $token_id );

$params['card_on_file'] = $token->get_token();
$params['card_on_file_require_cvv'] = false;
$params['card_on_file_require_exp_date'] = false;
unset( $params['recurring'] );
}

// "Save Card" flag
$order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card );
$order->save_meta_data();

return $params;
}

/**
* Process Payment
*
* @param int $order_id Order ID.
*
* @return array|false
* @throws Exception If payment error.
*/
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );

try {
$this->woo_blocks_conditions( $order );
}catch (Exception $e){
wc_add_notice( $e->getMessage(), 'error' );
return array(
'messages' => __( $e->getMessage() ),
'result' => 'failure',
);
}

$token_id = isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) ? wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) : 'new';

if ( wcs_is_payment_change() ) {
return $this->wcs_change_payment_method( $order, $token_id );
}

$params = $this->get_params( $order, $token_id );

$data = array(
'country' => $params['order']['customer']['country'],
'customer_handle' => $params['order']['customer']['handle'],
'test_mode' => $this->test_mode,
'return_url' => $this->get_return_url( $order ),
'language' => $this->get_language(),
'button_text' => reepay()->get_setting( 'payment_button_text' ),
);

// Try to charge with saved token.
if ( absint( $token_id ) > 0 ) {
$token = new TokenReepay( $token_id );
if ( ! $token->get_id() ) {
wc_add_notice( __( 'Failed to load token.', 'reepay-checkout-gateway' ), 'error' );

return false;
}

if ( $token->get_user_id() !== $order->get_user_id() ) {
wc_add_notice( __( 'Access denied.', 'reepay-checkout-gateway' ), 'error' );

return false;
}

$params['card_on_file'] = $token->get_token();
$params['card_on_file_require_cvv'] = false;
$params['card_on_file_require_exp_date'] = false;
unset( $params['recurring'] );

// Don't charge payment if zero amount.
if ( abs( $order->get_total() ) < 0.01 ) {
if ( wcs_cart_only_subscriptions() ) {
$result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token->get_token(), $params['button_text'] );

if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

if ( ! empty( $result['id'] ) ) {
update_post_meta( $order_id, 'reepay_session_id', $result['id'] );
}

if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

if ( wcs_cart_only_subscriptions() ) { // Cart contain only WC or Reepay subscriptions
try {
ReepayTokens::assign_payment_token( $order, $token );
ReepayTokens::save_card_info_to_order( $order, $token->get_token() );
} catch ( Exception $e ) {
$order->add_order_note( $e->getMessage() );
$this->create_recurring( $order, $data, $token->get_token());
}catch (Exception $e){
wc_add_notice( $e->getMessage(), 'error' );

return array(
'result' => 'failure',
Expand All @@ -938,7 +949,7 @@ public function process_payment( $order_id ) {
ReepayTokens::assign_payment_token( $order, $token );
ReepayTokens::save_card_info_to_order( $order, $token->get_token() );
} catch ( Exception $e ) {
$order->add_order_note( $e->getMessage() );
wc_add_notice( $e->getMessage(), 'error' );

return array(
'result' => 'failure',
Expand Down Expand Up @@ -1001,18 +1012,6 @@ public function process_payment( $order_id ) {
);
}

// "Save Card" flag
$order->update_meta_data( '_reepay_maybe_save_card', $maybe_save_card );
$order->save_meta_data();

if ( ! empty( $customer_handle ) && 0 === $order->get_customer_id() ) {
reepay()->api( $this )->request(
'PUT',
'https://api.reepay.com/v1/customer/' . $customer_handle,
$params['order']['customer']
);
}

$have_sub = (class_exists( WC_Reepay_Renewals::class ) && WC_Reepay_Renewals::is_order_contain_subscription( $order )) || wcs_cart_have_subscription();

$only_items_lines = array();
Expand All @@ -1032,18 +1031,17 @@ public function process_payment( $order_id ) {
// If here's Subscription or zero payment.
if ( ( $have_sub ) && ( abs( $order->get_total() ) < 0.01 || empty( $only_items_lines ) ) ) {

$result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, false, $params['button_text'] );

if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}
try {
$this->create_recurring( $order, $data );
}catch (Exception $e){
wc_add_notice( $e->getMessage(), 'error' );

if ( ! empty( $result['id'] ) ) {
update_post_meta( $order_id, 'reepay_session_id', $result['id'] );
return array(
'result' => 'failure',
'message' => $e->getMessage(),
);
}

do_action( 'reepay_instant_settle', $order );

$redirect = '#!reepay-checkout';

if ( ! empty( $result['url'] ) ) {
Expand All @@ -1064,6 +1062,35 @@ public function process_payment( $order_id ) {
return $this->process_session_charge( $params, $order );
}

public function create_recurring( $order, $data, $token = false ){
$result = reepay()->api( $this )->recurring( $this->payment_methods, $order, $data, $token );

if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

if ( ! empty( $result['id'] ) ) {
update_post_meta( $order->get_id(), 'reepay_session_id', $result['id'] );
}

if( $token ){
try {
ReepayTokens::assign_payment_token( $order, $token );
ReepayTokens::save_card_info_to_order( $order, $token->get_token() );
} catch ( Exception $e ) {
$order->add_order_note( $e->getMessage() );

return array(
'result' => 'failure',
'message' => $e->getMessage(),
);
}
}


do_action( 'reepay_instant_settle', $order );
}

/**
* Handle WooCommerce subscription changing or adding payment method.
*
Expand Down

0 comments on commit 3da9a11

Please sign in to comment.