Skip to content

WooCommerce plugin: Developer reference ​

How Maytes for WooCommerce works on each request. Nothing here needs to be built: the plugin does it. Read this if you maintain the store, want to keep credentials out of the database, or need to hook into the payment.

To set the plugin up, follow the shop owner's guide.

How a payment flows ​

The order is paid only at step 7, when the capture succeeds. The order-received page at step 8 confirms the order; it doesn't trigger the payment. If the customer gets there before the capture, the page says the payment is being confirmed.

Installing without the plugin directory ​

Most stores install from Plugins → Add New Plugin. To install from a file or the command line instead, download maytes-for-woocommerce.zip:

text
Plugins → Add New Plugin → Upload Plugin
Choose maytes-for-woocommerce.zip → Install Now → Activate
bash
wp plugin install maytes-for-woocommerce --activate       # from the plugin directory
wp plugin install maytes-for-woocommerce.zip --activate   # from the zip

Requirements: WordPress 6.5 or later, WooCommerce 9.0 or later, PHP 7.4 or later. The plugin has no Composer dependencies.

Credentials in wp-config.php ​

The settings page is enough for most stores. To keep credentials out of the database, define them as constants.

Define any of these above the line /* That's all, stop editing! */. A constant always wins over the settings page, and its field shows a lock and the constant's name.

php
define( 'MAYTES_ENV',                       'sandbox' ); // 'production' when live
define( 'MAYTES_SANDBOX_CLIENT_ID',         '…' );
define( 'MAYTES_SANDBOX_CLIENT_SECRET',     '…' );
define( 'MAYTES_SANDBOX_WEBHOOK_SECRET',    '…' );
define( 'MAYTES_PRODUCTION_CLIENT_ID',      '…' );
define( 'MAYTES_PRODUCTION_CLIENT_SECRET',  '…' );
define( 'MAYTES_PRODUCTION_WEBHOOK_SECRET', '…' );

MAYTES_CLIENT_ID, MAYTES_CLIENT_SECRET and MAYTES_WEBHOOK_SECRET from version 1.1.0 still work: they apply to the environment in use.

The plugin's files ​

FileWhat it does
maytes-for-woocommerce.phpPlugin header, compatibility with the Checkout block and HPOS, loads the plugin
uninstall.phpRemoves the plugin's settings and cached tokens when you delete it. Orders keep their Maytes notes
includes/class-maytes-woo.phpBuilds the plugin's parts and hooks them in
includes/class-maytes-woo-gateway.phpThe payment method under WooCommerce → Settings → Payments, and its settings page
includes/class-maytes-woo-settings.phpReads each setting from the settings page, or from its wp-config.php constant
includes/class-maytes-woo-button.phpLoads the button on the checkout and cart, and prints the classic express rows
includes/class-maytes-woo-blocks.phpRegisters Split with Maytes with the Checkout and Cart blocks
includes/class-maytes-woo-checkout-route.phpPOST /wp-json/maytes/v1/checkout: checks the details, creates the order and the Maytes checkout
includes/class-maytes-woo-order-mapper.phpTurns the order into the Maytes checkout's line items
includes/class-maytes-woo-webhook-route.phpPOST /wp-json/maytes/v1/webhook: verifies each delivery and acts on it once
includes/class-maytes-woo-payment-handler.phpCaptures or releases the payment and sets the order status
includes/class-maytes-woo-api.phpCalls the Maytes API with the WordPress HTTP API, with the OAuth token
includes/class-maytes-woo-webhook-verifier.phpVerifies the webhook signature, ported from the Maytes PHP SDK
includes/class-maytes-woo-log.phpWrites to WooCommerce → Status → Logs
assets/js/maytes-blocks.jsThe payment method and the express button on the Checkout and Cart blocks
assets/js/maytes-classic.jsThe button in place of Place order and the express row on the classic checkout, and the row on the classic cart
assets/js/maytes-sheet.jsThe cart's short form for the customer's name, email and mobile
assets/css/maytes.cssThe button's layout on the checkout and cart, and the cart's form
assets/css/maytes-admin.css, assets/js/maytes-admin.jsThe settings page's layout and its copy button
assets/images/The Maytes logo and icon shown in WooCommerce admin
includes/class-maytes-woo-order-status-route.phpGET /wp-json/maytes/v1/orders/<id>/status: where an order stands, and the capture when webhooks are failing
includes/class-maytes-woo-webhook-health.phpReads Maytes' delivery log and checks the webhook route, for the settings page and the admin notice

How the plugin works ​

The button ​

The button script loads from https://js.maytes.co/v1/checkout-button.js with crossorigin="anonymous", only on the checkout and cart, and only when the store charges in AUD. The button matches the express buttons: block: true fills the row, height: 48 and radius: 6. mode stays at its default, popup; on phones the button opens Maytes in the same tab.

As the payment method, Split with Maytes is listed with your other payment methods. While it's chosen, its button takes the place of Place order, so the click opens the Maytes window just as the express button does. On WooCommerce versions whose Checkout block doesn't let a payment method replace Place order, and on the pay-for-order page, the usual Place order button sends the customer to Maytes in the same tab and back.

In the express row on the Checkout block, Split with Maytes is an express payment method. WooCommerce lays express buttons out in a grid; while Maytes is there, the wallets keep sharing their row and Maytes takes a full-width row after them. If you set a button height or corner radius in the Checkout block's express payment settings, Maytes uses them too.

In the express row on the classic checkout, the plugin prints an empty #maytes-express-row on the woocommerce_checkout_before_customer_details hook. The script moves it directly under Stripe's express element and renders the button, and moves it back each time WooCommerce refreshes the checkout (updated_checkout).

The classic WooCommerce checkout with Split with Maytes under the express buttons and above the "OR" separator

The classic checkout on the Storefront theme. The wallet buttons are placeholders for Stripe's express element.

Element on the classic checkoutWhat changes
#wc-stripe-express-checkout-element (Link, Apple Pay, Google Pay)Nothing. Stripe's iframe is never moved, because moving an iframe reloads it
#maytes-express-rowNew. Inserted directly after Stripe's element
#wc-stripe-express-checkout-button-separator ("— OR —")Nothing. It now sits under Maytes
Billing details, order review, card paymentNothing

On the cart, the button shows with the express buttons only when nothing in the cart ships, because Maytes doesn't collect an address. On the Cart block it's the same express payment method, hidden while the cart needs shipping; on the classic cart, the plugin prints #maytes-cart-row above Proceed to checkout on the woocommerce_proceed_to_checkout hook. It's the same Maytes button as on the checkout. If the store already has the customer's name, email and mobile (a logged-in customer, say), it opens Maytes straight away. Otherwise its click opens a short form for them, with its own Maytes button that opens Maytes once they're filled in. A cart that ships is sent to the checkout.

The button checks the customer's details first. Maytes doesn't return a name, email or address, and your orders need them for the confirmation email. If a required field is empty, the email is invalid or the terms box is unticked, the customer sees the checkout's usual error and no order is created: inline field errors on the Checkout block, WooCommerce's error list on the classic checkout. Wallets like Link skip this check because they supply the details.

On the classic checkout, a click submits the checkout form to WooCommerce's own checkout (?wc-ajax=checkout) with payment_method=maytes, exactly as Place order does. WooCommerce validates the form, creates the order and calls the gateway's process_payment(), so every rule your store and its other plugins apply to a card payment applies here too. On the Checkout block and the cart, which have no classic form, the button posts the customer's details to /wp-json/maytes/v1/checkout with a WordPress REST nonce and a plugin nonce. Either way it maps the reply to { checkoutId: checkout_uuid, checkoutUrl: checkout_url }, and opens that URL. On the Checkout block, closing the Maytes window hands the checkout back, with the customer's previous payment method selected.

Creating the checkout ​

This is the route the Checkout block and the cart use; the classic checkout goes through WooCommerce's own checkout and joins at step 5. POST /wp-json/maytes/v1/checkout builds the order with WooCommerce's own WC_Checkout::create_order(), so coupons, GST, shipping, booking dates and any other order data come out exactly as they would for a card payment.

  1. Load the cart. Return 400 if it's empty.
  2. Check the details: on the classic checkout, every required billing (and shipping) field, the email format and the terms box; on the Checkout block, which validates its own fields in the browser, the email; on the cart, the name, email and mobile. Return 422 with the messages if any fail. From the cart, a cart that needs shipping gets 409 with needs_checkout.
  3. Create the pending order, or reuse the one in the session's order_awaiting_payment, so a second click on the same cart doesn't create a second order.
  4. For a new order, fire woocommerce_checkout_order_processed, so booking and stock plugins react as they do for a card checkout.
  5. Create the Maytes checkout, with the order's billing name, email and phone as customer_data, so the Maytes checkout pre-fills them instead of asking again.
  6. Save checkout_uuid on the order as _maytes_checkout_uuid. Every uuid is kept, in case the customer opens two windows.
  7. Return checkout_uuid and checkout_url exactly as Maytes sent them.
php
$created = self::api()->create_checkout( array(
	'merchant_order_id' => (string) $order->get_id(),
	'total_amount'      => self::minor( $order->get_total() ), // cents
	'currency'          => $order->get_currency(),
	'return_url'        => $order->get_checkout_order_received_url(),
	'cancel_url'        => wc_get_checkout_url(),
	'items'             => $items, // category 'other' line items
	'customer_data'     => array( // only the ones the order has
		'name'   => 'Sam Mate',
		'email'  => $order->get_billing_email(),
		'mobile' => $order->get_billing_phone(),
	),
) );

The plugin calls the Maytes API with the WordPress HTTP API, so it needs no Composer packages. It gets an OAuth token from /oauth/token with your client credentials and caches it until shortly before it expires, so most checkouts and webhooks don't fetch one. If a call returns 401, it fetches a new one once. If Maytes returns 409 order_already_authorized, the customer is told the order is already being paid with Maytes.

How amounts are sent ​

Maytes rejects a checkout with 422 invalid_amount_arithmetic unless the line items add up to total_amount (Create a checkout). Coupons reduce each line's total, and prices that include GST carry their GST, so every line goes out at its final price in cents. For example, a $65 class with a $13 coupon:

WooCommerce lineAmountSent to Maytes
Watercolour class, 1 seat$65.00One item, unit_price 5200
Coupon SPRING20−$13.00Included in the item price
GST (included)$4.73Included in the item price
Order total$52.00total_amount 5200

Fees and shipping go out as their own items. If your store prices exclude tax, the tax goes out as its own item too. If rounding leaves the lines a cent away from the total, or a fee is negative, the plugin sends a single "<Your store> order #…" line for the exact total instead.

Confirming the payment ​

An order is paid when Maytes sends checkout.authorized and the plugin's capture succeeds. Capture must happen within 2 minutes, so the plugin captures straight away and leaves the emails to WooCommerce.

php
$event = Maytes_Woo_Webhook_Verifier::verify(
	$request->get_body(),                       // the raw bytes, never re-encoded JSON
	$request->get_header( 'x-maytes-signature' ),
	MAYTES_WEBHOOK_SECRET
); // throws Maytes_Woo_Webhook_Error: reply 400

self::api()->capture_checkout( $uuid, array(
	'expected_total_amount' => self::minor( $order->get_total() ),
	'currency'              => $order->get_currency(),
	'merchant_order_id'     => (string) $order->get_id(),
) );

$order->payment_complete( $uuid ); // your usual order emails go out

Each step leaves a note on the order, newest first:

WooCommerce order notes: Maytes checkout created, Payment via Split with Maytes, and Paid with Split with Maytes, each with the checkout id

The verifier is ported from the Maytes PHP SDK: it checks the signature over the raw body, accepts either signature during a secret rotation, compares in constant time and rejects deliveries more than 5 minutes old. The order is found by merchant_order_id, and the event's checkout_uuid must be one saved on that order. Processed event ids are kept for 4 days, because deliveries are at-least-once.

SituationWhat the plugin doesReply
Bad or stale signatureLogs the reason and stops400
Event id already handledNothing200
Unknown checkout, order already paid, or order no longer payableCancels the checkout, to release the customer's funds200
Your maytes_woo_can_fulfil filter returns falseCancels the checkout, and the order is marked Failed200
Capture succeedspayment_complete(), and an order note200
Capture returns 409 or 422The order is marked Failed200
Capture hits a network error or a 5xxNothing yet: Maytes retries inside the 2-minute window503
checkout.voided on an unpaid orderThe order is marked Failed, with the reason200
checkout.refundedRecords on the order any refund WooCommerce doesn't have yet, and adds a note200
checkout.refund.failedAdds a note with the amount and the reason200
Any other event, such as checkout.capturedNothing. WooCommerce orders are captured by the plugin, so Maytes never sends it for them200

When webhooks are blocked ​

If the store can't receive checkout.authorized, nothing would capture and the authorization would be voided after 2 minutes. The plugin has a second path:

  1. Creating a checkout returns a status_url for the order: GET /wp-json/maytes/v1/orders/<id>/status?key=<order key>. It answers paid, pending or failed, and needs the order key.
  2. While the Maytes popup is open, the checkout page calls it every 20 seconds, for up to 15 minutes, and for 45 seconds after the popup closes.
  3. The route reads Maytes' delivery log (GET /api/merchant/v1/webhooks/deliveries) at most every 20 seconds for the whole store. While the latest finished delivery failed, it also reads the order's checkout and captures an authorized one, as the webhook would.
  4. The webhook and this route share a per-order lock, so an order is never captured twice.

Once the order is paid, the route also returns the order received page, and the checkout page goes there at its next check while the popup stays open.

With webhooks working, the route answers from the order alone and makes no call to Maytes about it. In redirect mode, which the button uses on phones, the checkout page is gone, so this path doesn't run.

Last-minute checks ​

Use the maytes_woo_can_fulfil filter to release a payment before any money is taken, for example when a class has just filled up. It runs inside the webhook, after the authorization and before the capture, so keep it fast: deliveries time out after 15 seconds.

php
add_filter(
	'maytes_woo_can_fulfil',
	function ( bool $can_fulfil, WC_Order $order ): bool {
		return $can_fulfil && my_store_has_seats_for( $order );
	},
	10,
	2
);

Returning false cancels the Maytes checkout, so the customer's funds are released and nothing is captured.

This is the plugin's only hook, because WooCommerce's own cover the rest:

  • Before the payment starts, the plugin runs the checks your checkout runs for any payment method (woocommerce_check_cart_items, woocommerce_checkout_process, woocommerce_after_checkout_validation). Add an error there and no order or Maytes checkout is created. Run longer checks here.
  • After the capture, the order is paid like any other: use woocommerce_payment_complete, woocommerce_order_status_changed and woocommerce_order_refunded.

Related: Maytes for WooCommerce · Maytes Checkout Button · Create a checkout · Webhooks · Capture & checkout status