小程序支付功能实现

1.小程序中js中写上一句请求代码这里,data的参数只需要“openid”,请成功后,执行success函数,在使用wx.requestPayment的小程序接口,在回到那里取出后台支付接口回调的参数设置到下面的timestamp以下的支付中,就可以成功调用支付的页面。

2.后台c#的代码,注释掉的代码不用看。

  private static TenPayV3Info _tenPayV3Info;
        public static TenPayV3Info TenPayV3Info
        {
            get
            {
                if (_tenPayV3Info == null)
                {
                    WeixinConfigInfo config = Meidie_MallReservationApplet.config.Config.WeixinConfig;
                    string NotifyUrl = config.CallBackUrl + "/Pay/wxPayResult";
                    _tenPayV3Info = new TenPayV3Info(config.AppID, config.AppSecret, config.MchId, config.AppKey, NotifyUrl, NotifyUrl);
                }
                return _tenPayV3Info;
            }
        }
  /// <summary>
        /// 接收支付结果通知
        /// </summary>
        /// <returns></returns>
        public ActionResult wxPayResult()
        {
            ResponseHandler resHandler = new ResponseHandler(null);

            string return_code = resHandler.GetParameter("return_code");
            string return_msg = resHandler.GetParameter("return_msg");

            string res = null;

            resHandler.SetKey(TenPayV3Info.Key);
            //验证请求是否从微信发过来(安全)
            if (resHandler.IsTenpaySign())
            {
                res = "success";
                //正确的订单处理
            }
            else
            {
                res = "wrong";
                //错误的订单处理
            }

            string xml = string.Format(@"<xml>
   <return_code><![CDATA[{0}]]></return_code>
   <return_msg><![CDATA[{1}]]></return_msg>
</xml>", return_code, return_msg);
            return Content(xml, "text/xml");
        }
try
    {
                通过,用code换取access_token
                //var openIdResult = OAuthApi.GetAccessToken(TenPayV3Info.AppId, TenPayV3Info.AppSecret, code);
                //if (openIdResult.errcode != ReturnCode.请求成功)
                //{
                //    return Json(new AjaxResult { data = null, message = "错误:" + openIdResult.errmsg });
                //}
                //时间戳
                string timeStamp = TenPayV3Util.GetTimestamp();
                string nonceStr = TenPayV3Util.GetNoncestr();
                //微信支付提交的XML Data数据模型
                TenPayV3UnifiedorderRequestData tpV3 = new TenPayV3UnifiedorderRequestData(
                    TenPayV3Info.AppId,//小程序/公众号ID
                    TenPayV3Info.MchId,//商户号
                    weixinconfig.WebTitle + "-" + productname,//商品描述
                    ordercode,//商户订单号
                    totalamount,//商品金额,以分为单位(money * 100)
                    Request.UserHostAddress,//用户的公网ip,不是商户服务器IP
                    TenPayV3Info.TenPayV3_WxOpenNotify,//接收财付通通知的回调URL
                    TenPayV3Type.JSAPI,//交易类型
                    //openIdResult.openid,//用户的openId
                    openid,//用户的openId
                    TenPayV3Info.Key,//商户支付密钥key,用于生成签名sign
                    nonceStr//随机字符串
                    );
                //调用微信支付[统一下单]接口
                var result = TenPayV3.Unifiedorder(tpV3);
                //接收返回的预支付订单ID
                string prepayId = result.prepay_id;
                //获取UI调用的支付签名
                string paySign = TenPayV3.GetJsPaySign(TenPayV3Info.AppId, timeStamp, nonceStr, "prepay_id=" + prepayId, TenPayV3Info.Key);
                //返回json提供给小程序调用
                var paydata = new
                {
                    timeStamp = timeStamp,
                    nonceStr = nonceStr,
                    prepay_id = prepayId,
                    paySign = paySign,
                };
                return Json(new AjaxResult { data = new { paydata }, message = "ok" + result.return_msg, state = tpV3 });
            }
            catch (Exception e)
            {
                return Json(new AjaxResult { data = null, message = e.Message });
            }
//微信退款提交的XML Data数据模型
TenPayV3RefundRequestData tpV3 = new TenPayV3RefundRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, TenPayV3Info.Key, "", Noncestr, "", ordercode, ordercode, totalamount, totalamount, TenPayV3Info.MchId, "");
var result = TenPayV3.Refund(tpV3, @weixinconfig.CretAbsolutePath, TenPayV3Info.MchId);
return Json(new AjaxResult { data = new { result }, message = "ok" });

 

3.注意事项:1.要有微信商户号和key秘钥,2.小程序要有申请开通好的支付接口,3.要到公众商户平台上绑定小程序的appid以及回调路径,4.所有的完成后就可以实现小程序的支付功能了。

4.成功图片展示事例:

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Spring Boot 2.0小程序支付功能实现weixin-java-pay的完整代码: 1. 引入weixin-java-pay依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-pay</artifactId> <version>3.8.0</version> </dependency> ``` 2. 配置微信支付参数 在application.yml或application.properties文件中配置微信支付参数,例如: ```yaml wx: pay: appId: xxx mchId: xxx mchKey: xxx notifyUrl: xxx ``` 3. 创建订单 在程序中创建订单,并将订单信息封装到UnifiedOrderRequest对象中,例如: ```java UnifiedOrderRequest request = new UnifiedOrderRequest(); request.setBody("测试订单"); request.setOutTradeNo("2018061500000001"); request.setTotalFee(1); request.setSpbillCreateIp("127.0.0.1"); request.setTradeType(TradeType.JSAPI); request.setOpenid("xxxxxx"); ``` 4. 调用微信支付接口 使用WxPayService对象调用微信支付接口,例如: ```java WxPayService wxPayService = new WxPayServiceImpl(); WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request); ``` 5. 获取支付参数 根据微信支付接口返回的结果,获取支付参数并返回给小程序,例如: ```java WxPayJsapiSignature signature = wxPayService.createJsapiSignature("prepay_id=" + result.getPrepayId(), false); Map<String, String> payParams = new HashMap<>(); payParams.put("appId", signature.getAppId()); payParams.put("timeStamp", signature.getTimestamp()); payParams.put("nonceStr", signature.getNonceStr()); payParams.put("package", signature.getPackageValue()); payParams.put("signType", signature.getSignType()); payParams.put("paySign", signature.getPaySign()); return payParams; ``` 完整代码如下: ```java @RestController @RequestMapping("/pay") public class PayController { @Autowired private WxPayService wxPayService; @Value("${wx.pay.appId}") private String appId; @PostMapping("/unifiedOrder") public Map<String, String> unifiedOrder(@RequestParam("openid") String openid, @RequestParam("totalFee") int totalFee) throws WxPayException { // 创建订单 UnifiedOrderRequest request = new UnifiedOrderRequest(); request.setBody("测试订单"); request.setOutTradeNo("2018061500000001"); request.setTotalFee(totalFee); request.setSpbillCreateIp("127.0.0.1"); request.setTradeType(TradeType.JSAPI); request.setOpenid(openid); // 调用微信支付接口 WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request); // 获取支付参数 WxPayJsapiSignature signature = wxPayService.createJsapiSignature("prepay_id=" + result.getPrepayId(), false); Map<String, String> payParams = new HashMap<>(); payParams.put("appId", signature.getAppId()); payParams.put("timeStamp", signature.getTimestamp()); payParams.put("nonceStr", signature.getNonceStr()); payParams.put("package", signature.getPackageValue()); payParams.put("signType", signature.getSignType()); payParams.put("paySign", signature.getPaySign()); return payParams; } @PostMapping("/notify") public String notify(HttpServletRequest request) throws WxPayException { // 处理支付结果通知 WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(request.getInputStream()); // 处理业务逻辑,并返回响应给微信支付系统 return WxPayNotifyResponse.success("处理成功"); } } ``` 其中,/pay/unifiedOrder接口用于创建订单并获取支付参数,/pay/notify接口用于处理支付结果通知。注意,支付结果通知的处理需要验签,可以使用WxPayService对象的checkSignature方法来验签,或者使用WxPayConfig对象的checkSignature方法来验签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值