Can you Convert Stripe Java Code into CFscript

眉间皱痕 提交于 2019-12-25 01:44:17

问题


The code below is how to impletment stripe payment API, I was wondering if I just convert it to CFscript and call my normal variable will it work?

    // Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_aHhoYVOnsayNSIleB1ETUCSq00vUOS9YVQ";

Map<String, Object> params = new HashMap<String, Object>();

ArrayList<String> paymentMethodTypes = new ArrayList<>();
paymentMethodTypes.add("card");
params.put("payment_method_types", paymentMethodTypes);

ArrayList<HashMap<String, Object>> lineItems = new ArrayList<>();
HashMap<String, Object> lineItem = new HashMap<String, Object>();
lineItem.put("name", "T-shirt");
lineItem.put("description", "Comfortable cotton t-shirt");
lineItem.put("amount", 500);
lineItem.put("currency", "usd");
lineItem.put("quantity", 1);
lineItems.add(lineItem);
params.put("line_items", lineItems);

params.put("success_url", "https://example.com/success?session_id={CHECKOUT_SESSION_ID}");
params.put("cancel_url", "https://example.com/cancel");

Session session = Session.create(params);

回答1:


While not a direct conversion of the Java code you've provided above, it should be fairly straight-forward to do this with cfscript using http service functions. For example:

<cfscript>
secKey = "sk_test_xxxx";

/* create new http service */
httpService = new http();
httpService.setMethod("post");
httpService.setCharset("utf-8");
httpService.setUrl("https://api.stripe.com/v1/checkout/sessions");

/* add header */
httpService.addParam(type="header", name="Authorization", value="Bearer " & secKey);

/* add params */ 
httpService.addParam(type="formfield",name="success_url",value="https://example.com/success");
httpService.addParam(type="formfield",name="cancel_url",value="https://example.com/fail");
httpService.addParam(type="formfield",name="payment_method_types[]",value="card");
httpService.addParam(type="formfield",name="line_items[0][amount]",value="1000");
httpService.addParam(type="formfield",name="line_items[0][currency]",value="usd");
httpService.addParam(type="formfield",name="line_items[0][quantity]",value="1");
httpService.addParam(type="formfield",name="line_items[0][name]",value="widget");

/* make the http call */
result = httpService.send().getPrefix();

/* parse json and print id */
chkSession = DeserializeJSON(result.fileContent);
writeoutput(chkSession.id)
</cfscript>


来源:https://stackoverflow.com/questions/58309934/can-you-convert-stripe-java-code-into-cfscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!