Important Notice: New topic creation is now closed on this board, but you can still participate by replying to existing topics. For new topics, check out our newly refreshed Developer categories!
Solved

payment gateway error with graphql mutation customerPaymentMethodCreditCardCreate

Schmidtc63
Shopify Partner
90 13 23

I'm trying to create a payment using this mutation:

mutation { customerPaymentMethodCreditCardCreate(billingAddress: {address1:"1234 Main Street", city:"MyTown", company:"", countryCode:US, firstName:"Chris", lastName: "Schmidt", phone:"5555551212",provinceCode:"WA",zip:"12345"}, customerId: "gid://shopify/Customer/6521351733502", sessionId: "east-a9f25aee5072554da4e19f23ca7d1b2c") { customerPaymentMethod { id revokedReason } userErrors { field message } } }

 

However, I get this error:

{"data":{"customerPaymentMethodCreditCardCreate":{"customerPaymentMethod":null,"userErrors":[{"field":["paymentGatewayId"],"message":"Payment gateway is invalid"}]}},"extensions":{"cost":{"requestedQueryCost":10,"actualQueryCost":10,"throttleStatus":{"maximumAvailable":2000.0,"currentlyAvailable":1990,"restoreRate":100.0}}}}

 

There is no payment gateway input field.

Accepted Solution (1)

Schmidtc63
Shopify Partner
90 13 23

This is an accepted solution.

Wow. Creating a payment externally is not really documented any place. Here's my step by step guide:

 

setup our billing address as a graphql object  

 

 

billingAddress={address1:"1234 Main Street", city:"Any Town", company:"", countryCode:US, firstName:"Bobo", lastName: "Smith", phone:"5095551212",provinceCode:"WA",zip:"12345"}
​

 

 


create a transaction session id

 

 

rdata={"credit_card":{"number":"4242424242424242","first_name":"Bobo","last_name":"Smith","month":"1","year":"2024","verification_value":"555"}}​

 

   
NOTE -- this query does NOT validate the credit card. It simply creates a session that belongs to whatever information is submitted, valid or not.

 

Set objRequest = server.createobject("WinHttp.WinHttpRequest.5.1")
objRequest.open "POST","https://elb.deposit.shopifycs.com/sessions", false
objRequest.setRequestHeader "X-Shopify-Access-Token", ${adminApiToken}
objRequest.setRequestHeader "Content-Type", "application/json"
objRequest.send rdata​

 


the response will look something like this:

 

{"id":"east-c6b1e88affb51bfe809ad229a52175f5"}
sessionId=response.id​

 


let's make sure the credit card is valid and is available in the customer's vault.

 

gqlRequest="mutation {customerPaymentMethodCreditCardCreate(billingAddress: ${billingAddress}, customerId: "gid://shopify/Customer/1234567890", sessionId: "${sessionId}") { customerPaymentMethod { id revokedReason } userErrors { field message }}}"

t=submitAdminGraphQL(gqlRequest,ct_Graphql) <--------------GRAPHQL REQUEST​

 


parse that response for errors. About the only valid error is "has already been taken", meaning the card has already been entered into the card vault for that customer. Otherwise, we need nothing from that response if there are no other errors

create the parent transaction. NOTE: the "source":"external" is important...and completely undocumented

 

rdata={"transaction":{"currency":"USD","amount":20,"kind":"authorization","source":"external"}}

t=submitAdminApi("POST", "json","orders/"${orderId}"/transactions.json", rdata) <-----------------API REQUEST​

 


We'll get back something like this:

 

{
        "transaction": {
            "id": 5819902296318,
            "order_id": 5018385514750,
            "kind": "authorization",
            "gateway": "manual (shopify_installments)",
            "status": "success",
            "message": null,
            "created_at": "2022-12-06T16:36:10-08:00",
            "test": false,
            "authorization": null,
            "location_id": null,
            "user_id": null,
            "parent_id": null,
            "processed_at": "2022-12-06T16:36:10-08:00",
            "device_id": null,
            "error_code": null,
            "source_name": "6722249",
            "receipt": {},
            "amount": "20.00",
            "currency": "USD",
            "admin_graphql_api_id": "gid:\/\/shopify\/OrderTransaction\/5819902296318"
        }
    }
    parentTransactionId=transaction.id​

 


Now, create the payment/sale/capture transaction:

 

 rdata={"transaction":{"currency":"USD","amount":20,"kind":"sale","parentTransactionId":"${parentTransactionId}","source":"external"}}
    t=submitAdminApi("POST", "json","orders/"&orderId&"/transactions.json", rdata)  <----------API REQUEST​

 

   
We get something like this back:

 

{
        "transaction": {
            "id": 5902144798958,
            "order_id": 5027944726766,
            "kind": "sale",
            "gateway": "shopify_payments",
            "status": "success",
            "message": null,
            "created_at": "2022-12-06T17:02:33-08:00",
            "test": true,
            "authorization": null,
            "location_id": null,
            "user_id": null,
            "parent_id": null,
            "processed_at": "2022-12-06T17:02:33-08:00",
            "device_id": null,
            "error_code": null,
            "source_name": "6771057",
            "receipt": {},
            "amount": "20.00",
            "currency": "USD",
            "admin_graphql_api_id": "gid:\/\/shopify\/OrderTransaction\/5902144798958"
        }
    }​

 



and we've successfully created a payment

 

View solution in original post

Replies 2 (2)

Schmidtc63
Shopify Partner
90 13 23

This is an accepted solution.

Wow. Creating a payment externally is not really documented any place. Here's my step by step guide:

 

setup our billing address as a graphql object  

 

 

billingAddress={address1:"1234 Main Street", city:"Any Town", company:"", countryCode:US, firstName:"Bobo", lastName: "Smith", phone:"5095551212",provinceCode:"WA",zip:"12345"}
​

 

 


create a transaction session id

 

 

rdata={"credit_card":{"number":"4242424242424242","first_name":"Bobo","last_name":"Smith","month":"1","year":"2024","verification_value":"555"}}​

 

   
NOTE -- this query does NOT validate the credit card. It simply creates a session that belongs to whatever information is submitted, valid or not.

 

Set objRequest = server.createobject("WinHttp.WinHttpRequest.5.1")
objRequest.open "POST","https://elb.deposit.shopifycs.com/sessions", false
objRequest.setRequestHeader "X-Shopify-Access-Token", ${adminApiToken}
objRequest.setRequestHeader "Content-Type", "application/json"
objRequest.send rdata​

 


the response will look something like this:

 

{"id":"east-c6b1e88affb51bfe809ad229a52175f5"}
sessionId=response.id​

 


let's make sure the credit card is valid and is available in the customer's vault.

 

gqlRequest="mutation {customerPaymentMethodCreditCardCreate(billingAddress: ${billingAddress}, customerId: "gid://shopify/Customer/1234567890", sessionId: "${sessionId}") { customerPaymentMethod { id revokedReason } userErrors { field message }}}"

t=submitAdminGraphQL(gqlRequest,ct_Graphql) <--------------GRAPHQL REQUEST​

 


parse that response for errors. About the only valid error is "has already been taken", meaning the card has already been entered into the card vault for that customer. Otherwise, we need nothing from that response if there are no other errors

create the parent transaction. NOTE: the "source":"external" is important...and completely undocumented

 

rdata={"transaction":{"currency":"USD","amount":20,"kind":"authorization","source":"external"}}

t=submitAdminApi("POST", "json","orders/"${orderId}"/transactions.json", rdata) <-----------------API REQUEST​

 


We'll get back something like this:

 

{
        "transaction": {
            "id": 5819902296318,
            "order_id": 5018385514750,
            "kind": "authorization",
            "gateway": "manual (shopify_installments)",
            "status": "success",
            "message": null,
            "created_at": "2022-12-06T16:36:10-08:00",
            "test": false,
            "authorization": null,
            "location_id": null,
            "user_id": null,
            "parent_id": null,
            "processed_at": "2022-12-06T16:36:10-08:00",
            "device_id": null,
            "error_code": null,
            "source_name": "6722249",
            "receipt": {},
            "amount": "20.00",
            "currency": "USD",
            "admin_graphql_api_id": "gid:\/\/shopify\/OrderTransaction\/5819902296318"
        }
    }
    parentTransactionId=transaction.id​

 


Now, create the payment/sale/capture transaction:

 

 rdata={"transaction":{"currency":"USD","amount":20,"kind":"sale","parentTransactionId":"${parentTransactionId}","source":"external"}}
    t=submitAdminApi("POST", "json","orders/"&orderId&"/transactions.json", rdata)  <----------API REQUEST​

 

   
We get something like this back:

 

{
        "transaction": {
            "id": 5902144798958,
            "order_id": 5027944726766,
            "kind": "sale",
            "gateway": "shopify_payments",
            "status": "success",
            "message": null,
            "created_at": "2022-12-06T17:02:33-08:00",
            "test": true,
            "authorization": null,
            "location_id": null,
            "user_id": null,
            "parent_id": null,
            "processed_at": "2022-12-06T17:02:33-08:00",
            "device_id": null,
            "error_code": null,
            "source_name": "6771057",
            "receipt": {},
            "amount": "20.00",
            "currency": "USD",
            "admin_graphql_api_id": "gid:\/\/shopify\/OrderTransaction\/5902144798958"
        }
    }​

 



and we've successfully created a payment

 

kuldeepak
Shopify Partner
8 0 0

Hello @Schmidtc63,

I hope this message finds you well.
I have implemented the APIs in Laravel but I am encountering an issue when I add an incorrect zip code in the  API request. The error messages I received were:

1.'Instrument ownership payment instrument is invalid'
2.'Payment instrument zip is not valid for the United Kingdom'
3.'Has already been taken' (in the second attempt with the correct details)
I have since corrected the address and card details, but unfortunately, the payment was not successful in Shopify. Is there any API to update the customer's vault to ensure accurate testing? Your assistance would be greatly appreciated.

Thank you in advance.