Skip to main content

Create the payments function

You can build payment customizations that hide, reorder, and rename the payment options available to buyers at checkout with Shopify Functions. This tutorial shows how to use Shopify Functions to hide a payment option offered to customers at checkout, based on the total value of their cart.


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.
Screenshot that shows a hidden payment method

  • You're using API version 2025-07 or higher for your function.

Anchor to Rust-specific requirementsRust-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 the wasm32-wasip1 target:

    Terminal

    rustup target add wasm32-wasip1

Anchor to Limitations and considerationsLimitations 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.
  • 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 Point of Sale, Payment Customization Functions don't currently run, and operations aren't applied.
  • 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 activate a maximum of twenty-five payment customization functions on each store.
  • When setting payment terms on checkout, consider the following things:
    • Payment terms only apply to the specific order being processed and don't change the buyer's default payment terms.
    • Existing payment terms aren't provided in the function input and can't be referenced when setting new payment terms.
    • Payment terms can only be set if the shop is on a Shopify Plus plan.
    • Payment terms can't be set on accelerated checkouts.
    • D2C checkouts with payment terms don't support pay now options—buyers only see deferred payment options. B2B checkouts have additional deferred payment options, including vaulted credit cards.
    • Event-based payment terms and automatic payment capture work differently for B2B and D2C:
      • D2C checkouts: When the store has the Automatically when fulfilling setting enabled, payments are captured automatically based on the event trigger (FULFILLMENT_CREATED captures per fulfillment, ORDER_FULFILLED captures when the entire order is fulfilled) set by the function.
      • B2B checkouts: Automatic payment capture isn't supported. Merchants must manually capture payments when the fulfillment event occurs.

Anchor to Step 1: Create the payment customization functionStep 1: Create the payment customization function

To create your payment customization function, 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.

  1. Navigate to your app directory:

    Terminal

    cd <directory>
  2. Run the following command to create a new payment customization extension:

    Terminal

    shopify app generate extension --template payment_customization --name payment-customization
  1. 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.

    Terminal

    ? What would you like to work in?
    > (1) Rust
    (2) JavaScript
    (3) TypeScript
    (4) Wasm
  1. Navigate to extensions/payment-customization:

    Terminal

    cd extensions/payment-customization
  2. Replace the contents of src/cart_payment_methods_transform_run.graphql file with the following code.

    cart_payment_methods_transform_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.

    cart_payment_methods_transform_run.graphql

    src/cart_payment_methods_transform_run.graphql

    query Input {
    cart {
    cost {
    totalAmount {
    amount
    }
    }
    }
    paymentMethods {
    id
    name
    }
    }
    query CartPaymentMethodsTransformRunInput {
    cart {
    cost {
    totalAmount {
    amount
    }
    }
    }
    paymentMethods {
    id
    name
    }
    }
  3. If you're using JavaScript, then run the following command to regenerate types based on your input query:

    Terminal

    shopify app function typegen
  4. Replace the src/cart_payment_methods_transform_run.rs or src/cart_payment_methods_transform_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 above 100.

    File

    src/run.rs

    use super::schema;
    use shopify_function::prelude::*;
    use shopify_function::Result;

    #[shopify_function]
    fn cart_payment_methods_transform_run(input: schema::run::Input) -> Result<schema::CartPaymentMethodsTransformRunResult> {
    let no_changes = schema::CartPaymentMethodsTransformRunResult { operations: vec![] };

    // Get the cart total from the function input, and return early if it's below 100
    let cart_total: f64 = input.cart().cost().total_amount().amount().as_f64();
    if cart_total < 100.0 {
    eprintln!("Cart total is not high enough, no need to hide the payment method.");
    return Ok(no_changes);
    }

    // Find the payment method to hide, and create a hide output operation from it
    let operations = input
    .payment_methods()
    .iter()
    .find(|&method| method.name() == "Cash on Delivery")
    .map(|method| {
    vec![schema::Operation::Hide(schema::PaymentMethodHideOperation {
    payment_method_id: method.id().to_string(),
    placements: None,
    })]
    })
    .unwrap_or_default();

    Ok(schema::CartPaymentMethodsTransformRunResult { operations })
    }
    // @ts-check

    // Use JSDoc annotations for type safety
    /**
    * @typedef {import("../generated/api").CartPaymentMethodsTransformRunInput} CartPaymentMethodsTransformRunInput
    * @typedef {import("../generated/api").CartPaymentMethodsTransformRunResult} CartPaymentMethodsTransformRunResult
    */

    /**
    * @type {CartPaymentMethodsTransformRunResult}
    */
    const NO_CHANGES = {
    operations: [],
    };

    // The configured entrypoint for the 'cart.payment-methods.transform.run' extension target
    /**
    * @param {CartPaymentMethodsTransformRunInput} input
    * @returns {CartPaymentMethodsTransformRunResult}
    */
    export function cartPaymentMethodsTransformRun(input) {
    // Get the cart total from the function input, and return early if it's below 100
    const cartTotal = parseFloat(input.cart.cost.totalAmount.amount ?? "0.0");
    if (cartTotal < 100) {
    // You can use STDERR for debug logs in your function
    console.error("Cart total is not high enough, no need to hide the payment method.");
    return NO_CHANGES;
    }

    // Find the payment method to hide
    const hidePaymentMethod = input.paymentMethods
    .find(method => method.name.includes("Cash on Delivery"));

    if (!hidePaymentMethod) {
    return NO_CHANGES;
    }

    // The @shopify/shopify_function package applies JSON.stringify() to your function result
    // and writes it to STDOUT
    return {
    operations: [{
    paymentMethodHide: {
    paymentMethodId: hidePaymentMethod.id
    }
    }]
    };
    };
    Tip

    If you're selling to multiple markets, then you might need to update this logic to include conversion to the store's default currency. Refer to function development practices for money for more information.


Anchor to Step 2: Preview the function on a development storeStep 2: Preview the function on a development store

To test your function, you need to make it available to your development store.

  1. 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.
  1. Navigate back to your app root:

    Terminal

    cd ../..
  1. Use the Shopify CLI dev command to start app preview:

    Terminal

    shopify app dev

    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.

  2. Follow the CLI prompts to preview your app, and install it on your development store.


Anchor to Step 3: Create the payment customization with GraphiQLStep 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.

  1. 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.

    Note

    Make sure to select the read_payment_customizations and write_payment_customizations access scopes for the Admin API.

  2. In the GraphiQL app, in the API Version field, select the 2023-07 version.

  1. Find the ID of your function by executing the following query:

    find-function-query.graphql

    query {
    shopifyFunctions(first: 25) {
    nodes {
    app {
    title
    }
    apiType
    title
    id
    }
    }
    }

    The result contains a node with your function's ID:

    find-function-result.json

    {
    "app": {
    "title": "your-app-name-here"
    },
    "apiType": "payment_customization",
    "title": "payment-customization",
    "id": "YOUR_FUNCTION_ID_HERE"
    }
  1. Execute the following mutation and replace YOUR_FUNCTION_ID_HERE with the ID of your function:

    mutation.graphql

    mutation {
    paymentCustomizationCreate(paymentCustomization: {
    title: "Hide payment method by cart total",
    enabled: true,
    functionId: "YOUR_FUNCTION_ID_HERE",
    }) {
    paymentCustomization {
    id
    }
    userErrors {
    message
    }
    }
    }

    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 and functionId are correct, and try the request again.

    Tip

    If you receive a Could not find Function error, then confirm the following:


Anchor to Step 4: Test the payment customizationStep 4: Test the payment customization

  1. From the Shopify admin, go to Settings > Payments.
  2. Check the Payment customizations section. You should find the Hide payment method by cart total payment customization that you created with GraphiQL.
  3. From the Manual payment methods section, click Add manual payment method and then click Cash on Delivery (COD).
  4. Click Activate.
  1. 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.
  2. Add additional items to your cart to raise the total over 100. Your payment function should now hide the Cash on Delivery payment option.


Was this page helpful?