package com.stylefeng.guns.modular.system.utils;
|
|
import com.stripe.Stripe;
|
import com.stripe.exception.StripeException;
|
import com.stripe.model.Card;
|
import com.stripe.model.Charge;
|
import com.stripe.model.Customer;
|
import com.stripe.model.Token;
|
import org.springframework.stereotype.Component;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* stripe 支付工具类
|
*
|
* @author zhouquan
|
* @date 2021-01-26 13:44:00
|
*/
|
@Component
|
public class StripePayUtils {
|
|
// private static final String key = "sk_test_51Mu5D0KDN0sswRVwUNL5998QrX1VJkNrLsflVUXkEFH0cY2l9StrCc5O4c9gtzs7tLbVmeaTfyNL6pnrtw1tRClA00ZVfbNb3d";
|
private static final String key = "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";
|
|
/**
|
* 创建初始用户
|
* @param description 描述-用户编号
|
* @return
|
* @throws StripeException
|
*/
|
public static String createStripeCustomNoCard(String description) throws StripeException {
|
Stripe.apiKey = key;
|
Map<String, Object> params = new HashMap<>();
|
params.put( "description", description);
|
Customer customer = Customer.create(params);
|
return customer.getId();
|
}
|
|
|
/**
|
* 更新用户绑定银行卡
|
* @param stripeMemberId 客户stripeId
|
* @param cardParam 银行卡参数
|
* @return
|
* @throws StripeException
|
*/
|
public static String updateStripeCustomWithCard(String stripeMemberId, Map<String, Object> cardParam) throws StripeException {
|
Stripe.apiKey = key;
|
//创建银行卡Token,顺便借用stripe内部验证校验银行卡信息是否正确
|
String cardTokenId = createCardToken(cardParam);
|
Map<String, Object> retrieveParams = new HashMap<>();
|
List<String> expandList = new ArrayList<>();
|
expandList.add("sources");
|
retrieveParams.put("expand", expandList);
|
Customer customer = Customer.retrieve(stripeMemberId, retrieveParams,null);
|
|
Map<String, Object> params = new HashMap<>();
|
params.put("source", cardTokenId);
|
Card card = (Card) customer.getSources().create(params);
|
return card.getId();
|
}
|
// cus_NtikoSLGuD5goQ tok_1N8HCEKDN0sswRVwd8fQmIq6
|
public static void main(String[] args) throws StripeException {
|
|
// String stripeCustomNoCard = createStripeCustomNoCard("286988043@qq.com");
|
// System.out.println(stripeCustomNoCard);
|
// HashMap<String, Object> map = new HashMap<>();
|
// map.put("number","4147202602480020");
|
// map.put("exp_month","04");
|
// map.put("exp_year","28");
|
// map.put("cvc","541");
|
// String cardToken = createCardToken(map);
|
// System.out.println(cardToken);
|
//
|
// String cus_ntikoSLGuD5goQ = updateStripeCustomWithCard("cus_OdnGgggJJ7iEJh", map);
|
// System.out.println(cus_ntikoSLGuD5goQ);
|
// boolean charge = charge("cus_OdnGgggJJ7iEJh", new BigDecimal(0.01), "aud", "测试");
|
|
try {
|
boolean charge = charge("cus_OdnGgggJJ7iEJh", new BigDecimal(1), "c", "cs");
|
System.out.println(charge);
|
} catch (StripeException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
/**
|
* 修改默认支付银行卡
|
* @param stripeMemberId 客户stripeId
|
* @param stripeCardId 银行卡stripeId
|
* @throws StripeException
|
*/
|
public static void updateStripeDefaultCard(String stripeMemberId, String stripeCardId) 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(stripeMemberId, retrieveParams,null);
|
|
Map<String, Object> params = new HashMap<>();
|
params.put("default_source", stripeCardId);
|
customer.update(params);
|
}
|
|
|
/**
|
* 删除用户绑定银行卡
|
* @param stripeMemberId 客户stripeId
|
* @param cardId 银行卡stripeId
|
* @return
|
* @throws StripeException
|
*/
|
public void deleteCard(String stripeMemberId, String cardId) 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( stripeMemberId, retrieveParams, null );
|
|
Card card = (Card) customer.getSources().retrieve(cardId);
|
|
Card deletedCard = (Card) card.delete();
|
}
|
|
/**
|
* 生成卡token(鉴定卡对不对)
|
* @param cardParam number 卡号 exp_month 有效期月份 exp_year 有效期年份 cvc 安全码
|
* @return
|
* @throws StripeException
|
*/
|
public static String createCardToken(Map<String, Object> cardParam) throws StripeException {
|
Stripe.apiKey = key;
|
//生成卡片token
|
Map<String, Object> params = new HashMap<>();
|
params.put("card", cardParam);
|
Token token = Token.create(params);
|
return token.getId();
|
}
|
|
/**
|
* 修改银行卡信息
|
* @param stripeMemberId 客户stripeId
|
* @param stripeCardId 银行卡stripeId
|
* @param params 需要修改的参数
|
* @throws StripeException
|
*/
|
public void updateCard(String stripeMemberId, String stripeCardId,Map<String, Object> params) 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(stripeMemberId, retrieveParams,null);
|
|
Card card = (Card) customer.getSources().retrieve(stripeCardId);
|
|
card.update(params);
|
}
|
|
/**
|
* 同步支付
|
* @param stripeMemberId 客户stripeId
|
* @param money 实际支付金额
|
* @param moneyType 货币单位
|
* @param description 订单描述
|
* @return
|
* @throws StripeException
|
*/
|
public static boolean charge(String stripeMemberId, BigDecimal money, String moneyType, String description) throws StripeException {
|
Stripe.apiKey = key;
|
|
//发起支付
|
Map<String, Object> payParams = new HashMap<>();
|
//1元=100
|
payParams.put("amount", (money.multiply(new BigDecimal(100)).multiply(new BigDecimal(1.035))).longValue());
|
payParams.put("currency", "usd");
|
payParams.put("description", description);
|
payParams.put("customer", stripeMemberId);
|
payParams.put("capture", true);
|
Charge charge = Charge.create(payParams);
|
System.err.println(charge.toString());
|
//charge 支付是同步通知
|
if ("succeeded".equals(charge.getStatus())) {
|
//交易成功后,需要更新我们的订单表,修改业务参数,此处省略
|
return true;
|
} else {
|
String s = catchError(charge.getStatus());
|
System.err.println(s);
|
return false;
|
}
|
}
|
|
|
|
|
/**
|
* 捕获stripe平台返回的错误状态码
|
* @param code
|
*/
|
public static String catchError(String code){
|
switch (code){
|
case "account_number_invalid":
|
//银行卡号不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_account_number_invalid.getValue());
|
return "银行卡号不正确";
|
case "balance_insufficient":
|
//账户余额不足
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_balance_insufficient.getValue());
|
return "账户余额不足";
|
case "bank_account_declined":
|
//所提供的银行帐户尚未通过验证或不受支持,因此无法用于收费
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_bank_account_declined.getValue());
|
return "所提供的银行帐户尚未通过验证或不受支持,因此无法用于收费";
|
case "bank_account_exists":
|
//账户已存在
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_bank_account_exists.getValue());
|
return "账户已存在";
|
case "bank_account_unusable":
|
//提供的银行帐户不能用于付款。必须使用其他银行帐户。
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_bank_account_unusable.getValue());
|
return "提供的银行帐户不能用于付款。必须使用其他银行帐户。";
|
case "expired_card":
|
//卡已过期
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_expired_card.getValue());
|
return "卡已过期";
|
case "incorrect_cvc":
|
//卡的安全码不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_incorrect_cvc.getValue());
|
return "卡的安全码不正确";
|
case "incorrect_number":
|
//卡号不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_incorrect_number.getValue());
|
return "卡号不正确";
|
case "incorrect_zip":
|
//卡的邮政编码不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_incorrect_zip.getValue());
|
return "卡的邮政编码不正确";
|
case "instant_payouts_unsupported":
|
//此卡不支持即时付款
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_instant_payouts_unsupported.getValue());
|
return "此卡不支持即时付款";
|
case "invalid_cvc":
|
//卡的安全码无效
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_invalid_cvc.getValue());
|
return "卡的安全码无效";
|
case "invalid_expiry_month":
|
//卡的有效期限不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_invalid_expiry_month.getValue());
|
return "卡的有效期限不正确";
|
case "invalid_expiry_year":
|
//卡的有效期不正确
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_invalid_expiry_year.getValue());
|
return "卡的有效期不正确";
|
case "invalid_number":
|
//卡号无效
|
// throw new RRException(null, ErrorPromptEnum.BANK_STRIPE_invalid_number.getValue());
|
return "卡号无效";
|
default:
|
//系统异常
|
// throw new RRException(code, ErrorPromptEnum.BANK_STRIPE_error.getValue());
|
return "系统异常";
|
}
|
}
|
}
|