你能把 Stripe Java 代码转换成 CFscript 吗

Can you Convert Stripe Java Code into CFscript

下面的代码是如何实现条带支付的API,我想知道如果我把它转换成CFscript并调用我的普通变量就可以了吗?

    // 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);

虽然不是您上面提供的 Java 代码的直接转换,但使用 http service 函数对 cfscript 执行此操作应该相当简单。例如:

<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>