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

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

W外链微信推广短连接怎么做?

制作微信推广链接的难点分析 一、内容创作难度 制作微信推广链接时,首先需要创作有吸引力的内容。这不仅要求内容本身有趣、有价值,还要能够激起人们的分享欲望。对于许多企业和个人来说,尤其是那些缺乏创意和写作能力的人来说,这是制作微信推广链接的一大难点。 二、精准定位难度 微信用户群体庞大,不同用户的需求和兴趣各异。因此,制作推广链接时需要精准定位目标受众,以便更有效地吸引他们点击并分享链接

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

EMLOG程序单页友链和标签增加美化

单页友联效果图: 标签页面效果图: 源码介绍 EMLOG单页友情链接和TAG标签,友链单页文件代码main{width: 58%;是设置宽度 自己把设置成与您的网站宽度一样,如果自适应就填写100%,TAG文件不用修改 安装方法:把Links.php和tag.php上传到网站根目录即可,访问 域名/Links.php、域名/tag.php 所有模板适用,代码就不粘贴出来,已经打

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中,不同电脑的配置和操作系统(如Win11与Win7)可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行,需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下,使LabVIEW开发的程序保持稳定运行的有效策略。 LabVIEW版本兼容性 LabVIEW各版本对不同操作系统的支持存在差异。因此,在开发程序时,尽量使用

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

这些心智程序你安装了吗?

原文题目:《为什么聪明人也会做蠢事(四)》 心智程序 大脑有两个特征导致人类不够理性,一个是处理信息方面的缺陷,一个是心智程序出了问题。前者可以称为“认知吝啬鬼”,前几篇文章已经讨论了。本期主要讲心智程序这个方面。 心智程序这一概念由哈佛大学认知科学家大卫•帕金斯提出,指个体可以从记忆中提取出的规则、知识、程序和策略,以辅助我们决策判断和解决问题。如果把人脑比喻成计算机,那心智程序就是人脑的

js异步提交form表单的解决方案

1.定义异步提交表单的方法 (通用方法) /*** 异步提交form表单* @param options {form:form表单元素,success:执行成功后处理函数}* <span style="color:#ff0000;"><strong>@注意 后台接收参数要解码否则中文会导致乱码 如:URLDecoder.decode(param,"UTF-8")</strong></span>

uniapp设置微信小程序的交互反馈

链接:uni.showToast(OBJECT) | uni-app官网 (dcloud.net.cn) 设置操作成功的弹窗: title是我们弹窗提示的文字 showToast是我们在加载的时候进入就会弹出的提示。 2.设置失败的提示窗口和标签 icon:'error'是设置我们失败的logo 设置的文字上限是7个文字,如果需要设置的提示文字过长就需要设置icon并给

Python:豆瓣电影商业数据分析-爬取全数据【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】

**爬取豆瓣电影信息,分析近年电影行业的发展情况** 本文是完整的数据分析展现,代码有完整版,包含豆瓣电影爬取的具体方式【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】   最近MBA在学习《商业数据分析》,大实训作业给了数据要进行数据分析,所以先拿豆瓣电影练练手,网络上爬取豆瓣电影TOP250较多,但对于豆瓣电影全数据的爬取教程很少,所以我自己做一版。 目