Skip to main content
Inspiring
June 19, 2022
Answered

Problems in passing id values to Stripe API objects using cfhttp

  • June 19, 2022
  • 3 replies
  • 601 views

I am able to pass field values to Stripe's Customer object using the code

 

<cfhttp method = "POST" url="https://api.stripe.com/v1/customers">

<cfhttpparam type="header" name="Authorization" value="Bearer sk_test_.....">

<cfhttpparam type="FormField" name="id" value = "cust_admin_18">

<cfhttpparam type="FormField" name="address[country]" value = "US"> ........ </cfhttp>

 

However when I try similar code with other Stripe objects, for example Payment_Method, I get an error with the id field. So

<cfhttp method = "POST" url="https://api.stripe.com/v1/payment_methods">

<cfhttpparam type="header" name="Authorization" value="Bearer sk_test_.....">

<cfhttpparam type="FormField" name="id" value = "pm_123">

<cfhttpparam type="FormField" name="type" value = "card">

<cfhttpparam type="FormField" name="card[number]" value = "4242424242424242"> 

</cfhttp>

produces an error parameter_unknown - id

 

Changing "FormField" to "header" stops the error, but then the id value is not passed.

 

Thanks in advance for comments.

 

This topic has been closed for replies.
Correct answer BKBK

In REST APIs a POST request is the usual way to create a resource. After the server creates the resource, it sends the ID of the created resource to the client.

 

Your code suggests that you wish to create a payment method. If so, then the ID is as yet unknown at this point. The server first has to create the payment method, assign it an ID, then return the ID to you.  

 

So your code should be something like

 

<!--- See documentation https://stripe.com/docs/api/payment_methods/create --->
<cfhttp method = "POST" url="https://api.stripe.com/v1/payment_methods">
    <cfhttpparam type="header" name="Authorization" value="Bearer sk_test_.....">
    <cfhttpparam type="FormField" name="type" value = "card">
    <cfhttpparam type="FormField" name="card[number]" value = "4242424242424242"> 
    <cfhttpparam type="FormField" name="card[exp_month]" value = "6">
    <cfhttpparam type="FormField" name="card[exp_year]" value = "2023"> 
    <cfhttpparam type="FormField" name="card[cvc]" value = "314"> 
</cfhttp>

 

The API's response should then contain the payment method's ID. It will be something like pm_1LC1qa2eZvKYlo2Cl9jsP6CS

3 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
June 19, 2022

In REST APIs a POST request is the usual way to create a resource. After the server creates the resource, it sends the ID of the created resource to the client.

 

Your code suggests that you wish to create a payment method. If so, then the ID is as yet unknown at this point. The server first has to create the payment method, assign it an ID, then return the ID to you.  

 

So your code should be something like

 

<!--- See documentation https://stripe.com/docs/api/payment_methods/create --->
<cfhttp method = "POST" url="https://api.stripe.com/v1/payment_methods">
    <cfhttpparam type="header" name="Authorization" value="Bearer sk_test_.....">
    <cfhttpparam type="FormField" name="type" value = "card">
    <cfhttpparam type="FormField" name="card[number]" value = "4242424242424242"> 
    <cfhttpparam type="FormField" name="card[exp_month]" value = "6">
    <cfhttpparam type="FormField" name="card[exp_year]" value = "2023"> 
    <cfhttpparam type="FormField" name="card[cvc]" value = "314"> 
</cfhttp>

 

The API's response should then contain the payment method's ID. It will be something like pm_1LC1qa2eZvKYlo2Cl9jsP6CS

Inspiring
June 20, 2022

Many thanks. This code does some of what I need. I may come back with more comments.

BKBK
Community Expert
Community Expert
June 20, 2022

The Stripe API test code

<cfhttp method = "POST" url="https://api.stripe.com/v1/payment_methods">
	<cfhttpparam type="header" name="Authorization" value="Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc">
	<cfhttpparam type="FormField" name="type" value = "card">
	<cfhttpparam type="FormField" name="card[number]" value = "4242424242424242"> 
	<cfhttpparam type="FormField" name="card[exp_month]" value = "6">
	<cfhttpparam type="FormField" name="card[exp_year]" value = "2023"> 
	<cfhttpparam type="FormField" name="card[cvc]" value = "314"> 
</cfhttp>
<cfdump var="#cfhttp.filecontent#" >

 

gives the result:

{
	"id": "pm_1LCfsY2eZvKYlo2CIsSsRyzb",
	"object": "payment_method",
	"billing_details": {
		"address": {
			"city": null,
			"country": null,
			"line1": null,
			"line2": null,
			"postal_code": null,
			"state": null
		},
		"email": null,
		"name": null,
		"phone": null
	},
	"card": {
		"brand": "visa",
		"checks": {
			"address_line1_check": null,
			"address_postal_code_check": null,
			"cvc_check": "unchecked"
		},
		"country": "US",
		"exp_month": 6,
		"exp_year": 2023,
		"fingerprint": "Xt5EWLLDS7FJjR1c",
		"funding": "credit",
		"generated_from": null,
		"last4": "4242",
		"networks": {
			"available": ["visa"],
			"preferred": null
		},
		"three_d_secure_usage": {
			"supported": true
		},
		"wallet": null
	},
	"created": 1655713358,
	"customer": null,
	"livemode": false,
	"metadata": {},
	"type": "card"
}
Charlie Arehart
Community Expert
Community Expert
June 19, 2022

Help us out: which specific kind of call to that object are you making, that accepts an ID. Not all do, according to the options for that object shown at https://stripe.com/docs/api/payment_methods

/Charlie (troubleshooter, carehart. org)
Nancy OShea
Community Expert
Community Expert
June 19, 2022

[Moderator moved from Using the Community (forums) to ColdFusion.]

Nancy O'Shea— Product User & Community Expert