Create the payments function
You can use payment customizations to hide, reorder, and rename the payment options that are available to buyers during checkout. In this tutorial, you'll use Shopify Functions to hide a payment option offered to customers at checkout, based on the total value of their cart.
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:
- Generate starter code for Shopify Functions.
- Use GraphQL to define the input of your function.
- Deploy functions to the Shopify platform.
- Review logs for your function.
Requirements
Anchor link to section titled "Requirements"- You've created a Partner account.
- You've created a development store.
You've created an app that uses Shopify CLI 3.49.5 or higher. If you previously installed Shopify CLI, then make sure that you're using the latest version.
If you plan to create a UI for your extension, then start with the Remix app template.
You've installed Node.js 16 or higher.
You've installed your app on the development store.
Rust-specific requirements
Anchor link to section titled "Rust-specific requirements"The following requirements are specific to Rust-based development with Shopify Functions.
You've installed Rust.
On Windows, Rust requires the Microsoft C++ Build Tools. Make sure to select the Desktop development with C++ workload when installing the tools.
You've installed cargo-wasi:
Limitations and considerations
Anchor link to section titled "Limitations and considerations"- Plan and geographical restrictions apply. Learn more
- When the Payment Customization API usage is restricted, the function input will still contain all payment methods, however output operations that target restricted payment methods will not take effect on the checkout.
The Payment Customization API doesn't currently support draft orders.
You can't rename payment methods that have logos as a name, such as Shop Pay, Apple Pay and Google Pay. This also includes all wallets and the Shopify native gift card field.
In Shop Pay, payment customization functions do not apply operations on any payment methods except the native gift card field.
You can remove wallets from the Express or payment method section of checkout, but you can't reorder them.
You can install a maximum of five payment customization functions on each store.
Step 1: Create the payment customization function
Anchor link to section titled "Step 1: Create the payment customization function"To create your payment customization function, you can use Shopify CLI to generate a starter function, specify the inputs for your function using an input query, and implement your function logic using JavaScript or Rust.
Navigate to your app directory:
Run the following command to create a new payment customization extension:
Choose the language that you want to use. For this tutorial, you should select either Rust or JavaScript.
Shopify defaults to Rust as the most performant and recommended language choice to stay within the platform limits. For more information, refer to language considerations.
Navigate to
extensions/payment-customization
:Replace the contents of
src/run.graphql
file with the following code.run.graphql
defines the input for the function. You need the cart total and the available checkout payment methods.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 function logic will hide a payment method with a name containing
Cash on Delivery
when the cart total purchase amount is above100
.
Step 2: Preview the function on a development store
Anchor link to section titled "Step 2: Preview the function on a development store"To test your function, you need to make it available to your development store.
If you're developing a function in a language other than JavaScript or TypeScript, ensure you have configured
build.watch
in your function extension configuration.Navigate back to your app root:
Use the Shopify CLI
dev
command to start app preview:You can keep the preview running as you work on your function. When you make changes to a watched file, Shopify CLI rebuilds your function and updates the function extension's drafts, so you can immediately test your changes.
Follow the CLI prompts to preview your app, and install it on your development store.
Step 3: Create the payment customization with GraphiQL
Anchor link to section titled "Step 3: Create the payment customization with GraphiQL"To activate your function, you must create a payment customization on the store where you installed your app. You can do this using the paymentCustomizationCreate
GraphQL mutation.
In subsequent tutorials, you'll use metafields on this payment customization to configure your function, and create a user interface so merchants can configure the function themselves.
Install the Shopify GraphiQL app on your store. If you've already installed GraphiQL, then you should do so again to select the necessary access scopes for payment customizations.
In the GraphiQL app, in the API Version field, select the 2023-07 version.
Find the ID of your function by executing the following query:
The result contains a node with your function's ID:
Execute the following mutation and replace
YOUR_FUNCTION_ID_HERE
with the ID of your function:You should receive a GraphQL response that includes the ID of the created payment customization. If the response includes any messages under
userErrors
, then review the errors, check that your mutation andfunctionId
are correct, and try the request again.
Step 4: Test the payment customization
Anchor link to section titled "Step 4: Test the payment customization"- From the Shopify admin, go to Settings > Payments.
- Check the Payment customizations section. You should find the Hide payment method by cart total payment customization that you created with GraphiQL.
- From the Manual payment methods section, click Add manual payment method and then click Cash on Delivery (COD).
Click Activate.
Open your development store and build a cart with a total (including shipping and tax) under 100. The Cash on Delivery payment method should display in checkout.
Add additional items to your cart to raise the total over 100. 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.
- Add configuration to your payment customization using metafields.