mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Refactor to loop list of payment methods
This commit is contained in:
parent
5923aeb8b2
commit
907656ca88
2 changed files with 56 additions and 18 deletions
|
@ -23,6 +23,25 @@ return array(
|
||||||
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
'ppcp-local-apms.payment-methods' => static function( ContainerInterface $container): array {
|
||||||
|
return [
|
||||||
|
'bancontact' => array(
|
||||||
|
'id' => BancontactGateway::ID,
|
||||||
|
'country' => 'BE',
|
||||||
|
'currency' => 'EUR',
|
||||||
|
),
|
||||||
|
'blik' => array(
|
||||||
|
'id' => BlikGateway::ID,
|
||||||
|
'country' => 'PL',
|
||||||
|
'currency' => 'PLN',
|
||||||
|
),
|
||||||
|
'eps' => array(
|
||||||
|
'id' => EPSGateway::ID,
|
||||||
|
'country' => 'AT',
|
||||||
|
'currency' => 'EUR',
|
||||||
|
),
|
||||||
|
];
|
||||||
|
},
|
||||||
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
|
'ppcp-local-apms.bancontact.wc-gateway' => static function ( ContainerInterface $container ): BancontactGateway {
|
||||||
return new BancontactGateway(
|
return new BancontactGateway(
|
||||||
$container->get( 'api.endpoint.orders' ),
|
$container->get( 'api.endpoint.orders' ),
|
||||||
|
|
|
@ -47,9 +47,10 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
$methods[] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
|
$payment_methods = $c->get('ppcp-local-apms.payment-methods');
|
||||||
$methods[] = $c->get( 'ppcp-local-apms.blik.wc-gateway' );
|
foreach ($payment_methods as $key => $value) {
|
||||||
$methods[] = $c->get( 'ppcp-local-apms.eps.wc-gateway' );
|
$methods[] = $c->get( 'ppcp-local-apms.' . $key . '.wc-gateway' );
|
||||||
|
}
|
||||||
|
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
|
@ -71,14 +72,11 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
$customer_country = WC()->customer->get_billing_country() ?: WC()->customer->get_shipping_country();
|
$customer_country = WC()->customer->get_billing_country() ?: WC()->customer->get_shipping_country();
|
||||||
$site_currency = get_woocommerce_currency();
|
$site_currency = get_woocommerce_currency();
|
||||||
|
|
||||||
if ( $customer_country !== 'BE' || $site_currency !== 'EUR' ) {
|
$payment_methods = $c->get('ppcp-local-apms.payment-methods');
|
||||||
unset( $methods[ BancontactGateway::ID ] );
|
foreach ($payment_methods as $payment_method) {
|
||||||
}
|
if ( $customer_country !== $payment_method['country'] || $site_currency !== $payment_method['currency'] ) {
|
||||||
if ( $customer_country !== 'PL' || $site_currency !== 'PLN' ) {
|
unset( $methods[ $payment_method['id'] ] );
|
||||||
unset( $methods[ BlikGateway::ID ] );
|
}
|
||||||
}
|
|
||||||
if ( $customer_country !== 'AT' || $site_currency !== 'EUR' ) {
|
|
||||||
unset( $methods[ EPSGateway::ID ] );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,17 +87,20 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
add_action(
|
add_action(
|
||||||
'woocommerce_blocks_payment_method_type_registration',
|
'woocommerce_blocks_payment_method_type_registration',
|
||||||
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
|
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
|
||||||
$payment_method_registry->register( $c->get( 'ppcp-local-apms.bancontact.payment-method' ) );
|
$payment_methods = $c->get('ppcp-local-apms.payment-methods');
|
||||||
$payment_method_registry->register( $c->get( 'ppcp-local-apms.blik.payment-method' ) );
|
foreach ($payment_methods as $key => $value) {
|
||||||
$payment_method_registry->register( $c->get( 'ppcp-local-apms.eps.payment-method' ) );
|
$payment_method_registry->register( $c->get( 'ppcp-local-apms.' . $key . '.payment-method' ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
add_filter(
|
add_filter(
|
||||||
'woocommerce_paypal_payments_localized_script_data',
|
'woocommerce_paypal_payments_localized_script_data',
|
||||||
function ( array $data ) {
|
function ( array $data ) use ($c) {
|
||||||
|
$payment_methods = $c->get('ppcp-local-apms.payment-methods');
|
||||||
|
|
||||||
$default_disable_funding = $data['url_params']['disable-funding'] ?? '';
|
$default_disable_funding = $data['url_params']['disable-funding'] ?? '';
|
||||||
$disable_funding = array_merge( array( 'bancontact', 'blik', 'eps' ), array_filter( explode( ',', $default_disable_funding ) ) );
|
$disable_funding = array_merge( array_keys( $payment_methods ), array_filter( explode( ',', $default_disable_funding ) ) );
|
||||||
$data['url_params']['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
|
$data['url_params']['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
@ -113,7 +114,7 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
*
|
*
|
||||||
* @psalm-suppress MissingClosureParamType
|
* @psalm-suppress MissingClosureParamType
|
||||||
*/
|
*/
|
||||||
function( $order_id ) {
|
function( $order_id ) use($c) {
|
||||||
$order = wc_get_order( $order_id );
|
$order = wc_get_order( $order_id );
|
||||||
if ( ! $order instanceof WC_Order ) {
|
if ( ! $order instanceof WC_Order ) {
|
||||||
return;
|
return;
|
||||||
|
@ -124,8 +125,9 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
$order_key = wc_clean( wp_unslash( $_GET['key'] ?? '' ) );
|
$order_key = wc_clean( wp_unslash( $_GET['key'] ?? '' ) );
|
||||||
// phpcs:enable
|
// phpcs:enable
|
||||||
|
|
||||||
|
$payment_methods = $c->get('ppcp-local-apms.payment-methods');
|
||||||
if (
|
if (
|
||||||
! in_array( $order->get_payment_method(), array( BancontactGateway::ID, BlikGateway::ID, EPSGateway::ID ), true )
|
! $this->is_local_apm($order->get_payment_method(), $payment_methods)
|
||||||
|| ! $cancelled
|
|| ! $cancelled
|
||||||
|| $order->get_order_key() !== $order_key
|
|| $order->get_order_key() !== $order_key
|
||||||
) {
|
) {
|
||||||
|
@ -142,4 +144,21 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if given payment method is a local APM.
|
||||||
|
*
|
||||||
|
* @param string $selected_payment_method Selected payment method.
|
||||||
|
* @param array $payment_methods Available local APMs.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function is_local_apm(string $selected_payment_method, array $payment_methods): bool {
|
||||||
|
foreach ($payment_methods as $payment_method) {
|
||||||
|
if($payment_method['id'] === $selected_payment_method) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue