Skip to content

Commit

Permalink
release/4.6.0 (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerfinnell authored Oct 11, 2022
1 parent 1108e52 commit 78c9b72
Show file tree
Hide file tree
Showing 146 changed files with 8,318 additions and 1,850 deletions.
2,654 changes: 2,654 additions & 0 deletions data/tax-codes.json

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions includes/core/api/paymentintents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* API: PaymentIntents
*
* @package SimplePay\Core\API\PaymentIntents
* @copyright Copyright (c) 2022, Sandhills Development, LLC
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 4.6.0
*/

namespace SimplePay\Core\API\PaymentIntents;

use SimplePay\Core\Payments\Stripe_API;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Updates a PaymentIntent record.
*
* @since 4.6.0
*
* @param string $paymentintent_id ID of the PaymentIntent to update.
* @param array $paymentintent_args Data to update PaymentIntent with.
* @param array $api_request_args {
* Additional request arguments to send to the Stripe API when making a request.
*
* @type string $api_key API Secret Key to use.
* }
* @return \SimplePay\Vendor\Stripe\PaymentIntent $paymentintent Stripe PaymentIntent.
*/
function update( $paymentintent_id, $paymentintent_args, $api_request_args = array() ) {
return Stripe_API::request(
'PaymentIntent',
'update',
$paymentintent_id,
$paymentintent_args,
$api_request_args
);
}
13 changes: 11 additions & 2 deletions includes/core/assets/css/admin/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,9 @@
}
}

&__restrictions {
margin-left: 65px;
&__restrictions,
&__restrictions-ach {
margin-left: 72px;
}

&__icon {
Expand Down Expand Up @@ -1753,6 +1754,7 @@ select.simpay-currency-symbol {
}

.simpay-form-settings-notice {
font-weight: normal;
color: #1d2327;
position: relative;
margin: 18px 18px 0;
Expand Down Expand Up @@ -1796,6 +1798,13 @@ select.simpay-currency-symbol {
background: none;
}
}

// Add a border when output in settings pages (not a white background).
.simpay-settings & {

border: 2px solid #645aff;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/core/assets/css/simpay-admin.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion includes/core/assets/css/simpay-admin.min.css.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions includes/core/assets/js/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let spAdmin = {};
'_subscription_type',
'_subscription_custom_amount',
'_form_type',
'_tax_status',
'_enable_payment_page',
'.simpay-total-amount-label-recurring',
'.simpay-total-amount-label-tax',
Expand Down
1 change: 1 addition & 0 deletions includes/core/assets/js/admin/payment-form/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './general.js';
import './prices.js';
import './payment-methods.js';
import './taxes.js';
import './custom-fields.js';
import './stripe-checkout.js';
import './payment-page.js';
20 changes: 20 additions & 0 deletions includes/core/assets/js/admin/payment-form/taxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready';

/**
* Internal dependencies
*/
import { maybeBlockSelectWithUpgradeModal } from '@wpsimplepay/utils';

/**
* DOM ready.
*/
domReady( () => {
const selector = document.getElementById( '_tax_status_lite' );

if ( selector ) {
selector.addEventListener( 'change', maybeBlockSelectWithUpgradeModal );
}
} );
43 changes: 24 additions & 19 deletions includes/core/assets/js/frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import { default as hooks, doAction } from '@wpsimplepay/hooks';
import * as paymentForms from '@wpsimplepay/payment-forms';
import * as api from '@wpsimplepay/api';
import { default as legacyHelpers } from './utils/legacy.js';
import './payment-forms';

Expand All @@ -14,27 +13,10 @@ import './payment-forms';
*
* @todo Create automatically with Webpack.
*/

// Don't expose api.apiRequest to window.
const {
customers,
paymentintents,
sessions,
setupintents,
subscriptions,
} = api;

window.wpsp = {
hooks,
paymentForms,
initPaymentForm,
api: {
customers,
paymentintents,
sessions,
setupintents,
subscriptions,
},
};

/**
Expand Down Expand Up @@ -100,7 +82,11 @@ function initPaymentForm( $paymentForm, __unstableFormVars = false ) {
form: { prices, livemode, config = {} },
} = paymentFormData;

const { taxRates = [], paymentMethods = [] } = config;
const {
taxRates = [],
paymentMethods = [],
taxStatus = 'fixed-global',
} = config;

// Merge localized form data in to a semi-simplified object.
// Maintained for backwards compatibility.
Expand Down Expand Up @@ -150,6 +136,7 @@ function initPaymentForm( $paymentForm, __unstableFormVars = false ) {
} ),
paymentMethod: _.first( paymentMethods ),
taxRates,
taxStatus,
paymentMethods,
livemode,
displayType: formData.formDisplayType,
Expand All @@ -163,6 +150,24 @@ function initPaymentForm( $paymentForm, __unstableFormVars = false ) {
};
};

// Attach a helper to get the form data/state.
$paymentForm.getFormData = function () {
const _formData = {
...$paymentForm.__unstableLegacyFormData,
...$paymentForm.state,
};

// Remove additional data that is not needed and may trigger WAF rules.
const {
order: _o,
customer: _cus,
paymentMethods: _pms,
...cleanFormData // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#rest_property
} = _formData;

return JSON.stringify( cleanFormData );
};

// Attach a Stripe instance to the Payment Form jQuery object.
const {
key: publishableKey,
Expand Down
2 changes: 1 addition & 1 deletion includes/core/assets/js/frontend/payment-forms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function submitPaymentForm( paymentForm ) {
}
);

submit().catch( onError );
submit();
} catch ( error ) {
onError( error );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,44 @@ async function submit( paymentForm ) {

// Only generate a custom Customer if we need to map on-page form fields.
if ( hasCustomerFields ) {
const customerData = await customers
.create( {}, paymentForm )
.catch( onError );

if ( ! customerData ) {
return;
}

const {
customer: { id },
} = await customers.create( {}, paymentForm );
} = customerData;

customerId = id;
}

// Generate a Checkout Session.
const session = await sessions
.create(
{
customer_id: customerId,
payment_method_type: __unstableLegacyFormData.paymentMethods
? __unstableLegacyFormData.paymentMethods[ 0 ].id
: 'card',
},
paymentForm
)
.catch( onError );

// Bail if there was an error.
if ( ! session ) {
return;
}

const {
sessionId,
session: { url },
redirect_type: redirectType,
} = await sessions.create(
{
customer_id: customerId,
},
paymentForm
);
} = session;

// Redirect to Stripe.
if ( 'stripe' === redirectType ) {
Expand Down
7 changes: 2 additions & 5 deletions includes/core/assets/js/packages/api/src/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import { apiRequest } from './api-request.js';
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export function create( data = {}, paymentForm ) {
const { id, state, __unstableLegacyFormData } = paymentForm;
const { id, getFormData } = paymentForm;

return apiRequest( 'v2/customer', {
form_values: serialize( paymentForm[ 0 ], { hash: true } ),
form_data: JSON.stringify( {
...__unstableLegacyFormData,
...state,
} ),
form_data: getFormData(),
form_id: id,
...data,
} );
Expand Down
1 change: 1 addition & 0 deletions includes/core/assets/js/packages/api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './api-request.js';

export * as customers from './customers.js';
export * as paymentintents from './paymentintents.js';
export * as orders from './orders.js';
export * as sessions from './sessions.js';
export * as setupintents from './setupintents.js';
export * as subscriptions from './subscriptions.js';
55 changes: 55 additions & 0 deletions includes/core/assets/js/packages/api/src/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* "Order" refers to an internal order, not a Stripe Order object.
*/

/**
* Internal dependencies.
*/
import { apiRequest } from './api-request.js';
import { createToken } from '../../../frontend/utils/recaptcha.js';

/** @typedef {import('@wpsimplepay/payment-forms').PaymentForm} PaymentForm */

/**
* Creates and returns an order preview.
*
* @since 4.6.0
*
* @param {Object} data Data to pass to REST endpoint.
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export async function preview( data ) {
// Create a token for the reCAPTCHA.
const recaptcha = await createToken(
`simple_pay_form_${ data.form_id }_order_preview`
);

return apiRequest( 'v2/order/preview', {
...data,
captcha: {
recaptcha,
},
} );
}

/**
* Submits an Order.
*
* @since 4.6.0
*
* @param {Object} data Data to pass to REST endpoint.
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export async function submit( data ) {
// Create a token for the reCAPTCHA.
const recaptcha = await createToken(
`simple_pay_form_${ data.form_id }_order_submit`
);

return apiRequest( 'v2/order/submit', {
...data,
captcha: {
recaptcha,
},
} );
}
7 changes: 2 additions & 5 deletions includes/core/assets/js/packages/api/src/paymentintents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import { apiRequest } from './api-request.js';
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export function create( data = {}, paymentForm ) {
const { id, state, __unstableLegacyFormData } = paymentForm;
const { id, getFormData } = paymentForm;

return apiRequest( 'v2/paymentintent/create', {
form_values: serialize( paymentForm[ 0 ], { hash: true } ),
form_data: JSON.stringify( {
...__unstableLegacyFormData,
...state,
} ),
form_data: getFormData(),
form_id: id,
...data,
} );
Expand Down
7 changes: 2 additions & 5 deletions includes/core/assets/js/packages/api/src/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import { apiRequest } from './api-request.js';
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export function create( data = {}, paymentForm ) {
const { id, state, __unstableLegacyFormData } = paymentForm;
const { id, getFormData } = paymentForm;

return apiRequest( 'v2/checkout-session', {
form_values: serialize( paymentForm[ 0 ], { hash: true } ),
form_data: JSON.stringify( {
...__unstableLegacyFormData,
...state,
} ),
form_data: getFormData(),
form_id: id,
...data,
} );
Expand Down
4 changes: 2 additions & 2 deletions includes/core/assets/js/packages/api/src/setupintents.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { apiRequest } from './api-request.js';
* @return {jqXHR} jQuery XMLHttpRequest object.
*/
export function create( data = {}, paymentForm ) {
const { id, state } = paymentForm;
const { id, getFormData } = paymentForm;

return apiRequest( 'v2/setupintent/create', {
form_values: serialize( paymentForm[ 0 ], { hash: true } ),
form_data: JSON.stringify( state ),
form_data: getFormData(),
form_id: id,
...data,
} );
Expand Down
Loading

0 comments on commit 78c9b72

Please sign in to comment.