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

Expanding Store API Usage for ECE payments #3764

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
138 changes: 0 additions & 138 deletions client/api/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/* global Stripe */
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import {
getExpressCheckoutData,
getExpressCheckoutAjaxURL,
getRequiredFieldDataFromCheckoutForm,
} from 'wcstripe/express-checkout/utils';
import { getStripeServerData } from 'wcstripe/stripe-utils';
import { PAYMENT_METHOD_CASHAPP } from 'wcstripe/stripe-utils/constants';

Expand Down Expand Up @@ -450,136 +444,4 @@ export default class WCStripeAPI {
}
} );
}

/**
* Submits shipping address to get available shipping options
* from Express Checkout ECE payment method.
*
* @param {Object} shippingAddress Shipping details.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutECECalculateShippingOptions( shippingAddress ) {
return this.request(
getExpressCheckoutAjaxURL( 'get_shipping_options' ),
{
security: getExpressCheckoutData( 'nonce' )?.shipping,
is_product_page: getExpressCheckoutData( 'is_product_page' ),
...shippingAddress,
}
);
}

/**
* Updates cart with selected shipping option.
*
* @param {Object} shippingOption Shipping option.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutUpdateShippingDetails( shippingOption ) {
return this.request(
getExpressCheckoutAjaxURL( 'update_shipping_method' ),
{
security: getExpressCheckoutData( 'nonce' )?.update_shipping,
shipping_method: [ shippingOption.id ],
is_product_page: getExpressCheckoutData( 'is_product_page' ),
}
);
}

/**
* Get cart items and total amount.
*
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutGetCartDetails() {
return this.request( getExpressCheckoutAjaxURL( 'get_cart_details' ), {
security: getExpressCheckoutData( 'nonce' )?.get_cart_details,
} );
}

/**
* Creates order based on Express Checkout ECE payment method.
*
* @param {Object} paymentData Order data.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutECECreateOrder( paymentData ) {
return this.postToBlocksAPI( '/wc/store/v1/checkout', {
...getRequiredFieldDataFromCheckoutForm( paymentData ),
} );
}

/**
* Pays for an order based on the Express Checkout payment method.
*
* @param {number} order The order ID.
* @param {Object} paymentData Order data.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutECEPayForOrder( order, paymentData ) {
return this.postToBlocksAPI( `/wc/store/v1/checkout/${ order }`, {
...paymentData,
} );
}

/**
* Posts data to the Blocks API.
*
* @param {string} path The path to post to.
* @param {Object} data The data to post.
* @return {Promise} The promise for the request to the server.
*/
postToBlocksAPI( path, data ) {
return apiFetch( {
method: 'POST',
path,
headers: {
Nonce: getExpressCheckoutData( 'nonce' )?.wc_store_api,
},
data,
} );
}

/**
* Add product to cart from product page.
*
* @param {Object} productData Product data.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutAddToCart( productData ) {
return this.request( getExpressCheckoutAjaxURL( 'add_to_cart' ), {
security: getExpressCheckoutData( 'nonce' )?.add_to_cart,
...productData,
} );
}

/**
* Get selected product data from variable product page.
*
* @param {Object} productData Product data.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutGetSelectedProductData( productData ) {
return this.request(
getExpressCheckoutAjaxURL( 'get_selected_product_data' ),
{
security: getExpressCheckoutData( 'nonce' )
?.get_selected_product_data,
...productData,
}
);
}

/**
* Empty the cart.
*
* @param {number} bookingId Booking ID.
* @return {Promise} Promise for the request to the server.
*/
expressCheckoutEmptyCart( bookingId ) {
return this.request( getExpressCheckoutAjaxURL( 'clear_cart' ), {
security: getExpressCheckoutData( 'nonce' )?.clear_cart,
booking_id: bookingId,
} );
}
}
4 changes: 2 additions & 2 deletions client/blocks/express-checkout/express-checkout-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ const ExpressCheckoutComponent = ( {
} );

const onShippingAddressChange = ( event ) =>
shippingAddressChangeHandler( api, event, elements );
shippingAddressChangeHandler( event, elements );

const onShippingRateChange = ( event ) =>
shippingRateChangeHandler( api, event, elements );
shippingRateChangeHandler( event, elements );

const onElementsReady = ( event ) => {
const paymentMethodContainer = document.getElementById(
Expand Down
Loading
Loading