Skip to content

Commit

Permalink
Merge pull request #298 from reepay/dev
Browse files Browse the repository at this point in the history
v 1.6.1 - Card saving fixes, user creation fixesDev
  • Loading branch information
markusbrunke authored Nov 17, 2023
2 parents 70578bd + d23aac4 commit 8b3060d
Show file tree
Hide file tree
Showing 22 changed files with 927 additions and 496 deletions.
1 change: 1 addition & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The Billwerk+ Payments plugin extends WooCommerce allowing you to take payments
See installation guide right here: https://docu.billwerk.plus/help/en/apps/woocommerce/setup-woocommerce-plugin.html

== Changelog ==
v 1.6.1 - Card saving fixes, user creation fixes
v 1.6.0 - Lots of updates and fixes
v 1.5.0 - Billwerk+ version update and thankyou changes
v 1.4.73 - Billwerk+ naming changes
Expand Down
48 changes: 48 additions & 0 deletions includes/Actions/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Checkout actions
*
* @package Reepay\Checkout
*/

namespace Reepay\Checkout\Actions;

/**
* Class Admin
*
* @package Reepay\Checkout
*/
class Admin {
/**
* Admin constructor.
*/
public function __construct() {
add_action( 'admin_notices', array( $this, 'admin_notice_api_action' ) );
}

/**
* Add notifications in admin for api actions.
*/
public function admin_notice_api_action() {
$error = get_transient( 'reepay_api_action_error' );
$success = get_transient( 'reepay_api_action_success' );

if ( ! empty( $error ) ) :
?>
<div class="error notice is-dismissible">
<p><?php echo esc_html( $error ); ?></p>
</div>
<?php
set_transient( 'reepay_api_action_error', null, 1 );
endif;

if ( ! empty( $success ) ) :
?>
<div class="notice-success notice is-dismissible">
<p><?php echo esc_html( $success ); ?></p>
</div>
<?php
set_transient( 'reepay_api_action_success', null, 1 );
endif;
}
}
60 changes: 60 additions & 0 deletions includes/Actions/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Checkout actions
*
* @package Reepay\Checkout
*/

namespace Reepay\Checkout\Actions;

use WC_Order;
use WC_Order_Item_Product;

/**
* Class Checkout
*
* @package Reepay\Checkout
*/
class Checkout {
/**
* Checkout constructor.
*/
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;
}

/**
* Count line item discount
*
* @param WC_Order_Item_Product $item created order item.
* @param string $cart_item_key order item key in cart.
* @param array $values values from cart item.
* @param WC_Order $order new order.
*
* @see WC_Checkout::create_order_line_items
*/
public function action_checkout_create_order_line_item( WC_Order_Item_Product $item, string $cart_item_key, array $values, WC_Order $order ) {
$line_discount = $values['line_subtotal'] - $values['line_total'];
$line_discount_tax = $values['line_subtotal_tax'] - $values['line_tax'];

$item->update_meta_data( '_line_discount', $line_discount + $line_discount_tax );
}
}
18 changes: 18 additions & 0 deletions includes/Actions/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@ class Main {
*/
public function __construct() {
if ( ! apply_filters( 'reepay_running_tests', false ) ) {
new Admin();
new Checkout();
new ReepayCustomer();
new Subscriptions();

add_filter( 'allowed_redirect_hosts', array( $this, 'add_allowed_redirect_hosts' ) );
}
}

/**
* Add reepay domains to allowed hosts list.
*
* @param array $hosts array of allowed hosts.
*
* @return mixed
*/
public function add_allowed_redirect_hosts( array $hosts ) {
$hosts[] = 'reepay.com';
$hosts[] = 'checkout.reepay.com';

return $hosts;
}
}
37 changes: 36 additions & 1 deletion includes/Actions/ReepayCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ 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 ) ) {
$email = $customer->get_email();
}

if ( empty( $email ) ) {
return '';
Expand All @@ -58,4 +65,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;
}
}
5 changes: 1 addition & 4 deletions includes/Actions/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ public function add_subscription_payment_meta( array $payment_meta, WC_Subscript
* @throws Exception If validation error.
*/
public function validate_subscription_payment_meta( string $payment_method_id, array $payment_meta, WC_Subscription $subscription ) {
if ( in_array( $payment_method_id, self::PAYMENT_METHODS, true ) ) {
if ( empty( $payment_meta['post_meta']['_reepay_token']['value'] ) ) {
throw new Exception( 'A "Billwerk+ Token" value is required.' );
}
if ( in_array( $payment_method_id, self::PAYMENT_METHODS, true ) && ! empty( $payment_meta['post_meta']['_reepay_token']['value'] ) ) {

$tokens = explode( ',', $payment_meta['post_meta']['_reepay_token']['value'] );
if ( count( $tokens ) > 1 ) {
Expand Down
24 changes: 13 additions & 11 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,24 +1080,20 @@ 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();
if ( empty( $handle ) ) {
$handle = get_user_meta( $order->get_customer_id(), 'reepay_customer_id', true );
return $handle;
}

if ( empty( $handle ) ) {
$handle = rp_get_customer_handle( $order->get_customer_id() );
}

if ( empty( $handle ) ) {
if ( $order->get_customer_id() > 0 ) {
$handle = 'customer-' . $order->get_customer_id();
Expand All @@ -1105,6 +1102,11 @@ public function get_customer_handle_by_order( $order ) {
}
}

if ( ReepayCustomer::have_same_handle( $order->get_customer_id(), $handle ) ) {
$handle = 'cust-' . time();
update_user_meta( $order->get_customer_id(), 'reepay_customer_id', $handle );
}

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

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
10 changes: 8 additions & 2 deletions includes/Gateways/ReepayCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,9 @@ public function payment_fields() {
}

/**
* Ajax: Finalize Payment
* Ajax: Finalize Payment. Used only to change the payment method
*
* @see ReepayGateway::change_payment_method
*
* @throws Exception If wrong request data.
*/
Expand Down Expand Up @@ -725,7 +727,11 @@ public function reepay_finalize() {
}
}

wp_safe_redirect( $this->get_return_url( $order ) );
$subscription = wcs_get_subscription( $order->get_id() );

$redirect_url = ! empty( $subscription ) ? $subscription->get_view_order_url() : $this->get_return_url( $order );

wp_safe_redirect( $redirect_url );
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
wp_safe_redirect( $this->get_return_url() );
Expand Down
Loading

0 comments on commit 8b3060d

Please sign in to comment.