Skip to content

Commit

Permalink
Merge pull request #246 from reepay/dev
Browse files Browse the repository at this point in the history
v1.4.68
  • Loading branch information
markusbrunke authored Jun 8, 2023
2 parents b0f70d1 + 213c318 commit 0c6a450
Show file tree
Hide file tree
Showing 46 changed files with 1,758 additions and 723 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 Reepay plugin extends WooCommerce allowing you to take payments on your stor
See installation guide right here: https://intercom.help/reepay/reepay-plugins/woocommerce-plugin

== Changelog ==
v 1.4.68 - Change order handle generation
v 1.4.67 - Fix bugs on checkout and thank you pages
v 1.4.66 - Choose send order lines or not
v 1.4.65 - Fix instant settle to full amount
Expand Down
2 changes: 1 addition & 1 deletion bin/phpcs.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

standard='--standard=./ruleset.xml'
path='./includes'
path='./includes ./templates'
extra='--cache --colors -p -s' #remove colors if your terminal doesn't support them

#extra+=' --report=diff -vvv' #uncomment for debug
Expand Down
15 changes: 15 additions & 0 deletions bin/phpcs.tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

standard='--standard=./ruleset.xml'
path='./tests'
extra='--cache --colors -p -s' #remove colors if your terminal doesn't support them

#extra+=' --report=diff -vvv' #uncomment for debug

if [ "$1" == "-full" ]; then
php ./vendor/bin/phpcs $standard $path $extra
elif [ "$1" == '-fix' ]; then
php ./vendor/bin/phpcbf $standard $path $extra
else
php ./vendor/bin/phpcs --report=summary $standard $path $extra
fi
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"license": "GPL",
"autoload": {
"psr-4": {
"Reepay\\Checkout\\": "includes/"
"Reepay\\Checkout\\": "includes/",
"Reepay\\Checkout\\Tests\\Helpers\\": "tests/helpers/",
"Reepay\\Checkout\\Tests\\Mocks\\": "tests/mocks/"
}
},
"authors": [
Expand Down
4 changes: 2 additions & 2 deletions includes/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,11 @@ public function settle( WC_Order $order, $amount = null, $items_data = false, $l
$settle_data = InstantSettle::calculate_instant_settle( $order );

if ( ! $amount ) {
$amount = $settle_data->settle_amount;
$amount = $settle_data['settle_amount'];
}

if ( ! $items_data ) {
$items_data = $settle_data->items;
$items_data = $settle_data['items'];
}
}

Expand Down
23 changes: 1 addition & 22 deletions includes/Functions/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@
* @return string
*/
function rp_get_customer_handle( int $user_id ): string {
// Allow to pay exist orders by guests.
// ToDo test this case.
if ( isset( $_GET['pay_for_order'], $_GET['key'] ) ) {
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
if ( $order_id ) {
$order = wc_get_order( $order_id );

// Get customer handle by order.
$gateway = rp_get_payment_method( $order );
$handle = reepay()->api( $gateway )->get_customer_handle( $order );
if ( $handle ) {
return $handle;
}
}
}

$handle = get_user_meta( $user_id, 'reepay_customer_id', true );

if ( empty( $handle ) ) {
Expand Down Expand Up @@ -64,12 +48,7 @@ function rp_get_userid_by_handle( string $handle ) {
'count_total' => false,
)
);
if ( count( $users ) > 0 ) {
$user = array_shift( $users );

return $user->ID;
}

return false;
return ! empty( $users ) ? array_shift( $users )->ID : false;
}
}
4 changes: 2 additions & 2 deletions includes/Functions/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function rp_get_order_handle( WC_Order $order, $unique = false ): ?string {

if ( empty( $handle ) ) {
$handle = $unique ?
'order-' . $order->get_order_number() . '-' . time() :
'order-' . $order->get_order_number();
'order-' . $order->get_id() . '-' . time() :
'order-' . $order->get_id();

$order->add_meta_data( '_reepay_order', $handle );

Expand Down
16 changes: 15 additions & 1 deletion includes/Gateways/ReepayCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,21 @@ public function payment_fields() {
*/
public function add_payment_method() {
$user = get_userdata( get_current_user_id() );
$customer_handle = rp_get_customer_handle( $user->ID );
$customer_handle = '';

// Allow to pay exist orders by guests.
if ( isset( $_GET['pay_for_order'], $_GET['key'] ) ) {
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
if ( $order_id ) {
$order = wc_get_order( $order_id );

// Get customer handle by order.
$gateway = rp_get_payment_method( $order );
$customer_handle = reepay()->api( $gateway )->get_customer_handle( $order );
}
} else {
$customer_handle = rp_get_customer_handle( $user->ID );
}

$accept_url = add_query_arg( 'action', 'reepay_card_store', admin_url( 'admin-ajax.php' ) );
$accept_url = apply_filters( 'woocommerce_reepay_payment_accept_url', $accept_url );
Expand Down
91 changes: 47 additions & 44 deletions includes/OrderFlow/InstantSettle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Reepay\Checkout\Gateways\ReepayGateway;
use stdClass;
use WC_Order;
use WC_Order_Item;
use WC_Order_Item_Fee;
use WC_Order_Item_Product;
use WC_Product;
Expand All @@ -31,28 +32,40 @@ class InstantSettle {
const SETTLE_RECURRING = 'recurring';
const SETTLE_FEE = 'fee';

/**
* OrderCapture instance
*
* @var OrderCapture
*/
private static OrderCapture $order_capture;

/**
* Constructor.
*/
public function __construct() {
add_action( 'reepay_instant_settle', array( $this, 'maybe_settle_instantly' ), 10, 1 );
}

/**
* Set order capture instance
*
* @TODO remove this static method and replace other static methods with non static methods
*
* @param OrderCapture $order_capture order capture class instance.
*/
public static function set_order_capture( OrderCapture $order_capture ) {
self::$order_capture = $order_capture;
}

/**
* Maybe settle instantly. Hooked to reepay_instant_settle.
*
* @param WC_Order $order order to settle payment.
*/
public function maybe_settle_instantly( WC_Order $order ) {
if ( ! is_object( $order ) ) {
$order = wc_get_order( $order );
if ( rp_is_order_paid_via_reepay( $order ) ) {
$this->process_instant_settle( $order );
}

if ( ! rp_is_order_paid_via_reepay( $order ) ) {
return;
}

$this->process_instant_settle( $order );
}

/**
Expand All @@ -63,8 +76,6 @@ public function maybe_settle_instantly( WC_Order $order ) {
* @return void
*/
public function process_instant_settle( WC_Order $order ) {
$order_capture = OrderCapture::get_instance();

if ( ! empty( $order->get_meta( '_is_instant_settled' ) ) ) {
return;
}
Expand All @@ -77,19 +88,18 @@ public function process_instant_settle( WC_Order $order ) {
if ( ! empty( $settle_items ) ) {
foreach ( $settle_items as $item ) {
if ( empty( $item->get_meta( 'settled' ) ) ) {
$item_data = $order_capture->get_item_data( $item, $order );
$item_data = self::$order_capture->get_item_data( $item, $order );
$total = $item_data['amount'] * $item_data['quantity'];

if ( $total <= 0 && method_exists( $item, 'get_product' ) && wcs_is_subscription_product( $item->get_product() ) ) {
WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );
} elseif ( $total > 0 && $order_capture->check_capture_allowed( $order ) ) {
} elseif ( $total > 0 && self::$order_capture->check_capture_allowed( $order ) ) {
$items_data[] = $item_data;
$total_all += $total;
}
}
}

$order_capture->settle_items( $order, $items_data, $total_all, $settle_items );
self::$order_capture->settle_items( $order, $items_data, $total_all, $settle_items );
$order->add_meta_data( '_is_instant_settled', '1' );
$order->save_meta_data();
}
Expand All @@ -99,12 +109,13 @@ public function process_instant_settle( WC_Order $order ) {
* Check if product can be settled instantly.
*
* @param WC_Product $product if need check the product.
* @param string[] $settle_types settle types.
*
* @return bool
* @see ReepayGateway::$settle
*/
public static function can_product_be_settled_instantly( WC_Product $product, array $settle_types ): bool {
public static function can_product_be_settled_instantly( WC_Product $product ): bool {
$settle_types = reepay()->get_setting( 'settle' ) ?: array();

if ( in_array( self::SETTLE_PHYSICAL, $settle_types, true ) &&
( ! wcs_is_subscription_product( $product ) &&
$product->needs_shipping() &&
Expand All @@ -130,10 +141,10 @@ public static function can_product_be_settled_instantly( WC_Product $product, ar
*
* @param WC_Order $order order to get items.
*
* @return array
* @return WC_Order_Item[]
*/
public static function get_instant_settle_items( WC_Order $order ): array {
$settle_types = rp_get_payment_method( $order )->settle ?: array();
$settle_types = reepay()->get_setting( 'settle' ) ?: array();
$items_data = array();

// Walk through the order lines and check if order item is virtual, downloadable, recurring or physical.
Expand All @@ -145,7 +156,7 @@ public static function get_instant_settle_items( WC_Order $order ): array {
*/
$product = $order_item->get_product();

if ( self::can_product_be_settled_instantly( $product, $settle_types ) ) {
if ( self::can_product_be_settled_instantly( $product ) ) {
$items_data[] = $order_item;
}
}
Expand All @@ -157,7 +168,7 @@ public static function get_instant_settle_items( WC_Order $order ): array {
}

if ( in_array( self::SETTLE_PHYSICAL, $settle_types, true ) ) {
foreach ( $order->get_items( 'shipping' ) as $i => $item_shipping ) {
foreach ( $order->get_shipping_methods() as $i => $item_shipping ) {
$items_data[ $i ] = $item_shipping;
}
}
Expand All @@ -170,18 +181,16 @@ public static function get_instant_settle_items( WC_Order $order ): array {
*
* @param WC_Order $order order to calculate settle amount.
*
* @return stdClass
* @return array
*/
public static function calculate_instant_settle( WC_Order $order ): stdClass {
$is_instant_settle = false;
$delivery = false;
$total = 0;
$items_data = array();
public static function calculate_instant_settle( WC_Order $order ): array {
$total = 0;
$items_data = array();

$settle_types = rp_get_payment_method( $order )->settle ?: array();
$settle_types = reepay()->get_setting( 'settle' ) ?: array();

// Walk through the order lines and check if order item is virtual, downloadable, recurring or physical.
foreach ( $order->get_items() as $item ) {
foreach ( $order->get_items() as $key => $item ) {
/**
* WC_Order_Item_Product returns not WC_Order_Item
*
Expand All @@ -190,10 +199,9 @@ public static function calculate_instant_settle( WC_Order $order ): stdClass {
$product = $item->get_product();
$price_incl_tax = $order->get_line_subtotal( $item, true, false );

if ( self::can_product_be_settled_instantly( $product, $settle_types ) ) {
$is_instant_settle = true;
$total += $price_incl_tax;
$items_data[] = OrderCapture::get_instance()->get_item_data( $item, $order );
if ( self::can_product_be_settled_instantly( $product ) ) {
$total += $price_incl_tax;
$items_data[ $key ] = self::$order_capture->get_item_data( $item, $order );
}
}

Expand All @@ -203,7 +211,6 @@ public static function calculate_instant_settle( WC_Order $order ): stdClass {
$shipping = (float) $order->get_shipping_total();
$tax = (float) $order->get_shipping_tax();
$total += ( $shipping + $tax );
$delivery = true;
}
}

Expand All @@ -218,21 +225,17 @@ public static function calculate_instant_settle( WC_Order $order ): stdClass {

// Add discounts.
if ( $order->get_total_discount( false ) > 0 ) {
$discount_with_tax = $order->get_total_discount( false );
$total -= $discount_with_tax;
$total -= $order->get_total_discount( false );

if ( $total < 0 ) {
$total = 0;
}
}

$result = new stdClass();
$result->is_instant_settle = $is_instant_settle || $delivery;
$result->settle_amount = $total;
$result->items = $items_data;
$result->settings = $settle_types;

return $result;
return array(
'settle_amount' => $total,
'items' => $items_data,
);
}

/**
Expand All @@ -247,9 +250,9 @@ public static function calculate_instant_settle( WC_Order $order ): stdClass {
public static function get_settled_items( WC_Order $order ): array {
$settled = array();

foreach ( $order->get_items() as $item ) {
foreach ( $order->get_items() as $key => $item ) {
if ( ! empty( $item->get_meta( 'settled' ) ) ) {
$settled[] = OrderCapture::get_instance()->get_item_data( $item, $order );
$settled[ $key ] = self::$order_capture->get_item_data( $item, $order );
}
}

Expand Down
4 changes: 3 additions & 1 deletion includes/OrderFlow/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class Main {
*/
public function __construct() {
new OrderStatuses();
OrderCapture::get_instance();

new InstantSettle();
InstantSettle::set_order_capture( new OrderCapture() );

new ThankyouPage();
new Webhook();
}
Expand Down
Loading

0 comments on commit 0c6a450

Please sign in to comment.