Stripe 银行卡支付(Java)
一:概述
参考博客:https://blog.csdn.net/Sunshine_Moon/article/details/113867159?utm_medium=distribute.pc_relevant.none-task-blog-2defaultBlogCommendFromMachineLearnPai2default-10.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2defaultBlogCommendFromMachineLearnPai2default-10.control
官方文档:https://stripe.com/docs/api/payment_methods/object
二:代码示例
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StripTest {
private static String key="按官方文档获得key";
public static void main(String[] args) throws Exception {
//1.创建顾客 将生成 customerId 用以 strip用户
// String customer = createCustomer();
//2. 根据卡号创建 tokenId
tokenId();
//3.//根据tokenId创建card 绑定 customer
// card();
//4.支付
// stripePay();
// 5.退款
// stripeRefund(null);
}
//退款
public static Boolean stripeRefund(String chargeId) throws StripeException {
Stripe.apiKey = key;
Map<String, Object> params = new HashMap<>();
params.put("charge","chargeId");
params.put("amount",100);//属于int类型,金额为分
Refund refund = Refund.create(params);
if ("succeeded".equalsIgnoreCase(refund.getStatus())) {
System.out.println("true");
return true;
}else{
System.out.println("false");
return false;
}
}
//支付
public static void stripePay() throws StripeException {
Stripe.apiKey = key;
Map<String, Object> params = new HashMap<>();
//金额,实际金额*100,最小支付金额是0.5
params.put("amount", 2000);
//货币单位,小写,银行卡扣费货币换算stripe内部自行转换,我们不需要关心
params.put("currency", "aud");
// params.put("customer", "Customers对应id");
params.put("customer", "customerId");
params.put(
"description",
"My First Test Charge (created for API docs hf)"
);
//是否立即捕获费用。默认为true。如果为false,则费用会发出授权(或预授权),以后需要捕获。未捕获的费用将在7天内到期。
params.put("capture", true);
Charge charge = Charge.create(params);
//可以实时获取支付状态
if ("succeeded".equals(charge.getStatus())) {
//交易成功后,需要更新我们的订单表,修改业务参数,此处省略
System.out.println("pay success");
String id = charge.getId();
System.out.println(id);
} else {
System.out.println("pay failure");
}
}
//创建card 绑定 customer
public static String card() throws StripeException {
Stripe.apiKey = key;
Map<String, Object> retrieveParams = new HashMap<>();
List<String> expandList = new ArrayList<>();
expandList.add("sources");
retrieveParams.put("expand", expandList);
Customer customer =
Customer.retrieve(
"customerId",
retrieveParams,
null
);
Map<String, Object> params = new HashMap<>();
params.put("source", "tokenId");
Card card =
(Card) customer.getSources().create(params);
//建议将卡号保存下来
String cardId = card.getId();
System.out.println(cardId);
return cardId;
}
//创建tokenId
public static String tokenId() throws StripeException {
Stripe.apiKey = key;
Map<String, Object> card = new HashMap<>();
card.put("number", "4242424242424242");
card.put("exp_month", "2");
card.put("exp_year", "2022");
card.put("cvc", "314");
Map<String, Object> params = new HashMap<>();
params.put("card", card);
Token token = Token.create(params);
//在后面创建银行卡时可以使用,传参过程无需再填写银行卡相关信息
String tokenId = token.getId();
System.out.println(tokenId);
return tokenId;
}
//创建customerId
public static String createCustomer() throws StripeException {
Stripe.apiKey = key;
//创建客户时的参数,方便后面识别,具体参数详见api,这里的话推荐description参数必传,这里的值会展示在stripe平台上方便后面观察
Map<String, Object> params = new HashMap<>();
params.put(
"description",
"My First Test Customer (created for API docs hf)"
);
Customer customer = Customer.create(params);
//stripe平台Customers的唯一标识
String customerId = customer.getId();
System.out.println(customerId);
return customerId;
}
}