Add configuration to the payments function
You can use metafields for the configuration of your payment customization function. Metafields provide greater flexibility to use functions, and are a prerequisite to creating a merchant user interface for configuring functions.
What you'll learn
Anchor link to section titled "What you'll learn"In this tutorial, you'll learn how to do the following tasks:
- Define what configuration settings will be surfaced to merchants.
- Read and use the merchant-defined values in your function.
Requirements
Anchor link to section titled "Requirements"- You've completed the Getting started with building payment customizations tutorial.
Step 1: Configure the function
Anchor link to section titled "Step 1: Configure the function"To make your function reusable, you can replace hardcoded values in your function with metafield values. You can update your input query to request a metafield value on the created payment customization, which is the function owner for this function API. You can then use that value in your function logic.
Navigate to your function in
extensions/payment-customization
.Replace the code in the
src/run.graphql
file with the following code.This update to the input query adds a metafield from the
paymentCustomization
object, which is the function owner.The query differs slightly in Rust and JavaScript due to code generation requirements.
If you're using JavaScript, then run the following command to regenerate types based on your input query:
Replace the
src/run.rs
orsrc/run.js
file with the following code.This update includes parsing the JSON metafield value, and using values from that JSON in the function logic instead of hardcoded values.
This change is automatically reflected as long as you're running
dev
.
Step 2: Populate the payment customization configuration metafield
Anchor link to section titled "Step 2: Populate the payment customization configuration metafield"To populate the configuration metafield, you'll first use the paymentCustomizations
query to confirm the payment customization ID, and then use the metafieldsSet
mutation to populate the same metafield that you specified in the input query.
- Open the Shopify GraphiQL app on your development store.
- In the GraphiQL app, in the API Version field, select the 2023-07 version.
Execute the following query, and make note of the
id
value of the payment customization that you created in the previous tutorial. For more information about global IDs, refer to Global IDs in Shopify APIs.Execute the following mutation and replace
YOUR_CUSTOMIZATION_ID_HERE
with the full global ID of your payment customization.The value of the metafield specifies that the function should hide Cash on Delivery when the cart total is above 150.
You should receive a GraphQL response that includes the ID of the created metafield. If the response includes any messages under
userErrors
, then review the errors, check that your mutation andownerId
are correct, and try the request again.
Step 3: Test the payment customization
Anchor link to section titled "Step 3: Test the payment customization"- Open your development store and build a cart with a total (including shipping and tax) under 150. The Cash on Delivery payment method should display in checkout.
- Add additional items to your cart to raise the total over 150. Your payment function should now hide the Cash on Delivery payment option.
Open your terminal where
shopify app dev
is running, and review your function executions.When testing functions on development stores, the output of
dev
includes executions of your functions, any debug logging you have added to them, and a link to a local file with the full function execution details.In a new terminal window, use the Shopify CLI
app function replay
command to replay a function execution locally, and debug your function without the need to re-trigger the function execution on Shopify.Select the function execution from the top of the list. Press
q
to quit when you are finished debugging.
- Build a payment customization user interface with App Bridge.
- Learn how to use variables in your input query.