Skip to content

Commit

Permalink
Send wc_ prefixed fields for express payments.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Dec 14, 2023
1 parent e5bca8d commit 7627f38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import validator from 'validator';
import { applyFilters } from '@wordpress/hooks';

const useExpressCheckoutProductHandler = ( api ) => {
const getAttributes = () => {
Expand Down Expand Up @@ -104,14 +105,23 @@ const useExpressCheckoutProductHandler = ( api ) => {
}

const addOnForm = document.querySelector( 'form.cart' );
let allowedFieldNames = applyFilters(
'wcpayPaymentRequestAllowedFieldNames',
[]
);
// Ensure allowedFieldNames is an array.
if ( ! Array.isArray( allowedFieldNames ) ) {
allowedFieldNames = [ allowedFieldNames ];
}

if ( addOnForm ) {
const formData = new FormData( addOnForm );

formData.forEach( ( value, name ) => {
if (
/^addon-/.test( name ) ||
/^wc_gc_giftcard_/.test( name )
/^wc_/.test( name ) ||
allowedFieldNames.includes( name )
) {
if ( /\[\]$/.test( name ) ) {
const fieldName = name.substring( 0, name.length - 2 );
Expand Down
15 changes: 13 additions & 2 deletions client/payment-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { doAction } from '@wordpress/hooks';
import { applyFilters, doAction } from '@wordpress/hooks';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -175,8 +175,19 @@ jQuery( ( $ ) => {

// Add addons data to the POST body
const formData = $( 'form.cart' ).serializeArray();
let allowedFieldNames = applyFilters(
'wcpayPaymentRequestAllowedFieldNames',
[]
);
// Ensure allowedFieldNames is an array.
if ( ! Array.isArray( allowedFieldNames ) ) {
allowedFieldNames = [ allowedFieldNames ];
}
$.each( formData, ( i, field ) => {
if ( /^addon-/.test( field.name ) ) {
if (
allowedFieldNames.includes( field.name ) ||
/^(addon-|wc_)/.test( field.name )
) {
if ( /\[\]$/.test( field.name ) ) {
const fieldName = field.name.substring(
0,
Expand Down

0 comments on commit 7627f38

Please sign in to comment.