{"data":{"markdownRemark":{"html":"

How to Charge a Payment Method

\n

In order to charge a payment method, you'll need a payment method's ID, and information about the transaction you'd like to create using it. These make up the input for the chargePaymentMethod GraphQL mutation.

\n

If you charge a single-use payment method, this will consume it and you will no longer be able to look up, charge, or vault that payment method.

\n

Mutation

\n
mutation ExampleCharge($input: ChargePaymentMethodInput!) {\n  chargePaymentMethod(input: $input) {\n    transaction {\n      id\n      status\n    }\n  }\n}
\n

Variables

\n
{\n  \"input\": {\n    \"paymentMethodId\": \"id_of_payment_method\",\n    \"transaction\": {\n      \"amount\": \"11.23\"\n    }\n  }\n}
\n

Full Request

\n

To plug this mutation into a curl request, compose the query and variables into a JSON-valid string and use it as the payload. For example:

\n
curl \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: Basic PUBLICKEY:PRIVATEKEY_BASE64ENCODED' \\\n -H 'Braintree-Version: 2019-01-01' \\\n -X POST https://payments.sandbox.braintree-api.com/graphql \\\n -d '{\n  \"query\": \"mutation ExampleCharge($input: ChargePaymentMethodInput!) {\n    chargePaymentMethod(input: $input) {\n      transaction {\n        id\n        status\n      }\n    }\n  }\",\n  \"variables\": {\n    \"input\": {\n      \"paymentMethodId\": \"id_of_payment_method\",\n      \"transaction\": {\n        \"amount\": \"11.23\"\n      }\n    }\n  }\n}'
\n

There are many more possible fields you can use in the input or request in the output; see the Explorer for more.

\n

Response

\n

A successful charge results in a transaction with the SUBMITTED_FOR_SETTLEMENT status:

\n
{\n  \"data\": {\n    \"transaction\": {\n      \"id\": \"id_of_transaction\",\n      \"status\": \"SUBMITTED_FOR_SETTLEMENT\"\n    }\n  }\n}
\n

An unsuccessful charge results in a transaction with one of several failure statuses, which will give you more information about the type of failure.

\n

Transaction IDs

\n

After charging a payment method, you may want to store the ID of the resulting transaction in your database for future reference,\neither for presenting back in the UI at a later time, or for your own internal book-keeping, analysis, or reporting requirements.\nYou can retrieve this transaction by its ID using the node query.

\n

Transaction Statuses

\n

Even if the chargePaymentMethod mutation doesn't return any errors, the transaction it created may not have processed succesfully. For instance, the customer's card-issuing bank may have declined the charge, or your fraud protection settings may have blocked it. In this case, the Transaction will have a failure status, such as Processor Declined or Gateway Rejected, and further information on what caused the failure. You can then use this to decide how to handle the failure and how to report it back to your customer.

\n

A successful transaction will be updated over time as funds are debited from the customer and sent to you. As money moves, the transaction passes through multiple states in the payment lifecycle, most typically from Authorized to Submitted for Settlement, to Settling, to Settled.

\n

Using the complete list of transaction statuses and their meanings, you can check the status of a transaction to determine if it was successful or unsuccessful and why, and if successful, what will happen to the transaction next.

\n

See also:

\n\n

Error Handling

\n

Unsuccessful transactions are a normal part of transaction processing and should not be considered exceptional. If an error occurs, you will either not receive a transaction object on the payload, or you will receive only a partial object.

\n

Here's an example of an error on a transaction.

\n
{\n  \"data\": {\n    \"chargePaymentMethod\": null\n  },\n  \"errors\": [\n    {\n      \"message\": \"Amount must be greater than zero.\",\n      \"path\": [\"chargePaymentMethod\"],\n      \"extensions\": {\n        \"errorType\": \"user_error\",\n        \"errorClass\": \"VALIDATION\",\n        \"legacyCode\": \"81531\",\n        \"inputPath\": [\"input\", \"transaction\", \"amount\"]\n      }\n    }\n  ],\n  \"extensions\": {\n    \"requestId\": \"abc-123-def-456\"\n  }\n}
\n

See more details on handling errors in Making API Calls.

\n

Testing

\n

We provide test single-use payment methods in Sandbox, which can be used instead of tokenizing a new payment method every time. Pass any of these payment method \"nonces\" as the paymentMethodId in the chargePaymentMethod mutation. Or, to test vaulted payment methods, use one of these test single-use payment methods in a vaultPaymentMethod mutation, then pass the resulting payment method's ID into the chargePaymentMethod mutation.

\n

To test processor responses, you can use these test values as the transaction amount.

","fields":{"slug":"guides/charging_payment_methods/"},"frontmatter":{"tags":["graphql","integration"],"title":"Charging a Payment Method"},"excerpt":"How to Charge a Payment Method In order to charge a payment method, you'll need a payment method's ID, and information about the transaction…"}},"pageContext":{"sidebarLinks":[{"label":"Get Started","path":"guides/","links":null},{"label":"Payment API Concepts","path":"guides/concepts/","links":[{"label":"Lifecycle of a Payment","path":"guides/concepts#the-lifecycle-of-a-payment"},{"label":"Payment Methods","path":"guides/concepts#payment-methods"},{"label":"Transactions","path":"guides/concepts#transactions"}]},{"label":"Making API Calls","path":"guides/making_api_calls/","links":[{"label":"Requests","path":"guides/making_api_calls#the-shape-of-a-request"},{"label":"Responses","path":"guides/making_api_calls#the-shape-of-a-response"},{"label":"Using GraphiQL","path":"guides/making_api_calls#using-graphiql"}]},{"label":"Collecting Payment Information","path":"guides/collecting_payment_information/","links":null},{"label":"Charging Payment Methods","path":"guides/charging_payment_methods/","links":null},{"label":"Fetching Objects via the Node Query","path":"guides/node_query/","links":null},{"label":"Vaulting Payment Methods","path":"guides/vaulting_payment_methods/","links":null},{"label":"Reversing a Transaction","path":"guides/reversing_transactions/","links":null},{"label":"Testing","path":"guides/testing/","links":null}],"slug":"guides/charging_payment_methods/"}}