我试图让Stripe发送令牌,我的页面转到后台运行的java服务,这将完成所有处理。我不做很多网络工作,所以我很抱歉,如果答案非常明显,但我还没能在网上找到任何可以告诉我如何获得令牌并简单发送的简单方法它
这是我表单的代码
<form action="localhost:8080/api/charge" method="POST" id="monthButton">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_KEY"
data-allow-remember-me="true"
data-amount="2000"
data-description="1 month"
data-label="Purchase 1 month"
data-locale="auto"
data-name="Demo"
data-zip-code="true">
</script>
</form>
它只是坐在我的HTML页面的正文中。服务器还没有多少,我可以改变它的工作方式,但这就是我现在要听的内容
private static HttpServer startServer() {
final ResourceConfig resourceConfig = new ResourceConfig().packages("com.asc");
resourceConfig.register("javax.ws.rs.container.ContainerResponseFilter");
resourceConfig.register("com.asc.CrossDomainFilter");
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
}
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at %s/application.wadl\nHit enter to stop it...", BASE_URI));
final int in = System.in.read();
System.out.print(String.format("In: %d", in));
server.shutdown();
}
这个位基本上只是从here复制而来。
@Path("/api")
public class StripeResource {
@POST
@Path("/charge")
public void charge(HttpServletRequest request, HttpServletResponse response) {
System.out.print("Started");
// 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_KEY";
// Get the credit card details submitted by the form
String token = request.getParameter("stripeToken");
// Create a Customer
Map<String, Object> customerParams = new HashMap<String, Object>();
customerParams.put("source", token);
customerParams.put("description", "Example customer");
try {
Customer customer = Customer.create(customerParams);
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (CardException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
// Create the charge on Stripe's servers - this will charge the user's card
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 1000); // amount in cents, again
chargeParams.put("currency", "usd");
chargeParams.put("source", token);
chargeParams.put("description", "Example charge");
Map<String, String> initialMetadata = new HashMap<String, String>();
initialMetadata.put("order_id", "6735");
chargeParams.put("metadata", initialMetadata);
Charge charge = Charge.create(chargeParams);
} catch (CardException e) {
// The card has been declined
} catch (APIException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
}
}