Stripe支付微信小程序端完整解决方案

2024-05-10 09:08

本文主要是介绍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支付微信小程序端完整解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/976005

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

C#文件复制异常:"未能找到文件"的解决方案与预防措施

《C#文件复制异常:未能找到文件的解决方案与预防措施》在C#开发中,文件操作是基础中的基础,但有时最基础的File.Copy()方法也会抛出令人困惑的异常,当targetFilePath设置为D:2... 目录一个看似简单的文件操作问题问题重现与错误分析错误代码示例错误信息根本原因分析全面解决方案1. 确保

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果