liujie
2023-10-15 871efa21e6c95520e9825ae1f2338c9a919fdd5d
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
package com.stylefeng.guns.modular.system.controller;
 
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.gson.Gson;
import com.stripe.Stripe;
import com.stripe.exception.SignatureVerificationException;
import com.stripe.exception.StripeException;
import com.stripe.model.*;
import com.stripe.net.ApiResource;
import com.stripe.net.Webhook;
import com.stripe.param.CustomerCreateParams;
import com.stripe.param.PriceCreateParams;
import com.stripe.param.ProductCreateParams;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.model.TUser;
import com.stylefeng.guns.modular.system.model.TUserBank;
import com.stylefeng.guns.modular.system.model.TUserBankAddDto;
import com.stylefeng.guns.modular.system.service.ITUserService;
import com.stylefeng.guns.modular.system.service.TUserBankService;
import com.stylefeng.guns.modular.system.utils.StripePayUtils;
import com.stylefeng.guns.modular.system.utils.tips.ErrorTip;
import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
import io.github.cdimascio.dotenv.Dotenv;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.smartcardio.Card;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;
 
import static spark.Spark.*;
 
@Controller
@Api(tags = "客户端---银行卡")
@RequestMapping("/api/bank")
public class UserBankController {
    @Resource
    private TUserBankService tUserBankService;
 
    @Resource
    private ITUserService userService;
 
    @Autowired
    private StripePayUtils stripePayUtils;
 
 
 
    /**
     * 获取列表
     */
    @ApiOperation(value = "用户银行卡列表",notes="用户银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageNumber", value = "pageNumber", required = true, dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "int",paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public Object list(int userId,int pageNumber,int pageSize) {
        Page<TUserBank> bankPage = new Page<>(pageNumber, pageSize);
        Page<TUserBank> tUserBankPage = tUserBankService.selectPage(bankPage, new EntityWrapper<TUserBank>().eq("user_id", userId));
        TUser tUser = userService.selectById(userId);
        String companyName = tUser.getCompanyName();
        String phone = tUser.getPhone();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",companyName);
        map.put("phone",phone);
        map.put("cards",tUserBankPage);
        return new SuccessTip(map);
    }
 
    @ApiOperation(value = "根据id获取银行卡详情",notes="根据id获取银行卡详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "id", value = "卡id", required = true, dataType = "int",paramType = "query"),
    })
    @GetMapping(value = "/cardInfo")
    @ResponseBody
    public Object cardInfo(int id) {
        TUserBank tUserBank = tUserBankService.selectById(id);
        return new SuccessTip(tUserBank);
    }
 
 
    @ApiOperation(value = "添加银行卡",notes="添加银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @PostMapping(value = "/addCard")
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Object addCard(@RequestBody TUserBankAddDto tUserBank) {
        Integer userId = tUserBank.getUserId();
        TUser tUser = userService.selectById(userId);
        String createUser =null;
        if(ToolUtil.isEmpty(tUser.getUserNumber())){
            // 需要创建初始用户
            try {
                 createUser = stripePayUtils.createStripeCustomNoCard(tUser.getAccount());
                tUser.setUserNumber(createUser);
                userService.updateById(tUser);
            } catch (StripeException e) {
                e.printStackTrace();
                return new ErrorTip(500,e.getMessage());
            }
        }else {
            createUser=tUser.getUserNumber();
        }
        tUserBank.setCreateTime(new Date());
        TUserBank tUserBank1 = new TUserBank();
        BeanUtil.copyProperties(tUserBank,tUserBank1);
 
        HashMap<String, Object> map = new HashMap<>();
        map.put("number",tUserBank.getAccountNumber());
        map.put("exp_month",tUserBank.getMonth());
        map.put("exp_year",tUserBank.getYear());
        map.put("cvc",tUserBank.getCvc());
        try {
            String cardToken = stripePayUtils.updateStripeCustomWithCard(createUser,map);
            tUserBank1.setCardToken(cardToken);
            tUserBank1.setUserNumber(createUser);
            tUserBankService.insert(tUserBank1);
            return new SuccessTip();
        } catch (StripeException e) {
            e.printStackTrace();
            return new ErrorTip(500,e.getMessage());
        }
 
    }
 
    private void cardBind() throws StripeException {
        Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
 
        Map<String, Object> retrieveParams =
                new HashMap<>();
        List<String> expandList = new ArrayList<>();
        expandList.add("sources");
        retrieveParams.put("expand", expandList);
        Customer customer =
                Customer.retrieve(
                        "cus_9s6XWPuHZWFcfK",
                        retrieveParams,
                        null
                );
 
        Map<String, Object> params = new HashMap<>();
        params.put("source", "tok_mastercard");
 
        Card card =
                (Card) customer.getSources().create(params);
    }
 
    private void card() throws StripeException {
        Stripe.apiKey ="";
 
        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);
    }
 
 
    private static Gson gson = new Gson();
 
    static class CreatePaymentResponse {
        private String publishableKey;
        private String clientSecret;
 
        public CreatePaymentResponse(String publishableKey, String clientSecret) {
            this.publishableKey = publishableKey;
            this.clientSecret = clientSecret;
        }
    }
 
    public static void main(String[] args) throws StripeException {
        Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
 
 
        ProductCreateParams productParams =
                ProductCreateParams.builder()
                        .setName("Starter Subscription")
                        .setDescription("10/Month subscription")
                        .build();
        Product product = Product.create(productParams);
        System.out.println("Success! Here is your starter subscription product id: " + product.getId());
 
        PriceCreateParams params =
                PriceCreateParams
                        .builder()
                        .setProduct(product.getId())
                        .setCurrency("usd")
                        .setUnitAmount(1200L)
                        .setRecurring(
                                PriceCreateParams.Recurring
                                        .builder()
                                        .setInterval(PriceCreateParams.Recurring.Interval.MONTH)
                                        .build())
                        .build();
        Price price = Price.create(params);
        System.out.println("Success! Here is your starter subscription price id: " + price.getId());
    }
}