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

相关文章

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM