Passing child parameters to Stripe API from CFML

377 Views Asked by At

I can connect to the Stripe API 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_14">
<cfhttpparam type="FormField" name="name" value =#Customer_Name_1#>
......
</cfhttp>```

But I am unable to pass child parameters associated with fields such as "address".

I have tried the code

<CFSET Town_1 = "..."> <CFSET Street_1 = "..."> <CFSET Street_2 = "...">

<CFSET Address_1 = SerializeJSON({city: #Town_1#, line1: #Street_1#, line2: #Street_2#})>
<cfhttpparam type="FormField"   name="address"  value = '#Address_1#'>

but this fails, and I get an error

"error": {
"message": "Invalid object",
"param": "address",
"type": "invalid_request_error"
}

Thanks in advance for comments.

1

There are 1 best solutions below

0
koopajah On

Stripe's API uses application/x-www-form-urlencoded for posting parameters. This means you are not supposed to serialize the parameters to JSON. Instead, you should pass the parameters as individual parameters with their full "path".

If you want to send an address with line1 and country you would send each one separately as address[line1]="my_value" and address[country]="US".

If you wanted to do multiple levels you continue the bracket logic such as shipping[address][country]="US".

With your example it would likely look like this:

<cfhttpparam type="FormField" name="address[line1]"       value = 'my_line1'>
<cfhttpparam type="FormField" name="address[city]"        value = 'my_city'>
<cfhttpparam type="FormField" name="address[postal_code]" value = '90210'>
<cfhttpparam type="FormField" name="address[state]"       value = 'CA'>
<cfhttpparam type="FormField" name="address[country]"     value = 'US'>