stripe支付 旧版 charge付款方式 demo代码 可参考
/**
* @program: test
* @description: PaymentIntent api 保存客户和客户卡对象,授权冻结卡金额,结算进行捕获并解冻
* @author: 闲走天涯
* @create: 2021-09-01 10:22
* 支付流程
* 1.创建支付【status = requires_confirmation】
* 2.确认支付 【status = requires_capture】 注意:这里进行确认授权,如果是AUTOMATIC(自动捕获)则直接扣款,如果是 MANUAL(手动捕获),则进行授权冻结
* 3.捕获并解冻其余资金 【status = successed】
*/
public class PaymentIntentTest {
// key格式 sk_test开头为测试环境key,sk_live开头为生产环境key
private static final String privateKey = "test_key";
/**
* 创建stripe客户
* @param email
* @param name
* @return
*/
public static Customer createCustomer(String email,String name){
Stripe.apiKey = privateKey;
try {
CustomerCreateParams params = CustomerCreateParams.builder()
.setEmail(email)
.setName(name)
.build();
Customer customer = Customer.create(params);
return customer;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 删除stripe客户对象
* @param customerId stripe客户对象id
* @return
*/
public static Customer deleteCustomer(String customerId){
Stripe.apiKey = privateKey;
try {
//检索对象
Customer customer = Customer.retrieve(customerId);
//删除
Customer customer1 = customer.delete();
return customer1;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 创建客户支付方式(需要绑定客户对象)
* @param creditCard
* @param customerId stripe客户对象id
* @return
*/
public static PaymentMethod createPaymentMethod(CreditCard creditCard,String customerId){
Stripe.apiKey = privateKey;
try{
Map<String, Object> card = new HashMap<>();
card.put("exp_month",creditCard.getCardExpiryMonth());
card.put("exp_year",creditCard.getCardExpiryYear());
card.put("number",creditCard.getCardNo());
card.put("cvc",creditCard.getCvv());
Map<String, Object> params = new HashMap<>();
params.put("type", "card");
params.put("card", card);
PaymentMethod paymentMethod = PaymentMethod.create(params);
Map<String, Object> params2 = new HashMap<>();
params2.put("customer", customerId);
paymentMethod.attach(params2);
return paymentMethod;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 删除客户支付方式(绑定客户对象)
* @param paymentMethodId
* @return
*/
public static PaymentMethod deletePaymentMethod(String paymentMethodId){
Stripe.apiKey = privateKey;
try{
//检索支付方式
PaymentMethod paymentMethod = PaymentMethod.retrieve(paymentMethodId);
//从客户对象上分离
PaymentMethod paymentMethod1 = paymentMethod.detach();
return paymentMethod1;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 创建 支付(授权冻结卡资金或者 确认后直接捕获资金)
* @param orderCode 订单编号
* @param frozenAmount 需要授权的金额
* @param customerId stripe客户对象id
* @param paymentMethodId stripe支付方式id
* @return
*/
public static PaymentIntent createPaymentIntent(String orderCode,Double frozenAmount,String customerId,String paymentMethodId){
try{
Stripe.apiKey = privateKey;
//以分为单位
BigDecimal decimalAmount = new BigDecimal(frozenAmount);
decimalAmount = decimalAmount.multiply(new BigDecimal(100));
PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
.setAmount(Long.valueOf(decimalAmount.intValue()))
.setPaymentMethod(paymentMethodId)//绑定支付方式
.setCustomer(customerId)//绑定客户对象
.setDescription("订单编号:["+orderCode+"]")//支付说明(可用于订单流水查询)
.setCurrency("BRL")
.addPaymentMethodType("card")
.setConfirmationMethod(PaymentIntentCreateParams.ConfirmationMethod.MANUAL)
.setCaptureMethod(PaymentIntentCreateParams.CaptureMethod.MANUAL)//手动捕获 AUTOMATIC:自动捕获,确认后会扣款 MANUAL:手动捕获,可以在订单结算时进行捕获
.build();
PaymentIntent intent = PaymentIntent.create(params);
System.out.println("paymentIntent请求结果="+ JSON.toJSONString(intent));
return intent;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 确认订单 paymentIntent
* @param paymentIntentId stripe paymentIntent对象id
* @return
*/
public static PaymentIntent confirmPaymentIntent(String paymentIntentId){
try{
Stripe.apiKey = privateKey;
//检索
PaymentIntent intent = PaymentIntent.retrieve(paymentIntentId);
//确认
PaymentIntent intent1 = intent.confirm();
System.out.println("createPaymentIntent请求结果="+ JSON.toJSONString(intent1));
return intent1;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 捕获金额(进行扣款)
* @param paymentId stripe paymentIntent对象id
* @param captureAmount 需要扣款的金额
* @return
*/
public static PaymentIntent capturePaymentIntent(String paymentId,Double captureAmount){
try{
Stripe.apiKey = privateKey;
PaymentIntent intent2 = null;
if(captureAmount==0){
intent2 = PaymentIntentTest.cancelPaymentIntent(paymentId);
}else {
PaymentIntent intent = PaymentIntent.retrieve(paymentId);
//以分为单位
BigDecimal decimalAmount = new BigDecimal(captureAmount);
decimalAmount = decimalAmount.multiply(new BigDecimal(100));
PaymentIntentCaptureParams params = PaymentIntentCaptureParams.builder()
.setAmountToCapture(Long.valueOf(decimalAmount.intValue()))//捕获金额
.build();
intent2 = intent.capture(params);
}
System.out.println("capture请求结果="+ JSON.toJSONString(intent2));
return intent2;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 取消支付 paymentIntent
* @param paymentIntentId stripe paymentIntent对象id
* @return
*/
public static PaymentIntent cancelPaymentIntent(String paymentIntentId){
try{
Stripe.apiKey = privateKey;
//检索
PaymentIntent intent = PaymentIntent.retrieve(paymentIntentId);
//确认
PaymentIntent intent1 = intent.cancel();
System.out.println("cancelPaymentIntent请求结果="+ JSON.toJSONString(intent1));
return intent1;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 退款
* @param paymentId
* @param amount
* @return
*/
public static Refund refundPaymentIntent(String paymentId, Double amount){
try{
if(amount<=0){
System.out.println("金额不能低于等于0");
return null;
}
Stripe.apiKey = privateKey;
//以分为单位
BigDecimal refundAmount = new BigDecimal(amount);
refundAmount = refundAmount.multiply(new BigDecimal(100));
Map<String, Object> refundParam = new HashMap<>();
refundParam.put("payment_intent", paymentId);
refundParam.put("amount", refundAmount);
Refund refund = Refund.create(refundParam);
System.out.println("退款请求结果="+ JSON.toJSONString(refund));
if(refund!=null && VerifyData.strIsNotNull(refund.getId())) {
return refund;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 检索
* @param paymentId
* @return
*/
public static PaymentIntent retrievePaymentIntent(String paymentId){
try{
Stripe.apiKey = privateKey;
PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentId);
System.out.println("检索请求结果="+ JSON.toJSONString(paymentIntent));
if(paymentIntent!=null && VerifyData.strIsNotNull(paymentIntent.getId())) {
return paymentIntent;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws StripeException {
String orderCode = "201112115156181515";//订单编号
Double frozenAmount = 100d;//授权冻结的资金
Double captureAmout = 20d;//需要扣款的金额
CreditCard creditCard = new CreditCard();
creditCard.setCardNo("4242424242424242");
creditCard.setCardExpiryYear("2026");
creditCard.setCardExpiryMonth("8");
creditCard.setCvv("123");
creditCard.setEmail("[email protected]");
creditCard.setName("qatest123456");
//1.创建customer对象
Customer customer = PaymentIntentTest.createCustomer(creditCard.getEmail(),creditCard.getName());
//2.生成支付方式并绑定对象
PaymentMethod paymentMethod = PaymentIntentTest.createPaymentMethod(creditCard,customer.getId());
//3.发起支付
PaymentIntent paymentIntent = PaymentIntentTest.createPaymentIntent(orderCode,frozenAmount, customer.getId(), paymentMethod.getId());
//待绑定支付方式 创建paymentintent时未绑定或者超过7天自动返回requires_payment_method状态
if("requires_payment_method".equals(paymentIntent.getStatus())) {
Map<String, Object> params = new HashMap<>();
params.put("paymentMethod",paymentMethod.getId());
params.put("customer",customer.getId());
paymentIntent = paymentIntent.update(params);
}
//待确认
if("requires_confirmation".equals(paymentIntent.getStatus())) {
//4.进行确认 paymentIntent.status = requires_capture
PaymentIntent confirmIntent = PaymentIntentTest.confirmPaymentIntent(paymentIntent.getId());
//创建支付时的设置为AUTOMATIC AUTOMATIC:自动捕获,确认后会扣款 MANUAL:手动捕获
if ("succeeded".equals(confirmIntent.getStatus())) {//AUTOMATIC 自动捕获,此处会进行捕获并解冻
System.out.println("捕获的金额=" + confirmIntent.getAmountReceived() + ",剩余金额解冻成功");
} else if ("requires_capture".equals(confirmIntent.getStatus())) {//MANUAL:手动捕获
//5.捕获
PaymentIntent capturePaymentIntent = PaymentIntentTest.capturePaymentIntent(paymentIntent.getId(), captureAmout);
if ("succeeded".equals(capturePaymentIntent.getStatus())) {
System.out.println("捕获的金额=" + capturePaymentIntent.getAmountReceived() + ",剩余金额解冻成功");
}
}
}else if("requires_capture".equals(paymentIntent.getStatus())) {//待捕获
//冻结金额成功
// requires_capture 是在手动捕获情况下确认支付后的状态,如果是自动捕获,那么其状态会跳过requires_capture,直接到succeeded
//System.out.println("资金冻结失败");
//5.捕获
PaymentIntent capturePaymentIntent= PaymentIntentTest.capturePaymentIntent(paymentIntent.getId(),captureAmout);
if("succeeded".equals(capturePaymentIntent.getStatus())){
System.out.println("捕获的金额="+capturePaymentIntent.getAmountReceived()+",剩余金额解冻成功");
}
}else if("succeeded".equals(paymentIntent.getStatus())){
//支付成功,这里是支付 冻结金额,后面可通过扣款
System.out.println("捕获的金额="+paymentIntent.getAmountReceived()+",剩余金额解冻成功");
}else{
//冻结金额失败
System.out.println("资金冻结失败");
}
//6.退款
Refund refund = PaymentIntentTest.refundPaymentIntent(paymentIntent.getId(),captureAmout);
PaymentIntentTest.retrievePaymentIntent(paymentIntent.getId());
}
}
@Data
class CreditCard {
private String cardNo; //卡号
private String cardExpiryYear;//过期年
private String cardExpiryMonth;//过期月
private String cvv;
private String email;//邮箱
private String name;//名称
}