Skip to content

Commit

Permalink
Merge pull request #292 from reepay/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
markusbrunke authored Oct 27, 2023
2 parents 07abb2e + 5364c4a commit 70578bd
Show file tree
Hide file tree
Showing 43 changed files with 1,983 additions and 512 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.0 - Lots of updates and fixes
v 1.5.0 - Billwerk+ version update and thankyou changes
v 1.4.73 - Billwerk+ naming changes
v 1.4.72 - MS tokens saving and HPOS support
Expand Down
3 changes: 2 additions & 1 deletion assets/js/woo-blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
if (window.wc
&& window.wc.wcBlocksRegistry
&& window.React
&& window.wc_reepay) {
&& (window.wc_reepay || document.body.classList.contains('wp-admin'))) {

const PAYMENT_METHOD_NAME = (new URL(document.currentScript.src)).searchParams.get('name');

if (!PAYMENT_METHOD_NAME) {
Expand Down
21 changes: 17 additions & 4 deletions bin/phpunit-test-with-plugins.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
echo 'Plugins: woo'
export HPOS_ENABLED="no"

echo 'PHPUNIT_PLUGINS=woo'
echo 'HPOS_ENABLED=no'
export PHPUNIT_PLUGINS="woo"
php ./vendor/bin/phpunit

echo 'Plugins: woo,woo_subs'
echo 'PHPUNIT_PLUGINS=woo,woo_subs'
echo 'HPOS_ENABLED=no'
export PHPUNIT_PLUGINS="woo,woo_subs"
php ./vendor/bin/phpunit

echo 'Plugins: woo,rp_subs'
echo 'PHPUNIT_PLUGINS=woo,rp_subs'
echo 'HPOS_ENABLED=no'
export PHPUNIT_PLUGINS="woo,rp_subs"
php ./vendor/bin/phpunit

echo 'Plugins: woo,woo_subs,rp_sub'
echo 'PHPUNIT_PLUGINS=woo,woo_subs,rp_subs'
echo 'HPOS_ENABLED=no'
export PHPUNIT_PLUGINS="woo,woo_subs,rp_subs"
php ./vendor/bin/phpunit

export HPOS_ENABLED="yes"

echo 'PHPUNIT_PLUGINS=woo,woo_subs,rp_subs'
echo 'HPOS_ENABLED=yes'
export PHPUNIT_PLUGINS="woo,woo_subs,rp_subs"
php ./vendor/bin/phpunit
27 changes: 27 additions & 0 deletions includes/Actions/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Actions
*
* @package Reepay\Checkout
*/

namespace Reepay\Checkout\Actions;

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

/**
* Class Main
*
* @package Reepay\Checkout
*/
class Main {
/**
* Main constructor.
*/
public function __construct() {
if ( ! apply_filters( 'reepay_running_tests', false ) ) {
new ReepayCustomer();
new Subscriptions();
}
}
}
6 changes: 1 addition & 5 deletions includes/Actions/ReepayCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ public function user_register( int $user_id ) {
* @param int $user_id user id to set handle.
*/
public static function set_reepay_handle( int $user_id ): string {
try {
$customer = new WC_Customer( $user_id );
} catch ( Exception $e ) {
return '';
}
$customer = new WC_Customer( $user_id );

$email = $customer->get_email() ?: $customer->get_billing_email();

Expand Down
58 changes: 17 additions & 41 deletions includes/Actions/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ public function __construct() {
add_filter( 'wcs_renewal_order_created', array( $this, 'renewal_order_created' ), 10, 2 );

foreach ( self::PAYMENT_METHODS as $method ) {
add_action( 'woocommerce_thankyou_' . $method, array( $this, 'thankyou_page' ) );

// Update failing payment method.
add_action( 'woocommerce_subscription_failing_payment_method_updated_' . $method, array( $this, 'update_failing_payment_method' ), 10, 2 );

// Charge the payment when a subscription payment is due.
add_action( 'woocommerce_scheduled_subscription_payment_' . $method, array( $this, 'scheduled_subscription_payment' ), 10, 2 );

// Charge the payment when a subscription payment is due.
add_action( 'scheduled_subscription_payment_' . $method, array( $this, 'scheduled_subscription_payment' ), 10, 2 );
}

// Don't transfer customer meta to resubscribe orders.
Expand Down Expand Up @@ -119,11 +114,12 @@ public function add_subscription_card_id( int $order_id ) {
continue;
}

$token = ReepayTokens::get_payment_token_subscription( $subscription );
$token = ReepayTokens::get_payment_token_by_order( $subscription );

if ( ! $token ) {
// Copy tokens from parent order.
$order = wc_get_order( $order_id );
$token = ReepayTokens::get_payment_token_order( $order );
$token = ReepayTokens::get_payment_token_by_order( $order );

if ( $token ) {
ReepayTokens::assign_payment_token( $subscription, $token );
Expand All @@ -133,36 +129,24 @@ public function add_subscription_card_id( int $order_id ) {
}

/**
* Create a renewal order to record a scheduled subscription payment.
* Set _reepay_order meta to renewal order
*
* @param WC_Order|int $renewal_order new order.
* @param WC_Subscription|int $subscription new subscription.
* @param WC_Order $renewal_order new order.
* @param WC_Subscription|int $subscription new subscription.
*
* @return bool|WC_Order|WC_Order_Refund
* @return WC_Order
*/
public function renewal_order_created( $renewal_order, $subscription ) {
$renewal_order = wc_get_order( $renewal_order );
public function renewal_order_created( WC_Order $renewal_order, $subscription ): WC_Order {
if ( in_array( $subscription->get_payment_method(), self::PAYMENT_METHODS, true ) ) {
$renewal_order->delete_meta_data( '_reepay_order' );
$renewal_order->save();

if ( in_array( $renewal_order->get_payment_method(), self::PAYMENT_METHODS, true ) ) {
// Remove Reepay order handler from renewal order.
delete_post_meta( $renewal_order->get_id(), '_reepay_order' );
rp_get_order_handle( $renewal_order );
}

return $renewal_order;
}

/**
* Thank you page
*
* @param int $order_id order id of current order.
*
* @return void
* @throws Exception If invalid order token.
*/
public function thankyou_page( int $order_id ) {
self::add_subscription_card_id( $order_id );
}

/**
* Update the card meta for a subscription after using this payment method
* to complete a payment to make up for an automatic renewal payment which previously failed.
Expand Down Expand Up @@ -195,7 +179,6 @@ public function delete_resubscribe_meta( WC_Order $resubscribe_order ) {
delete_post_meta( $resubscribe_order->get_id(), '_payment_tokens' );
delete_post_meta( $resubscribe_order->get_id(), '_reepay_token' );
delete_post_meta( $resubscribe_order->get_id(), '_reepay_token_id' );
delete_post_meta( $resubscribe_order->get_id(), '_reepay_order' );
}
}

Expand Down Expand Up @@ -295,20 +278,21 @@ public function scheduled_subscription_payment( float $amount_to_charge, WC_Orde
$gateway->log( 'WCS process scheduled payment' );
// Lookup token.
try {
$token = ReepayTokens::get_payment_token_order( $renewal_order );
$token = ReepayTokens::get_payment_token_by_order( $renewal_order );
// Try to find token in parent orders.
if ( ! $token ) {
// Get Subscriptions.
$subscriptions = wcs_get_subscriptions_for_order( $renewal_order, array( 'order_type' => 'any' ) );
foreach ( $subscriptions as $subscription ) {
$token = ReepayTokens::get_payment_token_subscription( $subscription );
$token = ReepayTokens::get_payment_token_by_order( $subscription );

if ( ! $token ) {
$token = ReepayTokens::get_payment_token_order( $subscription->get_parent() );
$token = ReepayTokens::get_payment_token_by_order( $subscription->get_parent() );
}
}
}

// Failback: If token doesn't exist, but reepay token is here
// Fallback: If token doesn't exist, but reepay token is here
// We need that to provide woocommerce_subscription_payment_meta support
// See https://github.com/Prospress/woocommerce-subscriptions-importer-exporter#importing-payment-gateway-meta-data.
if ( ! $token ) {
Expand Down Expand Up @@ -361,14 +345,6 @@ public function scheduled_subscription_payment( float $amount_to_charge, WC_Orde

$gateway->log( sprintf( 'WCS token for schedule payment: %s', $token->get_token() ) );

// Fix the reepay order value to prevent "Invoice already settled".
$currently = get_post_meta( $renewal_order->get_id(), '_reepay_order', true );
$should_be = 'order-' . $renewal_order->get_id();
if ( $currently !== $should_be ) {
$renewal_order->update_meta_data( '_reepay_order', $should_be );
$renewal_order->save_meta_data();
}

// Charge payment.
$result = reepay()->api( $gateway )->charge(
$renewal_order,
Expand Down
74 changes: 37 additions & 37 deletions includes/Admin/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Reepay\Checkout\Admin;

use Automattic\WooCommerce\Utilities\OrderUtil;

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

/**
Expand All @@ -32,47 +34,45 @@ public function __construct() {
* @param string $hook current page hook.
*/
public function admin_enqueue_scripts( string $hook ) {
if ( 'post.php' !== $hook ) {
return;
}

$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( 'post.php' === $hook || rp_hpos_is_order_page() ) {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

wp_enqueue_style(
'wc-gateway-reepay-admin',
reepay()->get_setting( 'css_url' ) . 'admin' . $suffix . '.css',
array(),
reepay()->get_setting( 'plugin_version' )
);
wp_enqueue_style(
'wc-gateway-reepay-admin',
reepay()->get_setting( 'css_url' ) . 'admin' . $suffix . '.css',
array(),
reepay()->get_setting( 'plugin_version' )
);

wp_register_script(
'reepay-js-input-mask',
reepay()->get_setting( 'js_url' ) . 'jquery.inputmask' . $suffix . '.js',
array( 'jquery' ),
'5.0.3',
true
);

wp_enqueue_script(
'reepay-admin-js',
reepay()->get_setting( 'js_url' ) . 'admin' . $suffix . '.js',
array(
'jquery',
wp_register_script(
'reepay-js-input-mask',
),
reepay()->get_setting( 'plugin_version' ),
true
);
reepay()->get_setting( 'js_url' ) . 'jquery.inputmask' . $suffix . '.js',
array( 'jquery' ),
'5.0.3',
true
);

wp_enqueue_script(
'reepay-admin-js',
reepay()->get_setting( 'js_url' ) . 'admin' . $suffix . '.js',
array(
'jquery',
'reepay-js-input-mask',
),
reepay()->get_setting( 'plugin_version' ),
true
);

wp_localize_script(
'reepay-admin-js',
'Reepay_Admin',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'text_wait' => __( 'Please wait...', 'reepay-checkout-gateway' ),
'nonce' => wp_create_nonce( 'reepay' ),
)
);
wp_localize_script(
'reepay-admin-js',
'Reepay_Admin',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'text_wait' => __( 'Please wait...', 'reepay-checkout-gateway' ),
'nonce' => wp_create_nonce( 'reepay' ),
)
);
}
}
}

Loading

0 comments on commit 70578bd

Please sign in to comment.