本文主要是介绍Stripe支付微信小程序端完整解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近接了一个国外的微信小程序,要用到Stripe支付,微信小程序本身是推荐微信支付的,所以Stripe支付完全是由后端处理,话不多说上代码。
stripe依赖
<!-- stripe --><dependency><groupId>com.stripe</groupId><artifactId>stripe-java</artifactId><version>16.4.0</version></dependency>
Controller层
/*** 发起支付** @param request* @param param* @return* @date 2019年2月12日* @*/@Authorize@RequestMapping(value = "/payment", method = RequestMethod.POST)@ResponseBodypublic CommonResult payment(HttpServletRequest request, @RequestBody OmsOrderParam param) {UmsMember user = (UmsMember)request.getAttribute(Constant.user);param.setUserId(user.getId());param.setUserId(user.getId());param.setStripeChargeId(user.getStripeChargeId());param.setOpenId(user.getOpenId());Map<String, Object> res = omsPayService.payment(param,true);return CommonResult.success(res);}/*** 获取用户卡片列表** @return*/@Authorize@RequestMapping(value = "/getCardList", method = RequestMethod.POST)@ResponseBodypublic CommonResult getCardList(HttpServletRequest request) {UmsMember user = (UmsMember)request.getAttribute(Constant.user);List<StripePayResult> list = omsPayService.getCardList(user.getStripeChargeId());return CommonResult.success(list);}/*** 选择默认卡** @return*/@Authorize@RequestMapping(value = "/defaultSource", method = RequestMethod.POST)@ResponseBodypublic CommonResult defaultSource(HttpServletRequest request, @RequestBody StripePayParam stripePayParam) {UmsMember user = (UmsMember)request.getAttribute(Constant.user);stripePayParam.setUserId(user.getId());stripePayParam.setStripeChargeId(user.getStripeChargeId());boolean result = omsPayService.defaultSource(stripePayParam);return CommonResult.result(result);}/*** 添加用户卡片** @return*/@Authorize@RequestMapping(value = "/addCard", method = RequestMethod.POST)@ResponseBodypublic CommonResult addCard(HttpServletRequest request, @RequestBody StripePayParam stripePayParam) {UmsMember user = (UmsMember)request.getAttribute(Constant.user);stripePayParam.setUserId(user.getId());stripePayParam.setStripeChargeId(user.getStripeChargeId());boolean result = omsPayService.addCard(stripePayParam);return CommonResult.result(result);}/*** 删除卡片** @return*/@Authorize@RequestMapping(value = "/delCard", method = RequestMethod.POST)@ResponseBodypublic CommonResult delCard(HttpServletRequest request, @RequestBody StripePayParam stripePayParam) {UmsMember user = (UmsMember)request.getAttribute(Constant.user);stripePayParam.setUserId(user.getId());stripePayParam.setStripeChargeId(user.getStripeChargeId());boolean result = omsPayService.delCard(stripePayParam);return CommonResult.result(result);}
实现
import com.alibaba.fastjson.JSON;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.*;
import com.uslife.common.constant.StripeConstant;
import com.uslife.common.exception.ApiException;
import com.uslife.dto.StripePayParam;
import com.uslife.dto.StripePayResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @description: Stripe* @author: fun* @create: 2020-03-27 20:48*/
public class StripeUtil {private static Logger logger = LoggerFactory.getLogger(StripeUtil.class);public static Token createToken(StripePayParam payParam) {Stripe.apiKey = StripeConstant.apiKey;try {Map<String, Object> card = new HashMap<>();card.put("number", payParam.getNumber());card.put("exp_month", payParam.getExpMonth());card.put("exp_year", payParam.getExpYear());card.put("cvc", payParam.getCvc());Map<String, Object> params = new HashMap<>();params.put("card", card);Token token = Token.create(params);logger.info("token res:" + JSON.toJSONString(token));return token;} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}}public static String createCustomer(StripePayParam payParam) {Stripe.apiKey = StripeConstant.apiKey;try {Map<String, Object> params = new HashMap<>();params.put("name", payParam.getName());params.put("source", StripeUtil.createToken(payParam).getId());if (!StringUtils.isEmpty(payParam.getLine1())&&!StringUtils.isEmpty(payParam.getCity())&&!StringUtils.isEmpty(payParam.getCountry())&&!StringUtils.isEmpty(payParam.getPostalCode())&&!StringUtils.isEmpty(payParam.getState())&&!StringUtils.isEmpty(payParam.getShippingName())) {Map<String, Object> address = new HashMap<>();address.put("line1", payParam.getLine1());address.put("city", payParam.getCity());address.put("country", payParam.getCountry());address.put("line2", payParam.getLine2());address.put("postal_code", payParam.getPostalCode());address.put("state", payParam.getState());Map<String, Object> shipping = new HashMap<>();shipping.put("name", payParam.getShippingName());shipping.put("address", address);params.put("address", address);params.put("shipping", shipping);}logger.info("createCustomer params:" + JSON.toJSONString(params));Customer customer = Customer.create(params);logger.info("createCustomer res:" + JSON.toJSONString(customer));if (customer != null) {return customer.getId();}} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}return null;}public static String createCard(StripePayParam payParam) {Stripe.apiKey = StripeConstant.apiKey;try {Customer customer = Customer.retrieve(payParam.getStripeChargeId());Map<String, Object> params = new HashMap<>();params.put("source", StripeUtil.createToken(payParam).getId());Card card = (Card) customer.getSources().create(params);logger.info("token res:" + JSON.toJSONString(card));return card.getCustomer();} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}}public static boolean delCard(StripePayParam payParam) {Stripe.apiKey = StripeConstant.apiKey;try {Customer customer = Customer.retrieve(payParam.getStripeChargeId());Card card = (Card) customer.getSources().retrieve(payParam.getCardId());Card deletedCard = card.delete();logger.info("token res:" + JSON.toJSONString(deletedCard));return deletedCard.getDeleted();} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}}public static Boolean defaultSource(StripePayParam payParam) {Stripe.apiKey = StripeConstant.apiKey;try {Customer customer = Customer.retrieve(payParam.getStripeChargeId());System.out.println("给客户修改默认卡号");Map<String, Object> tokenParam = new HashMap<String, Object>();tokenParam.put("default_source", payParam.getCardId());customer.update(tokenParam);logger.info("updateCustomer res:" + JSON.toJSONString(customer));return true;} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}}public static List<StripePayResult> getCardList(String stripeChargeId) {Stripe.apiKey = StripeConstant.apiKey;List list = new ArrayList<>();try {Map<String, Object> params = new HashMap<>();params.put("limit", 5);params.put("object", "card");Customer customer = Customer.retrieve(stripeChargeId);List cardList = customer.getSources().list(params).getData();
// List cardList = Customer.retrieve(stripeChargeId).list(params).getData();logger.info("getCardList res:" + JSON.toJSONString(cardList));for (Object p : cardList) {StripePayResult result = new StripePayResult();Card c = (Card) p;result.setLast4(c.getLast4());result.setExpYear(c.getExpYear());result.setExpMonth(c.getExpMonth());result.setCardId(c.getId());result.setDefaultSource(c.getId().equals(customer.getDefaultSource()));list.add(result);}} catch (Exception e) {e.printStackTrace();}return list;}public static Map charge(String amount,String stripeChargeId){Stripe.apiKey = StripeConstant.apiKey;try {//发起支付Map<String, Object> payParams = new HashMap<>();payParams.put("amount", amount);payParams.put("currency", StripeConstant.currency);payParams.put("customer", stripeChargeId);Charge charge = Charge.create(payParams);logger.info("charge res:" + JSON.toJSONString(charge));//charge 支付是同步通知if ("succeeded".equals(charge.getStatus())) {Map<String, Object> result = new HashMap<>();result.put("id", charge.getId());result.put("amount", charge.getAmount());return result;}} catch (Exception e) {e.printStackTrace();throw new ApiException(e.getMessage());}return null;}public static String createRefund(String chargeId, String amount) {Stripe.apiKey = StripeConstant.apiKey;try {Map<String, Object> params = new HashMap<>();params.put("charge", chargeId);params.put("amount", amount);Refund refund = Refund.create(params);logger.info("createRefund res:" + JSON.toJSONString(refund));if ("succeeded".equals(refund.getStatus())) {return refund.getId();}} catch (StripeException e) {e.printStackTrace();throw new ApiException(e.getMessage());}return null;}}
以上就完成了,欢迎大家交流学习
这篇关于Stripe支付微信小程序端完整解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!