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 bankPage = new Page<>(pageNumber, pageSize); Page tUserBankPage = tUserBankService.selectPage(bankPage, new EntityWrapper().eq("user_id", userId)); TUser tUser = userService.selectById(userId); String companyName = tUser.getCompanyName(); String phone = tUser.getPhone(); HashMap 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 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 retrieveParams = new HashMap<>(); List expandList = new ArrayList<>(); expandList.add("sources"); retrieveParams.put("expand", expandList); Customer customer = Customer.retrieve( "cus_9s6XWPuHZWFcfK", retrieveParams, null ); Map params = new HashMap<>(); params.put("source", "tok_mastercard"); Card card = (Card) customer.getSources().create(params); } private void card() throws StripeException { Stripe.apiKey =""; Map card = new HashMap<>(); card.put("number", "4242424242424242"); card.put("exp_month", 2); card.put("exp_year", 2022); card.put("cvc", "314"); Map 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()); } }