Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimaspolohov #295

Merged
merged 10 commits into from
Nov 15, 2023
18 changes: 18 additions & 0 deletions includes/Actions/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ class Checkout {
*/
public function __construct() {
add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'action_checkout_create_order_line_item' ), 10, 4 );
add_filter( 'woocommerce_cart_needs_payment', array( $this, 'check_need_payment' ), 10 );
}

/**
* Count line item discount
*
* @param bool $need_payment need payment marker.
*
* @see WC_Cart::needs_payment
*
* @return bool
*/
public function check_need_payment( bool $need_payment ): bool {
if ( wcs_cart_have_subscription() ) {
return true;
}

return $need_payment;
}

/**
Expand Down
33 changes: 32 additions & 1 deletion includes/Actions/ReepayCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function user_register( int $user_id ) {
public static function set_reepay_handle( int $user_id ): string {
$customer = new WC_Customer( $user_id );

$email = $customer->get_email() ?: $customer->get_billing_email();
$email = $customer->get_billing_email();
if(empty( $email ) && !empty( $_POST['billing_email'] )){
$email = $_POST['billing_email'];
}

if ( empty( $email ) ) {
return '';
Expand All @@ -58,4 +61,32 @@ public static function set_reepay_handle( int $user_id ): string {

return $customer_handle;
}

/**
* Check exist customer in reepay with same handle but another data
*
* @param int $user_id user id to set handle.
* @param string $handle user id to set handle.
*/
public static function have_same_handle( int $user_id, string $handle ): bool {
$user_reepay = reepay()->api( 'reepay_user_register' )->request(
'GET',
'https://api.reepay.com/v1/customer/' . $handle,
);

if( !empty( $user_reepay ) && !is_wp_error( $user_reepay ) ){
$customer = new WC_Customer( $user_id );
$email = $customer->get_billing_email();

if(empty( $email ) && !empty( $_POST['billing_email'] )){
$email = $_POST['billing_email'];
}

if(!empty($email) && $user_reepay['email'] !== $email){
return true;
}
}

return false;
}
}
24 changes: 12 additions & 12 deletions includes/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Reepay\Checkout\Gateways\ReepayGateway;
use Reepay\Checkout\OrderFlow\InstantSettle;
use Reepay\Checkout\OrderFlow\OrderCapture;
use Reepay\Checkout\Actions\ReepayCustomer;
use WC_Order;
use Reepay\Checkout\OrderFlow\OrderStatuses;
use WC_Order_Item;
Expand Down Expand Up @@ -1079,22 +1080,13 @@ public function get_customer_handle_by_order( $order ) {
$order = wc_get_order( $order );

$handle = $this->get_customer_handle( $order );
if ( ! empty( $handle ) ) {
return $handle;
}

if ( $order->get_customer_id() === 0 ) {
if ( $order->get_customer_id() === 0 && empty( $handle )) {
$handle = $order->get_meta( '_reepay_customer' );
if ( ! empty( $handle ) ) {
return $handle;
}
}

$handle = rp_get_customer_handle( $order->get_customer_id() );
if ( ! empty( $handle ) ) {
$order->add_meta_data( '_reepay_customer', $handle );
$order->save_meta_data();
return $handle;
if( empty( $handle ) ){
$handle = rp_get_customer_handle( $order->get_customer_id() );
}

if ( empty( $handle ) ) {
Expand All @@ -1105,6 +1097,14 @@ public function get_customer_handle_by_order( $order ) {
}
}

if( ReepayCustomer::have_same_handle( $order->get_customer_id(), $handle ) ){
$handle = 'cust-' . time();
$order->add_meta_data( '_reepay_customer', $handle );
$order->save_meta_data();

return $this->get_customer_handle_by_order( $order );
}

$order->add_meta_data( '_reepay_customer', $handle );
$order->save_meta_data();

Expand Down
1 change: 1 addition & 0 deletions includes/Functions/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function rp_get_customer_handle( int $user_id ): string {
}
}


return $handle;
}
}
Expand Down
5 changes: 4 additions & 1 deletion includes/Functions/hpos.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
* @return bool
*/
function rp_hpos_enabled(): bool {
return OrderUtil::custom_orders_table_usage_is_enabled();
if(class_exists(OrderUtil::class)){
return OrderUtil::custom_orders_table_usage_is_enabled();
}
return false;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions includes/Gateways/ReepayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,18 @@ public function process_payment( $order_id ) {
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message(), $result->get_error_code() );
}

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(),
);
}
}

$order->payment_complete();
Expand Down
Loading