最近接了一个国外的微信小程序,要用到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支付微信小程序端完整解决方案相关推荐

  1. 微信小程序—微信小程序端支付代码

    只有微信小程序端的代码,如下 Page({data: {},onLoad: function (options) {// 页面初始化 options为页面跳转所带来的参数var that = this ...

  2. 外卖点餐列表滑动 微信小程序_外卖点餐微信小程序的详细解决方案

    随着移动互联网的发展,以及人们生活节奏的加快,工作生活之余,大家都习惯通过手机点餐.而这对于许多餐饮企业来说,就完全具备了通过长沙小程序开发,打造外卖点餐小程序的条件.那么接下来,创研科技就给大家谈谈 ...

  3. Node.js+MySQL开发的B2C商城系统源码+数据库(微信小程序端+服务端),界面高仿网易严选商城

    下载地址:Node.js+MySQL开发的B2C商城系统源码+数据库(微信小程序端+服务端) NideShop商城(微信小程序端) 界面高仿网易严选商城(主要是2016年wap版) 测试数据采集自网易 ...

  4. 【uniapp】微信小程序端总结

    文章目录 1.前言 2.uview-UI 3.异步 4.请求 5.全局数据共享 prototype Mixin vuex globalData 6.分包 6.1基本分包 6.2分包优化 6.3分包预加 ...

  5. 智慧校园:校务助手微信小程序端源码

    校园校务助手-智慧校园教师移动端 校园校务助手微信小程序端也指智慧校园教师端微信小程序 它包括哪些功能呢?我来介绍一下. 智慧校园教师端微信小程序是基于原生开发 教师端登录界面 ①.设备管理.通知管理 ...

  6. uni-app分割线微信小程序端不显示

    uni-app分割线微信小程序端不显示 文章目录 uni-app分割线微信小程序端不显示 问题描述 解决方案 问题描述 做项目时,遇到一个问题: 自定义的分割线组件在web端能显示,在微信小程序端却不 ...

  7. Vue3+Typescript+Vite 实现动态访问静态图片(含微信小程序端)

    前言:在最近新起的项目中,用到了较新的技术栈vue3.2+vite+ts,跟着网上的写法渐渐上手了,在菜单这一块我按照以往的写法,自己写了一个静态资源数据并用 require 包裹声明, 再以循环的方 ...

  8. 微信小程序 textarea 简易解决方案

    微信小程序 textarea 简易解决方案 参考文章: (1)微信小程序 textarea 简易解决方案 (2)https://www.cnblogs.com/bsyblog/p/6116973.ht ...

  9. uni-app 微信小程序端-AirKiss一键配网

    uni-app 微信小程序端-AirKiss一键配网 发现网上很多关于微信小程序配网的文章都是微信小程序原生开发,uni-app少之又少.这篇文章就介绍一下怎么在HBuilder X使用airkiss ...

最新文章

  1. mongodb 系列 ~ journal日志畅谈
  2. 如何使用Azure ML Studio开启机器学习
  3. VC++ 拖放编程简单Demo
  4. mysql列名可以用中文吗_用了这么久的MySQL,你知道它的存储引擎吗?
  5. 制造机器人的现状和发展趋势
  6. Java开发人员的十大戒律
  7. Openjudge-计算概论(A)-放苹果
  8. Git使用教程-idea系列中git使用教程
  9. Mac如何快速导出保存Pages文档里的图片
  10. linux安全与优化
  11. 【房价预测】基于matlab Elman神经网络房价预测【含Matlab源码 589期】
  12. python 网格搜索_网格搜索查找AUC参数
  13. 批处理清空文件夹内所有txt文件的内容
  14. 使用idea构建父子类springboot项目教程,并教你启动子项目(构建项目集合)
  15. Java 操作excel
  16. 谈《黑社会之龙城岁月》中之大D
  17. 计算机专业本科考教资可以考哪些,高中教师资格证计算机专业考什么内容
  18. 什么是以太坊大都会:终极指南
  19. mt4服务器修改,修改mt4服务器地址
  20. 零基础过五门CPA的一些经验及教训分享

热门文章

  1. C语言编译时产生的警告:initializing ‘char *‘ with an expression of type ‘const char *‘ discards qualifiers
  2. cv2.error: OpenCV(4.5.4-dev) :-1: error: (-5:Bad argument) in function ‘putText‘问题解决
  3. 5-9 打印倒直角三角形图案
  4. 递归算法实现二分查找
  5. 【detectron2】detectron2在ubuntu16.04系统下安装报错问题
  6. 供应链金融与贸易金融、商业保理、区块链的关系
  7. 中国能出现一家 Zoom 吗?
  8. DMA方式、中断方式的传输速率比较
  9. Scrapy 实战之爬取妹子图
  10. Python,菜菜,救救呜呜呜