This is a beta release of documentation for Magento 2.4, published for previewing soon-to-be-released functionality. Content in this version is subject to change. Links to the v2.4 code base may not properly resolve until the code is officially released.

setPaymentMethodOnCart mutation

The setPaymentMethodOnCart mutation defines which payment method to apply to the cart. Magento GraphQL supports the following offline payment methods:

Title Code
Bank Transfer Payment banktransfer
Cash on Delivery cashondelivery
Check / Money order checkmo
No Payment Information Required free
Purchase Order purchaseorder

Supported online payment methods include:

Syntax

mutation: {setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput}}

Example usage

Offline payment method

The following example assigns the banktransfer payment method to the specified cart.

Request:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mutation {
  setPaymentMethodOnCart(input: {
      cart_id: "rMQdWEecBZr4SVWZwj2AF6y0dNCKQ8uH"
      payment_method: {
          code: "banktransfer"
      }
  }) {
    cart {
      selected_payment_method {
        code
        title
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
{
  "data": {
    "setPaymentMethodOnCart": {
      "cart": {
        "selected_payment_method": {
          "code": "banktransfer",
          "title": "Bank Transfer Payment"
        }
      }
    }
  }
}

Input attributes

The top-level SetPaymentMethodOnCartInput object is listed first. All child objects are listed in alphabetical order.

SetPaymentMethodOnCartInput attributes

The SetPaymentMethodOnCartInput object must contain the following attributes:

Attribute Data Type Description
cart_id String! The unique ID that identifies the customer’s cart
payment_method PaymentMethodInput! An object containing the payment method code

PaymentMethodInput attributes

The PaymentMethodInput object can contain the following attributes:

Attribute Data Type Description
code String! The internal name for the payment method
purchase_order_number String The purchase order number. Optional for most payment methods

For all online payment methods, the payload must include an object that defines additional information specific to that payment method.

Output attributes

The SetPaymentMethodOnCartOutput object contains the Cart object.

Attribute Data Type Description
cart Cart! Describes the contents of the specified shopping cart

Cart object

Attribute Data Type Description
applied_coupon AppliedCoupon Deprecated. Use applied_coupons instead
applied_coupons [AppliedCoupon] An array of AppliedCoupon objects. Each object contains the code text attribute, which specifies the coupon code
applied_gift_cards [AppliedGiftCard] An array of AppliedGiftCard objects. An AppliedGiftCard object contains the code text attribute, which specifies the gift card code. applied_gift_cards is a Commerce-only attribute, defined in the GiftCardAccountGraphQl module
applied_store_credit AppliedStoreCredit Contains store credit information applied to the cart. applied_store_credit is a Commerce-only attribute, defined in the CustomerBalanceGraphQl module
available_payment_methods [AvailablePaymentMethod] Available payment methods
billing_address BillingCartAddress Contains the billing address specified in the customer’s cart
email String The customer’s email address
id ID! The ID of the cart
is_virtual Boolean! Indicates whether the cart contains only virtual products
items [CartItemInterface] Contains the items in the customer’s cart
prices CartPrices Contains subtotals and totals
selected_payment_method SelectedPaymentMethod Selected payment method
shipping_addresses [ShippingCartAddress]! Contains one or more shipping addresses
total_quantity Float! Total Quantity of products in the cart

Cart query output provides more information about the Cart object.

Errors

Error Description
Could not find a cart with ID "XXX" The specified cart_id value does not exist in the quote_id_mask table.
Required parameter "cart_id" is missing. The value specified in the cart_id argument is empty.
Required parameter "code" for "payment_method" is missing. The value specified in the code argument is empty.
The current user cannot perform operations on cart "XXX" An unauthorized user (guest) tried to set a payment method for an order on behalf of an authorized user (customer), or a customer tried to set a payment method for an order on behalf of another customer.
The requested Payment Method is not available. Specified in the payment_method argument payment method is disabled or does not exist.
The shipping address is missing. Set the address and try again. You ran setPaymentMethodOnCart mutation before setShippingAddressesOnCart. Set a shipping address first. GraphQL checkout tutorial shows the order placement sequence.