Skip to content

Commit

Permalink
Merge pull request #335 from reepay/pre-1.7.7
Browse files Browse the repository at this point in the history
Update version 1.7.7
  • Loading branch information
ponddeja authored Jul 30, 2024
2 parents dcbc7bb + f9348a5 commit d2c659e
Show file tree
Hide file tree
Showing 10 changed files with 920 additions and 153 deletions.
10 changes: 9 additions & 1 deletion Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: billwerk+, visa, mastercard, dankort, mobilepay
Requires at least: 4.0
Tested up to: 6.5.3
Requires PHP: 7.4
Stable tag: 1.7.6
Stable tag: 1.7.7
License: GPL
License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html

Expand All @@ -18,6 +18,14 @@ The Billwerk+ Pay plugin extends WooCommerce allowing you to take payments on yo
See installation guide right here: https://docu.billwerk.plus/help/en/apps/woocommerce/setup-woocommerce-plugin.html

== Changelog ==
v 1.7.7 -
* [Fix] - Missing payment_method_reference data in the Billwerk+ customer_payment_method_added webhook could cause PHP fatal error.
* [Fix] - WooCommerce Subscriptions had issues with change of payment method where orders got payment authorized but were not automatically captured and set to complete.
* [Fix] - Instant capture didn't work for orders with discount.
* [Fix] - Amounts in order notes were wrong for "Failed to settle" notes and some captures.
* [Improvement] - A WordPress notice appears when the module starts to use another API key. This is because the subscriptions are defined in the Billwerk+ account, and the notice is only showed if the subscription module "Optimize" is installed.
* [Compatibility] - Billwerk+ Optimize version 1.2.7

v 1.7.6 -
* [Fix] - Allow the activation of Santander and enforce a redirect for this payment.
* [Compatibility] - Billwerk+ Optimize version 1.2.6
Expand Down
102 changes: 86 additions & 16 deletions includes/Functions/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Reepay\Checkout\Utils\TimeKeeper;
use WC_Reepay_Renewals;

defined( 'ABSPATH' ) || exit();

Expand Down Expand Up @@ -129,32 +130,101 @@ function rp_get_not_subs_order_by_handle( string $handle ) {
* Get order by reepay order session.
*
* @param string $session_id reepay order session.
* @param string $handle reepay order handle.
*
* @return false|WC_Order
*/
function rp_get_order_by_session( string $session_id ) {
$order_id = wp_cache_get( $session_id, 'reepay_order_by_session' );
function rp_get_order_by_session( string $session_id = null, string $handle = null ) {
if ( ! is_null( $session_id ) ) {
if ( rp_hpos_enabled() ) {
$orders = wc_get_orders(
array(
'limit' => 1,
'meta_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
array(
'key' => 'reepay_session_id',
'value' => $session_id,
),

if ( empty( $order_id ) ) {
$orders = wc_get_orders(
array(
'limit' => 1,
'meta_key' => 'reepay_session_id', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $session_id, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_compare' => '=',
)
);
),
)
);
} else {
$orders = wc_get_orders(
array(
'limit' => 1,
'meta_key' => 'reepay_session_id', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $session_id, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_compare' => '=',
)
);
}

if ( ! empty( $orders ) ) {
$order_id = reset( $orders )->get_id();
wp_cache_set( $session_id, $order_id, 'reepay_order_by_session' );
} else {
return false;
clean_post_cache( $order_id );
return wc_get_order( $order_id );
} elseif ( ! is_null( $handle ) ) {
$order = rp_get_order_by_customer( $handle );
return $order;
}

return false;
} elseif ( ! is_null( $handle ) ) {
$order = rp_get_order_by_customer( $handle );
return $order;
} else {
return false;
}
}
}

if ( ! function_exists( 'rp_get_order_by_customer' ) ) {
/**
* Get order by reepay order customer.
*
* @param string $customer_id reepay order customer.
*
* @return false|WC_Order
*/
function rp_get_order_by_customer( string $customer_id ) {
$reepay_list_invoice = reepay()->api( 'list-invoice' )->request( 'GET', "https://api.reepay.com/v1/list/invoice?customer=$customer_id" );

clean_post_cache( $order_id );
return $order_id ? wc_get_order( $order_id ) : false;
if ( is_wp_error( $reepay_list_invoice ) || empty( $reepay_list_invoice['content'] ) ) {
return '';
}
$subscription_order = null;
foreach ( $reepay_list_invoice['content'] as $content ) {
$search_string = strpos( $content['handle'], 'order-' );
if ( false !== $search_string ) {
$handle = $content['handle'];
$orders = wc_get_orders(
array(
'limit' => 1,
'meta_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
array(
'key' => '_reepay_order',
'value' => $handle,
),

),
)
);

if ( ! empty( $orders ) ) {
$order_id = reset( $orders )->get_id();
$order = wc_get_order( $order_id );
if ( class_exists( WC_Reepay_Renewals::class ) && WC_Reepay_Renewals::is_order_contain_subscription( $order ) || order_contains_subscription( $order ) ) {
$subscription_order = $order;
}
}
}
}
if ( null !== $subscription_order ) {
return $subscription_order;
} else {
return '';
}
}
}

Expand Down
71 changes: 64 additions & 7 deletions includes/Gateways/ReepayCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function init_form_fields() {
'private_key_test' => array(
'title' => __( 'Test Private Key', 'reepay-checkout-gateway' ),
'type' => 'text',
'description' => __( 'Insert your private key from your Billwerk+ Pay test account', 'reepay-checkout-gateway' ),
'description' => __( 'Insert your private key from your test account', 'reepay-checkout-gateway' ),
'default' => '',
),
'verify_key_test' => array(
Expand Down Expand Up @@ -613,18 +613,36 @@ public function admin_options() {
public function process_admin_options(): bool {
parent::process_admin_options();

$current_key = $this->private_key ?? '';
$woocommerce_reepay_checkout_private_key = isset( $_POST['woocommerce_reepay_checkout_private_key'] ) ? wc_clean( $_POST['woocommerce_reepay_checkout_private_key'] ) : '';

if ( $current_key !== $woocommerce_reepay_checkout_private_key ) {
Statistics::private_key_activated();
}
$current_key = $this->private_key ?? '';
$current_test_mode = $this->test_mode;
$woocommerce_reepay_checkout_private_key = isset( $_POST['woocommerce_reepay_checkout_private_key'] ) ? wc_clean( $_POST['woocommerce_reepay_checkout_private_key'] ) : '';
$woocommerce_reepay_checkout_private_key_test = isset( $_POST['woocommerce_reepay_checkout_private_key_test'] ) ? wc_clean( $_POST['woocommerce_reepay_checkout_private_key_test'] ) : '';
$woocommerce_reepay_checkout_test_mode = isset( $_POST['woocommerce_reepay_checkout_test_mode'] ) ? 'yes' : 'no';

$this->init_settings();
$this->private_key = $this->settings['private_key'] ?? $this->private_key;
$this->private_key_test = $this->settings['private_key_test'] ?? $this->private_key_test;
$this->test_mode = $this->settings['test_mode'] ?? $this->test_mode;

if ( $current_key !== $woocommerce_reepay_checkout_private_key ) {
Statistics::private_key_activated();
}

/**
* Condition check notic message.
*/
if ( is_plugin_active( 'reepay-subscriptions-for-woocommerce/reepay-subscriptions-for-woocommerce.php' ) ) {
if ( $current_test_mode !== $woocommerce_reepay_checkout_test_mode && $woocommerce_reepay_checkout_private_key !== $woocommerce_reepay_checkout_private_key_test ) {
if ( 'yes' === $this->test_mode ) {
add_action( 'woocommerce_update_options_checkout', array( $this, 'notice_message_test_mode_enabled' ) );
} else {
add_action( 'woocommerce_update_options_checkout', array( $this, 'notice_message_test_mode_disabled' ) );
}
} elseif ( $current_key !== $woocommerce_reepay_checkout_private_key ) {
add_action( 'woocommerce_update_options_checkout', array( $this, 'notice_message_live_key_changed' ) );
}
}

reepay()->reset_settings();

parent::is_webhook_configured();
Expand Down Expand Up @@ -774,4 +792,43 @@ public function get_localize_script_data(): array {
'error_text' => __( 'Error with payment, please try again', 'reepay-checkout-gateway' ),
);
}

/**
* Message notice live key changed
*/
public function notice_message_live_key_changed() {
// translators: notic message live key changed.
$notice_message = sprintf( __( 'The Api key identifies the Billwerk account. Only subscription plan handles that exist under the account of the API key can be used to submit subscription orders. <a href="%s" target="_blank">Read more about this here.</a>', 'reepay-checkout-gateway' ), 'https://optimize-docs.billwerk.com/reference/account' );
?>
<div class="notice notice-info">
<p><?php echo $notice_message; ?></p>
</div>
<?php
}

/**
* Message notice enabled test mode
*/
public function notice_message_test_mode_enabled() {
// translators: notice message enabled test mode.
$notice_message = sprintf( __( 'You just enabled test mode, meaning your test API key will now be used. Please note that all subscription products previously linked to plans on your live account are no longer linked. If you try to purchase a subscription product now, an error will occur. Disabling test mode will restore all connections. <a href="%s" target="_blank">Read more about this here.</a>', 'reepay-checkout-gateway' ), 'https://optimize-docs.billwerk.com/reference/account' );
?>
<div class="notice notice-info">
<p><?php echo $notice_message; ?></p>
</div>
<?php
}

/**
* Message notice disabled test mode
*/
public function notice_message_test_mode_disabled() {
// translators: notice message disabled test mode.
$notice_message = sprintf( __( 'You just disabled test mode, meaning your live API key will now be used. Please note that all subscription products previously linked to plans on your live account are now restored. If you haven\'t linked your subscription products with your test account, they will remain unlinked. <a href="%s" target="_blank">Read more about this here.</a>', 'reepay-checkout-gateway' ), 'https://optimize-docs.billwerk.com/reference/account' );
?>
<div class="notice notice-info">
<p><?php echo $notice_message; ?></p>
</div>
<?php
}
}
4 changes: 2 additions & 2 deletions includes/Gateways/ReepayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ public function get_order_items( WC_Order $order, bool $only_not_settled = false
$price = OrderCapture::get_item_price( $order_item, $order );

$tax_percent = $price['tax_percent'];
$unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $order_item->get_quantity(), 2 );
$unit_price = round( ( $prices_incl_tax ? $price['with_tax_and_discount'] : $price['original_with_discount'] ) / $order_item->get_quantity(), 2 );

if ( $only_not_settled && ! empty( $order_item->get_meta( 'settled' ) ) ) {
continue;
Expand All @@ -1453,7 +1453,7 @@ public function get_order_items( WC_Order $order, bool $only_not_settled = false

$tax_percent = $price['tax_percent'];

$unit_price = round( ( $prices_incl_tax ? $price['with_tax'] : $price['original'] ) / $item_shipping->get_quantity(), 2 );
$unit_price = round( ( $prices_incl_tax ? $price['with_tax_and_discount'] : $price['original_with_discount'] ) / $item_shipping->get_quantity(), 2 );

if ( $only_not_settled && ! empty( $item_shipping->get_meta( 'settled' ) ) ) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions includes/OrderFlow/OrderCapture.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ public static function get_item_price( $order_item, WC_Order $order ): array {
$tax = $price['with_tax'] - $price['original'];
$price['tax_percent'] = ( $tax > 0 && $price['original'] > 0 ) ? round( 100 / ( $price['original'] / $tax ) ) : 0;

$price['original'] += $discount;
$price['with_tax'] += $discount;
$price['original_with_discount'] = $price['original'] + $discount;
$price['with_tax_and_discount'] = $price['with_tax'] + $discount;

return $price;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/OrderFlow/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function process( array $data ) {
break;
case 'customer_payment_method_added':
if ( ! empty( $data['payment_method_reference'] ) ) {
$order = rp_get_order_by_session( $data['payment_method_reference'] );
$order = rp_get_order_by_session( $data['payment_method_reference'], $data['customer'] );
if ( $order && order_contains_subscription( $order ) ) {
WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );

Expand Down
Binary file modified languages/reepay-checkout-gateway-en_US.mo
Binary file not shown.
Loading

0 comments on commit d2c659e

Please sign in to comment.