Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC - Centralized payment method definitions #10217

Open
wants to merge 69 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
3849078
Sorry for the big commit - first pass at getting the php side working…
brettshumaker Jan 15, 2025
888fcc6
We need to access this method outside of the definition.
brettshumaker Jan 15, 2025
66ab9a1
Update afterpay class to use new definitions.
brettshumaker Jan 15, 2025
ab7c4fb
Fix dark icon file checking.
brettshumaker Jan 15, 2025
845ecd6
Push generated types to client mapping
brettshumaker Jan 15, 2025
ae13734
Fix tests.
brettshumaker Jan 16, 2025
9d0cc13
Fixing the tests revealed that the stripe_id here should just be the …
brettshumaker Jan 16, 2025
c3c093c
Add note to Afterpay
brettshumaker Jan 16, 2025
b0aece2
Make get_stripe_id a consistent trait for the definitions.
brettshumaker Jan 16, 2025
b0d8884
Use existing constant for definition ID.
brettshumaker Jan 16, 2025
70ec9bd
Address notes from Marcin
brettshumaker Feb 3, 2025
2baec4b
Revert "Address notes from Marcin"
brettshumaker Feb 11, 2025
87e8f88
Rename classes to psr-4 and update references
brettshumaker Feb 11, 2025
ffe2a5d
Remove BNPL trait and definition.
brettshumaker Feb 11, 2025
136a012
Remove Payment_Method_Icons trait and add direct icon URL methods to …
brettshumaker Feb 11, 2025
45bc090
Remove Base_Payment_Method trait and move some of its functionality t…
brettshumaker Feb 11, 2025
9ae210b
Moved to static methods and updated all references to reflect that.
brettshumaker Feb 12, 2025
df36af2
white space clean up and use the "correct" id for the Affirm payment …
brettshumaker Feb 13, 2025
5258803
Update to definition registry, the definition registration process, a…
brettshumaker Feb 13, 2025
8fd22ab
Make a new CLI Commands class for implementing our CLI command and an…
brettshumaker Feb 13, 2025
d436df5
Fix PSR-4 naming inconsistencies in includes/payment-methods/Configs
brettshumaker Feb 13, 2025
22e3e4f
init payment definitions at run time
brettshumaker Feb 13, 2025
58e1feb
Make PaymentMethodDefinitionRegistry consistently have instance methods
brettshumaker Feb 14, 2025
bf2c18c
Rearrange method order
brettshumaker Feb 14, 2025
db37a11
Make is_reusable/bnpl/accept_only_domestic_payment part of the defini…
brettshumaker Feb 14, 2025
be7c41f
Merge remote-tracking branch 'origin/develop' into poc/centralized-pa…
brettshumaker Feb 14, 2025
76a18d2
Remove unused `use` statements
brettshumaker Feb 14, 2025
f3d88c0
Rename payment method definition method
brettshumaker Feb 14, 2025
7795dff
Add support for manual capture to definition
brettshumaker Feb 14, 2025
ff219f6
Add manual capture to the definition interface - missed this in the l…
brettshumaker Feb 14, 2025
a035b82
Remove unneeded npm command and associated file.
brettshumaker Feb 14, 2025
521b941
Simplify mapping class
brettshumaker Feb 17, 2025
fad6bde
Auto-generate icon components when generating types for payment methods.
brettshumaker Feb 17, 2025
174689d
Fix psalm linting
brettshumaker Feb 18, 2025
b4e30c1
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 18, 2025
c7b9b8e
Convert Klarna to new payment method definition
brettshumaker Feb 18, 2025
81f31e0
Add utility method for checking if a given currency is "domestic" for…
brettshumaker Feb 18, 2025
ceb5657
Remove unused method in definition.
brettshumaker Feb 18, 2025
dccdc2d
Fix js tests
brettshumaker Feb 18, 2025
8af7634
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 18, 2025
3069f05
Fix Klarna tests
brettshumaker Feb 18, 2025
ad0bc2e
Create poc-centralized-payment-method-definitions
brettshumaker Feb 18, 2025
ee07ebc
Merge branch 'develop' into poc/centralized-payment-method-definitions
frosso Feb 19, 2025
5e06321
Revert "Fix js tests"
brettshumaker Feb 19, 2025
b3cd1dc
Actually fix js tests
brettshumaker Feb 19, 2025
a16c8f3
Resolve (compiling) circular dependency.
brettshumaker Feb 19, 2025
7be446b
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 19, 2025
a52710f
Pass payment method config to inline script instead of generating
brettshumaker Feb 25, 2025
7ecea8a
Remove all code related to generating JS types for payment method def…
brettshumaker Feb 25, 2025
7e9f5e8
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 25, 2025
3b39e27
Fix FalsableReturnStatement error
brettshumaker Feb 26, 2025
2a9d6d1
Properly handle wp_json_encode
brettshumaker Feb 26, 2025
693f5ae
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 26, 2025
b1795e2
Really fix the psalm error - wasn't failing with npm run psalm
brettshumaker Feb 26, 2025
8aa160b
Merge branch 'poc/centralized-payment-method-definitions' of https://…
brettshumaker Feb 26, 2025
b55bca5
Fix jest config error - missed this when removing the js type generat…
brettshumaker Feb 26, 2025
6bcc283
Remove payment method definitions - will be re-added in individual PR…
brettshumaker Feb 26, 2025
10006d3
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 26, 2025
2a8ca73
Remove a few straggling references to the definitions I deleted.
brettshumaker Feb 26, 2025
78d4aa9
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 26, 2025
71d9650
Create tests for PaymentMethodDefinitionRegistry
brettshumaker Feb 26, 2025
df2d1fa
Create tests for PaymentMethodUtils
brettshumaker Feb 26, 2025
c9c83d9
Whoops!
brettshumaker Feb 27, 2025
13fec88
Remove accidental double encoding of JSON data.
brettshumaker Feb 27, 2025
43161c0
Use window instead of const just in case this ever gets added to the …
brettshumaker Feb 28, 2025
7850669
Restore Klarna payment method in WC_Payment_Gateway_WCPay_Test
brettshumaker Feb 28, 2025
186a0ac
Merge branch 'poc/centralized-payment-method-definitions' of https://…
brettshumaker Feb 28, 2025
5b86484
Use type parameter instead of casting
brettshumaker Feb 28, 2025
4f65ac7
Merge branch 'develop' into poc/centralized-payment-method-definitions
brettshumaker Feb 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/poc-centralized-payment-method-definitions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Add centralized payment method definitions to streamline implementation and maintenance of payment methods.
5 changes: 5 additions & 0 deletions client/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ declare global {
timeFormat: string;
};

const woopaymentsPaymentMethodDefinitions: Record<
string,
PaymentMethodDefinition
>;

const wc: {
wcSettings: typeof wcSettingsModule;
tracks: {
Expand Down
49 changes: 38 additions & 11 deletions client/payment-methods-map.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* External dependencies
*/
import React from 'react';
import { __ } from '@wordpress/i18n';
import classNames from 'classnames';

/**
* Internal dependencies
Expand Down Expand Up @@ -29,17 +31,41 @@ import {

const accountCountry = window.wcpaySettings?.accountStatus?.country || 'US';

export interface PaymentMethodMapEntry {
id: string;
label: string;
description: string;
icon: ReactImgFuncComponent;
currencies: string[];
stripe_key: string;
allows_manual_capture: boolean;
allows_pay_later: boolean;
accepts_only_domestic_payment: boolean;
}
import type { PaymentMethodMapEntry } from './types/payment-methods';

// Get any payment method definitions from the client.
const PaymentMethodDefinitions =
typeof woopaymentsPaymentMethodDefinitions !== 'undefined'
? woopaymentsPaymentMethodDefinitions
: {};

const convertedPaymentMethodDefinitions = Object.fromEntries<
PaymentMethodMapEntry
>(
Object.entries( PaymentMethodDefinitions ).map( ( [ key, value ] ) => [
key,
{
id: value.id,
label: value.title,
description: value.description,
icon: ( { className } ) => (
<img
src={ value.icon }
alt={ value.title }
className={ classNames(
'payment-method__icon',
className
) }
/>
),
currencies: value.currencies,
stripe_key: value.stripeId,
allows_manual_capture: value.allows_manual_capture,
allows_pay_later: value.allows_pay_later,
accepts_only_domestic_payment: value.accepts_only_domestic_payment,
},
] )
);

const PaymentMethodInformationObject: Record<
string,
Expand Down Expand Up @@ -292,6 +318,7 @@ const PaymentMethodInformationObject: Record<
allows_pay_later: false,
accepts_only_domestic_payment: false,
},
...convertedPaymentMethodDefinitions,
};

export default PaymentMethodInformationObject;
16 changes: 13 additions & 3 deletions client/types/payment-methods.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ export type PaymentMethod =
| 'ideal'
| 'p24'
| 'sepa_debit'
| 'sofort'
| 'affirm'
| 'afterpay_clearpay';
| 'sofort';

export interface PaymentMethodMapEntry {
id: string;
label: string;
description: string;
icon: ReactImgFuncComponent;
currencies: string[];
stripe_key: string;
allows_manual_capture: boolean;
allows_pay_later: boolean;
accepts_only_domestic_payment: boolean;
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"autoload": {
"psr-4": {
"WCPay\\MultiCurrency\\": "includes/multi-currency",
"WCPay\\PaymentMethods\\Configs\\": "includes/payment-methods/Configs",
"WCPay\\Vendor\\": "lib/packages",
"WCPay\\": "src"
},
Expand Down
15 changes: 15 additions & 0 deletions includes/admin/class-wc-payments-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WCPay\Database_Cache;
use WCPay\Logger;
use WCPay\WooPay\WooPay_Utilities;
use WCPay\PaymentMethods\Configs\Utils\PaymentMethodUtils;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -659,6 +660,13 @@ public function enqueue_payments_scripts() {
$this->get_js_settings()
);

$payment_method_definitions = rawurlencode( PaymentMethodUtils::get_payment_method_definitions_json() );
wp_add_inline_script(
'WCPAY_ADMIN_SETTINGS',
"window.woopaymentsPaymentMethodDefinitions = JSON.parse( decodeURIComponent( '" . esc_js( $payment_method_definitions ) . "' ) );",
'before'
);

// Output the settings JS and CSS only on the settings page.
wp_enqueue_script( 'WCPAY_ADMIN_SETTINGS' );
wp_enqueue_style( 'WCPAY_ADMIN_SETTINGS' );
Expand All @@ -684,6 +692,13 @@ public function enqueue_payments_scripts() {
$this->get_js_settings()
);

$payment_method_definitions = rawurlencode( PaymentMethodUtils::get_payment_method_definitions_json() );
wp_add_inline_script(
'WCPAY_DASH_APP',
"const woopaymentsPaymentMethodDefinitions = JSON.parse( decodeURIComponent( '" . esc_js( $payment_method_definitions ) . "' ) );",
'before'
);

wp_enqueue_script( 'WCPAY_DASH_APP' );
wp_enqueue_style( 'WCPAY_DASH_APP' );
}
Expand Down
13 changes: 13 additions & 0 deletions includes/class-duplicates-detection-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use WCPay\Payment_Methods\Sepa_Payment_Method;
use WCPay\Payment_Methods\Grabpay_Payment_Method;
use WCPay\Payment_Methods\Wechatpay_Payment_Method;
use WCPay\PaymentMethods\Configs\Registry\PaymentMethodDefinitionRegistry;

/**
* Class handling detection of payment methods enabled by multiple plugins simultaneously.
Expand Down Expand Up @@ -94,6 +95,8 @@ private function search_for_cc() {
* @return Duplicates_Detection_Service
*/
private function search_for_additional_payment_methods() {
// Get all payment method definitions.

$keywords = [
'bancontact' => Bancontact_Payment_Method::PAYMENT_METHOD_STRIPE_ID,
'sepa' => Sepa_Payment_Method::PAYMENT_METHOD_STRIPE_ID,
Expand All @@ -111,6 +114,16 @@ private function search_for_additional_payment_methods() {
'wechatpay' => Wechatpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID,
];

$payment_method_definitions = PaymentMethodDefinitionRegistry::instance()->get_all_payment_method_definitions();

// This gets all the registered payment method definitions. As new payment methods are converted from the legacy style, they need to be removed from the list above.
foreach ( $payment_method_definitions as $definition_class ) {
$definition_keywords = $definition_class::get_keywords();
foreach ( $definition_keywords as $keyword ) {
$keywords[ $keyword ] = $definition_class::get_id();
}
}

foreach ( $this->get_enabled_gateways() as $gateway ) {
foreach ( $keywords as $keyword => $payment_method ) {
if ( strpos( $gateway->id, $keyword ) !== false ) {
Expand Down
8 changes: 8 additions & 0 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
use WCPay\Payment_Methods\UPE_Payment_Method;
use WCPay\Payment_Methods\Grabpay_Payment_Method;
use WCPay\Payment_Methods\Wechatpay_Payment_Method;
use WCPay\PaymentMethods\Configs\Registry\PaymentMethodDefinitionRegistry;

/**
* Gateway class for WooPayments
Expand Down Expand Up @@ -3988,6 +3989,13 @@ public function get_upe_available_payment_methods() {
$available_methods[] = Grabpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID;
$available_methods[] = Wechatpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID;

// This gets all the registered payment method definitions. As new payment methods are converted from the legacy style, they need to be removed from the list above.
$payment_method_definitions = PaymentMethodDefinitionRegistry::instance()->get_all_payment_method_definitions();

foreach ( $payment_method_definitions as $definition_class ) {
$available_methods[] = $definition_class::get_id();
}

$available_methods = array_values(
apply_filters(
'wcpay_upe_available_payment_methods',
Expand Down
10 changes: 10 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use WCPay\Duplicates_Detection_Service;
use WCPay\Payment_Methods\Grabpay_Payment_Method;
use WCPay\WC_Payments_Currency_Manager;
use WCPay\PaymentMethods\Configs\Registry\PaymentMethodDefinitionRegistry;

/**
* Main class for the WooPayments extension. Its responsibility is to initialize the extension.
Expand Down Expand Up @@ -585,6 +586,15 @@ public static function init() {
Wechatpay_Payment_Method::class,
];

// Initialize and get payment method classes from the registry for those that have been converted.
$registry = PaymentMethodDefinitionRegistry::instance();
$registry->init();
$payment_method_definitions = $registry->get_all_payment_method_definitions();

foreach ( $payment_method_definitions as $definition_class ) {
$payment_method_classes[] = $definition_class::get_payment_method_class();
}

$payment_methods = [];
foreach ( $payment_method_classes as $payment_method_class ) {
$payment_method = new $payment_method_class( self::$token_service );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Payment Method Capability Constants
*
* @package WCPay\PaymentMethods\Configs\Constants
*/

namespace WCPay\PaymentMethods\Configs\Constants;

/**
* Class defining payment method capability constants.
*/
class PaymentMethodCapability {
/**
* Payment method can be saved and reused
*
* @var string
*/
public const TOKENIZATION = 'tokenization';

/**
* Payment method supports refunds
*
* @var string
*/
public const REFUNDS = 'refunds';

/**
* Payment method supports capturing payment later
*
* @var string
*/
public const CAPTURE_LATER = 'capture_later';

/**
* Payment method supports multiple currencies
*
* @var string
*/
public const MULTI_CURRENCY = 'multi_currency';

/**
* Payment method is a Buy Now Pay Later method
*
* @var string
*/
public const BUY_NOW_PAY_LATER = 'buy_now_pay_later';

/**
* Payment method only accepts domestic transactions
*
* @var string
*/
public const DOMESTIC_TRANSACTIONS_ONLY = 'domestic_transactions_only';
}
Loading
Loading