Payment use cases
This page explores different use cases and scenarios for implementing payment methods using the Adobe Commerce checkout starter kit.
For more general use cases, refer to use-cases.
Get order details from Adobe Commerce using the masked cart ID
The following steps demonstrate the payment flow:
The payment flow starts at the frontend. When checkout is completed, the frontend sends the masked cart ID to the payment gateway.
The payment gateway then sends a request to the App Builder application with the masked cart ID, as this is the only information it has about the order. This request could be a webhook or an event.
The App Builder application uses the Adobe Commerce HTTP Client to retrieve the order details using the masked cart ID. To facilitate this, the starter kit provides the method
getOrderByMaskedCartId
in the Adobe Commerce HTTP Client.
Validate payment info
To perform a headless checkout and payment, the Commerce instance must ensure that the payment has succeeded and the order can be placed.
Set payment method additional data
To ingest payment gateway specific information in the payment process, the checkout process must use the setPaymentMethodOnCart
mutation in combination with the payment_method.additional_data
field to persist the information required to validate the payment once the order is placed.
Copied to your clipboardsetPaymentMethodOnCart(input: {cart_id: $cartIdpayment_method: {code: $codeadditional_data: [{key: 'sessionId',value: '12A34B56-1A23-1234-A123-123456A78901',},{key: 'status',value: 'DONE',},]}}) {cart {selected_payment_method {codetitle}}}
Validate the payment with a webhook
With this additional information saved, you can configure an Adobe Commerce Webhook to validate the payment during order placement. This webhook triggers a synchronous call to your App Builder application, which is responsible for verifying payment details before the order is finalized.
The following example demonstrates how to add a webhook to the observer.sales_order_place_before
method:
PaaS only To register a webhook, modify the webhooks.xml
file and create a new webhook with the following configuration:
Copied to your clipboard<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_AdobeCommerceWebhooks:etc/webhooks.xsd"><method name="observer.sales_order_place_before" type="before"><hooks><batch name="out_of_process_payment_methods"><hook name="validate_payment"url="https://<your_app_builder>.adobeioruntime.net/api/v1/web/commerce-checkout-starter-kit/validate-payment"method="POST" timeout="20000" softTimeout="0" priority="100" required="true"fallbackErrorMessage="Error on validation"><fields><field name="payment_method" source="data.order.payment.method" /><field name="payment_additional_information" source="data.order.payment.additional_information" /></fields><rules><rule field="data.order.payment.method" operator="equal" value="<your_payment_method_code>" /></rules></hook></batch></hooks></method></config>
SaaS only For Adobe Commerce as a Cloud Service, you can create webhooks in the Admin.
Copied to your clipboardHook SettingsWebhook Method: observer.sales_order_place_beforeWebhook Type: beforeBatch Name: validate_paymentHook Name: oope_payment_methods_sales_order_place_beforeURL: https://<your_app_builder>.runtime.adobe.io/api/v1/web/commerce-checkout-starter-kit/validate-paymentActive: YesMethod: POSTHook FieldsName: payment_methodSource: data.order.payment.methodName: payment_additional_informationSource: data.order.payment.additional_informationHook RulesName: data.order.payment.methodValue: <your_payment_method_code>Operator: equal
To enhance security, enable webhook signature generation by following the webhooks signature verification instructions.
Refer to actions/validate-payment.js
for an example of how to validate the payment according to the payment gateway needs.
Filter out payment method
In some cases, you may want to filter out a payment method based on the cart details or the customer's information. For example, you may want to disable a payment method based on customer group or product attributes in the cart.
You can use the plugin.magento.out_of_process_payment_methods.api.payment_method_filter.get_list
webhook to filter out a payment method. This webhook is triggered every time the list of available payment methods is requested, allowing you to filter out the payment methods based on the cart details or customer information.
PaaS only The following example demonstrates how to add a webhook to the plugin.magento.out_of_process_payment_methods.api.payment_method_filter.get_list
method:
SaaS only SaaS webhooks have slightly different naming conventions. For this example, use the plugin.out_of_process_payment_methods.api.payment_method_filter.get_list
method.
Copied to your clipboard<method name="plugin.magento.out_of_process_payment_methods.api.payment_method_filter.get_list" type="after"><hooks><batch name="out_of_process_payment_methods"><hook name="payment_method_filter" url="https://<your_app_builder>.runtime.adobe.io/api/v1/web/commerce-checkout-starter-kit/filter-payment" method="POST" timeout="20000" softTimeout="0"><fields><field name="payload" /></fields></hook></batch></hooks></method>
Payload example:
Copied to your clipboard{"payload": {"cart": {"entity_id": "1","store_id": 1,"converted_at": null,"is_active": "1",..."items": [{"item_id": "4","quote_id": "1","product_id": "10","store_id": 1,"weight": "124.000000","qty": 2,"price": "600.0000","base_price": "600.0000",..."product": {"entity_id": "10",..."attributes": {"manufacturer": "Two","color": "Yellow","country_origin": "France",...}},},...],},"customer": {"entity_id": "1","website_id": "1","email": "test@example.com","group_id": "1",...}}}
You can find examples of how to filter out payment methods using customer data or product attributes in your App Builder application in actions/filter-payment.js
.