Ошибки, покрывающие полосу Javascript для ошибок CFscript - PullRequest
0 голосов
/ 09 октября 2019

Я работаю с Stripe API получил некоторую помощь , преобразовав Java-пример , совершив http-вызов. Верхняя часть кода ниже работает. Однако, когда я делаю последние два куска кода для перенаправления, я думаю, что это сложно, потому что он не может читать переменные, сделанные в cfscript.

Эта ссылка объясняет, что я пытаюсь сделать https://stripe.com/docs/payments/checkout/server, используя Java и конвертируя его.

<cfscript>
   secKey = "sk_test_********";

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

<script src="https://js.stripe.com/v3/"></script>
<script>
    const stripe = Stripe('pk_test_**********');
</script>
<script>
    const {error} = await stripe.redirectToCheckout({
       // Make the id field from the Checkout Session creation API response
       // available to this file, so you can provide it as parameter here
       // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
       sessionId: '(chkSession.id)'
   })
   // If `redirectToCheckout` fails due to a browser or network
   // error, display the localized error message to your customer
   // using `error.message`.
</script>

1 Ответ

0 голосов
/ 11 октября 2019

Похоже, у вас есть chkSession из кода cfscript, а затем вам нужно, чтобы он был доступен на стороне клиента. Для этого вам просто нужно вывести скрипт js с переменной output.

  sessionId: '<cfoutput>#chkSession.id#</cfoutput>'

Если переменная недоступна там, где вам нужно вывести ее, добавьте request. при ее установке и выводе, чтобы поместить ее в область запроса, которая доступна из всего кода на сервере CF, работающем втот же запрос.

...