Check gift card balance
When a shopper is checking out and enters a gift card code to check its balance, a third-party system is used to get the current balance value.
This use case runs with a webhook of type after
.
Webhook name
PaaS only plugin.magento.gift_card_account.api.gift_card_account_management.check_gift_card
SaaS only plugin.gift_card_account.api.gift_card_account_management.check_gift_card
Payloads
Default payload
Configured payload
Copied to your clipboard{"cartId": "int","giftCardCode": "string","result": "mixed"}
Copied to your clipboard{"giftCardCode": "string"}
Configuration
webhook.xml (PaaS)
Copied to your clipboard<method name="plugin.magento.gift_card_account.api.gift_card_account_management.check_gift_card" type="after"><hooks><batch name="check_gift_card"><hook name="get_balance" url="{env:APP_BUILDER_URL}/get-gift-card-balance" timeout="5000" softTimeout="1000" fallbackErrorMessage="Could not get the gift card balance" required="true"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><fields><field name="giftCardCode"/></fields></hook></batch></hooks></method>
Admin (SaaS)
Copied to your clipboardHook SettingsWebhook method: plugin.gift_card_account.api.gift_card_account_management.check_gift_cardWebhook type: afterBatch name: check_gift_cardHook name: get_balanceURL: <Host>/get-gift-card-balanceTimeout: 5000Soft timeout: 1000Fallback Error Message: Could not get the gift card balanceRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: giftCardCodeSource: giftCardCodeActive: Yes
Copied to your clipboard<method name="plugin.magento.gift_card_account.api.gift_card_account_management.check_gift_card" type="after"><hooks><batch name="check_gift_card"><hook name="get_balance" url="{env:APP_BUILDER_URL}/get-gift-card-balance" timeout="5000" softTimeout="1000" fallbackErrorMessage="Could not get the gift card balance" required="true"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><fields><field name="giftCardCode"/></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: plugin.gift_card_account.api.gift_card_account_management.check_gift_cardWebhook type: afterBatch name: check_gift_cardHook name: get_balanceURL: <Host>/get-gift-card-balanceTimeout: 5000Soft timeout: 1000Fallback Error Message: Could not get the gift card balanceRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: giftCardCodeSource: giftCardCodeActive: Yes
Endpoint code example
The following code example shows how to implement the webhook on your custom endpoint.
Copied to your clipboardconst fetch = require('node-fetch')const { Core } = require('@adobe/aio-sdk')const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')// main function that will be executed by Adobe I/O Runtimeasync function main (params) {// create a Loggerconst logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })try {// 'info' is the default level if not setlogger.info('Calling the main action')// log parameters, only if params.LOG_LEVEL === 'debug'logger.debug(stringParameters(params))//check for missing request input parameters and headersconst requiredParams = [/* add required params */]const requiredHeaders = ['Authorization']const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)if (errorMessage) {// return and log client errorsreturn errorResponse(400, errorMessage, logger)}// Place the real call to the 3rd party endpoint here.// Just for demo purposes, the gift card balance is replaced by 500 if the gift card code does not end with '001'const response = {statusCode: 200}if (!params.giftCardCode.endsWith('001')) {response.body = JSON.stringify({op: 'replace',path: 'result',value: 500})} else {response.body = JSON.stringify({op: 'success'})}return response} catch (error) {// log any server errorslogger.error(error)// return with 500return errorResponse(500, 'server error', logger)}}exports.main = main