liujie
2023-05-26 f9de931c4457c2a6bfe395879e3b2f2bfd7d9692
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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 {
        HashMap<String, Object> map = new HashMap<>();
        map.put("number","4242424242424242");
        map.put("exp_month","12");
        map.put("exp_year","34");
        map.put("cvc","541");
//        String cardToken = createCardToken(map);
//        System.out.println(cardToken);
//
        String cus_ntikoSLGuD5goQ = updateStripeCustomWithCard("cus_NtikoSLGuD5goQ", map);
        System.out.println(cus_ntikoSLGuD5goQ);
//        boolean charge = charge("cus_NtikoSLGuD5goQ", new BigDecimal(0.01), "aud", "测试");
    }
 
    /**
     * 修改默认支付银行卡
     * @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)).longValue());
        payParams.put("currency", moneyType);
        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 "系统异常";
        }
    }
}