Stripe example with metadata and/or other options

已查看 108 次
跳至第一个未读帖子

Greg M.

未读,
2016年12月10日 23:35:532016/12/10
收件人 cfpayment
Hi,

I'm testing out cfpayment and I'm using the quick example as a template. One thing I'm not sure of is how to pass metadata or other fields, like invoice or order, to stripe. I created a struct called options and passed it in there, but I got a Gateway returned unknown response: 400 back.
 
Does anyone have an example for this?


Thanks!

-Greg






Brian G

未读,
2016年12月11日 23:47:082016/12/11
收件人 cfpayment
Greg, 

You use the options struct to pass in additional fields/metadata. Here's a few examples like for passing in optional statement_descriptor:

<cfset res = gw.purchase(money = variables.svc.createMoney(5000), account = myAccount, options = {"statement_descriptor": "Test Data"}) />


Or when getting some data back to trigger an expansion in the results data:


<cfset res = gw.getBalanceTransaction(id = myID, options = {"expand[]": "source.data"}) />




Brian

Greg M.

未读,
2016年12月12日 12:48:232016/12/12
收件人 cfpayment
Thanks Brian! 

I was able to get statement_descriptor and others working, I'm still trying to figure out the syntax for the metadata structure.  

I thought it would be something like :
...
options = StructNew();
options.statement_descriptor = "Test Data";
options.receipt_email = "joe...@example.com";
options.metadata.user_id = "1";
options.metadata.otherdata = "stuff";
...
response = gw.purchase(money = money, account = account, options = options);

That works for the first two, but the last two always throw an error.

I also tried just creating a string 

options.metadata ="[userid] = 1"; but that failed with Invalid metadata: metadata must be a set of key-value pairs

I know I'm missing something very obvious. Any insight?

Thanks again.

-Greg

Brian G

未读,
2016年12月19日 23:24:442016/12/19
收件人 cfpayment
Greg, generally the metadata structure is like faux structure syntax, so you want to send something like:


<cfset var options = {"metadata[order_id]": 6735} />

So, if the metadata option you want to set is order_id, you actually create a structure key with the value "metadata[order_id]" and stripe unfurls that into a key named order_id instead an object/structure called metadata. In short, they use a PHP-like syntax for flattening out arrays and structures into key-value pairs for use in HTTP FORM POST where you don't have JSON/XML structure available.

So, put in another CF syntax, it looks like:

```
<cfset var options = structNew() />
<cfset structInsert(options, "metadata[order_id]", 6735, true) />
....
<cfset response = gw.purchase(money = money, account = account, options = options) />
```

Hope that makes sense, 


Brian

Greg M.

未读,
2016年12月20日 16:28:272016/12/20
收件人 cfpayment
Okay, that makes much more sense. I'll get to testing.

Thanks again for your help!

-Greg
回复全部
回复作者
转发
0 个新帖子