如何使条纹支付实现起作用?

huangapple go评论64阅读模式
英文:

How to make stripe payments implementation works?

问题

以下是您提供的代码的翻译部分:

public void chrage(HttpServletRequest request, HttpServletResponse response)
{
    try {
        Stripe.apiKey = "pk_test_51He5QLAnfgVAb3ehd0OxqrNHlQSrX6MApLFlchIXfrOby4JrDwndkI76CC5QGT96y5sjbT301oj2RiC9utOgH7so00GGAJaU3d";

        // Token is created using Stripe Checkout or Elements!
        // Get the payment token ID submitted by the form:
        String token = request.getParameter("stripeToken");

        ChargeCreateParams params =
                ChargeCreateParams.builder()
                        .setAmount(999L)
                        .setCurrency("usd")
                        .setDescription("Example charge")
                        .setSource(token)
                        .build();

        Charge charge = Charge.create(params.toMap());
    } catch (AuthenticationException ex) {
        Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidRequestException ex) {
        Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (APIConnectionException ex) {
        Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (CardException ex) {
        Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (APIException ex) {
        Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
    }
}
<div ng-controller="Controller" class="right_col controlador"> 
    <form  id = "frmCaptura" data-toggle="validator">
        <div class="x_panel"> 
            <div class="x_title">  
                <jopen:getLabel label="facturacion.nombre"/>

            </div>
            <div class="x_content">
                <script src="https://js.stripe.com/v3/"></script>

                <form action="/charge" method="post" id="payment-form">
                    <div class="form-row">
                        <label for="card-element">
                            Credit or debit card
                        </label>
                        <div id="card-element">
                            <!-- A Stripe Element will be inserted here. -->
                        </div>

                        <!-- Used to display Element errors. -->
                        <div id="card-errors" role="alert"></div>
                    </div>

                    <button>Submit Payment</button>
                </form> 
            </div>
        </div>
    </form>
</div>

请注意,上面的翻译中只包含您提供的代码部分,不包含其他内容。如果您还有其他需要翻译的内容或有任何问题,请随时提问。

英文:

I've been trying to implement Stripe with java for testing, but my doubts are step 5 "Create Charge with the token", link for more info.
https://stripe.com/docs/payments/accept-a-payment-charges

this is my code:

java controller

public void chrage(HttpServletRequest request, HttpServletResponse response)
{
try {
Stripe.apiKey = &quot;pk_test_51He5QLAnfgVAb3ehd0OxqrNHlQSrX6MApLFlchIXfrOby4JrDwndkI76CC5QGT96y5sjbT301oj2RiC9utOgH7so00GGAJaU3d&quot;;
// Token is created using Stripe Checkout or Elements!
// Get the payment token ID submitted by the form:
String token = request.getParameter(&quot;stripeToken&quot;);
ChargeCreateParams params =
ChargeCreateParams.builder()
.setAmount(999L)
.setCurrency(&quot;usd&quot;)
.setDescription(&quot;Example charge&quot;)
.setSource(token)
.build();
Charge charge = Charge.create(params.toMap());
} catch (AuthenticationException ex) {
Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidRequestException ex) {
Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
} catch (APIConnectionException ex) {
Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
} catch (CardException ex) {
Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
} catch (APIException ex) {
Logger.getLogger(FacturacionControlador.class.getName()).log(Level.SEVERE, null, ex);
}
}

jsp

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;script type=&quot;text/javascript&quot; src=&quot;https://js.stripe.com/v3/&quot;&gt;
var stripe = Stripe(&#39;pk_test_51He5QLAnfgVAb3ehd0OxqrNHlQSrX6MApLFlchIXfrOby4JrDwndkI76CC5QGT96y5sjbT301oj2RiC9utOgH7so00GGAJaU3d&#39;);
var elements = stripe.elements();
var style = {
base: {
fontSize: &#39;16px&#39;,
color: &#39;#32325d&#39;
}
};
var card = elements.create(&#39;card&#39;, {style: style});
card.mount(&#39;#card-element&#39;);
var form = document.getElementById(&#39;payment-form&#39;);
form.addEventListener(&#39;submit&#39;, function(event) {
event.preventDefault();
stripe.createToken(card).then(function (result) {
if (result.error) {
var errorElement = document.getElementById(&#39;card-errors&#39;);
errorElement.textContent = result.error.message;
} else {
stripeTokenHandler(result.token);
}
});
});
function stripeTokenHandler(token) {
var form = document.getElementById(&#39;payment-form&#39;);
var hiddenInput = document.createElement(&#39;input&#39;);
hiddenInput.setAttribute(&#39;type&#39;, &#39;hidden&#39;);
hiddenInput.setAttribute(&#39;name&#39;, &#39;stripeToken&#39;);
hiddenInput.setAttribute(&#39;value&#39;, token.id);
form.appendChild(hiddenInput);
form.submit();
}
&lt;/script&gt;

<!-- language: lang-css -->

&lt;style type=&quot;text/css&quot;&gt;
StripeElement {
box-sizing: border-box;
height: 40px;
padding: 10px 12px;
border: 1px solid transparent;
border-radius: 4px;
background-color: white;
box-shadow: 0 1px 3px 0 #e6ebf1;
-webkit-transition: box-shadow 150ms ease;
transition: box-shadow 150ms ease;
}
.StripeElement--focus {
box-shadow: 0 1px 3px 0 #cfd7df;
}
.StripeElement--invalid {
border-color: #fa755a;
}
.StripeElement--webkit-autofill {
background-color: #fefde5 !important;
}
&lt;/style&gt;

<!-- language: lang-html -->

&lt;div ng-controller=&quot;Controller&quot; class=&quot;right_col controlador&quot;&gt; 
&lt;form  id = &quot;frmCaptura&quot; data-toggle=&quot;validator&quot;&gt;
&lt;div class=&quot;x_panel&quot; &gt;
&lt;div class=&quot;x_title&quot;&gt;  
&lt;jopen:getLabel label=&quot;facturacion.nombre&quot;/&gt;
&lt;/div&gt;
&lt;div class=&quot;x_content&quot;&gt;
&lt;script src=&quot;https://js.stripe.com/v3/&quot;&gt;&lt;/script&gt;
&lt;form action=&quot;/charge&quot; method=&quot;post&quot; id=&quot;payment-form&quot;&gt;
&lt;div class=&quot;form-row&quot;&gt;
&lt;label for=&quot;card-element&quot;&gt;
Credit or debit card
&lt;/label&gt;
&lt;div id=&quot;card-element&quot;&gt;
&lt;!-- A Stripe Element will be inserted here. --&gt;
&lt;/div&gt;
&lt;!-- Used to display Element errors. --&gt;
&lt;div id=&quot;card-errors&quot; role=&quot;alert&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;button&gt;Submit Payment&lt;/button&gt;
&lt;/form&gt; 
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Could someone tell me what else is needed to make it work? I'd be really grateful

答案1

得分: 0

你似乎在服务器端代码中使用了一个可发布的密钥,这是行不通的 - 你需要在那里使用一个秘密密钥。

另外,你可能想考虑使用更新的支付意向方法,因为收款不支持全面的SCA。

英文:

You appear to be using a Publishable Key in your server-side code which won't work - you need to use a Secret Key there.

Also you may want to consider using the newer Payment Intents approach instead since Charges don't support SCA at all.

huangapple
  • 本文由 发表于 2020年10月23日 07:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定