英文:
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 = "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);
}
}
jsp
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<script type="text/javascript" src="https://js.stripe.com/v3/">
var stripe = Stripe('pk_test_51He5QLAnfgVAb3ehd0OxqrNHlQSrX6MApLFlchIXfrOby4JrDwndkI76CC5QGT96y5sjbT301oj2RiC9utOgH7so00GGAJaU3d');
var elements = stripe.elements();
var style = {
base: {
fontSize: '16px',
color: '#32325d'
}
};
var card = elements.create('card', {style: style});
card.mount('#card-element');
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function (result) {
if (result.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
stripeTokenHandler(result.token);
}
});
});
function stripeTokenHandler(token) {
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
form.submit();
}
</script>
<!-- language: lang-css -->
<style type="text/css">
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;
}
</style>
<!-- language: lang-html -->
<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>
</div>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论