Uniapp微信小程序开发:微信小程序支付功能后台代码

需要的组件:

SKIT.FlurlHttpClient.Wechat.TenpayV3

代码示例

csharp 复制代码
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;

namespace MyWxPay.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TenPayApiV3Controller : ControllerBase
    {
        [HttpGet("CreateOrder")]
        public async Task<IActionResult> CreateOrder()
        {
            var options = new WechatTenpayClientOptions()
            {
                MerchantId = "微信商户号",
                MerchantV3Secret = "微信商户 v3 API 密钥",
                MerchantCertificateSerialNumber = "微信商户证书序列号",
                MerchantCertificatePrivateKey = System.IO.File.ReadAllText("/微信商户证书私钥文件路径/apiclient_key.pem"),
                PlatformAuthScheme = PlatformAuthScheme.PublicKey,
                PlatformPublicKeyManager = new InMemoryPublicKeyManager()
            };
             var client = WechatTenpayClientBuilder.Create(options).Build();
             var openId = "下单的人的微信OpenId";
             
            var request = new CreatePayTransactionJsapiRequest()
            {
                OutTradeNumber = "设置订单号,一般是传入自己内部系统的订单号",
                AppId = "微信小程序的Appid",
                Description = "测试商品",
                NotifyUrl = "写的支付成功的回调地址,必须是https",
                Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = 设置金额 注意单位是分 },
                Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = openId }
            };
                        var response = await client.ExecuteCreatePayTransactionJsapiAsync(request);
            if (response.IsSuccessful())
            {
                Console.WriteLine("PrepayId:" + response.PrepayId);
                List<IDictionary<string, string>> keyValuePairs = new List<IDictionary<string, string>>();

                //传入小程序的appid及微信返回的预支付ID获取想要返回给前端的数据
                var paramMap = client.GenerateParametersForJsapiPayRequest(request.AppId, response.PrepayId);
                keyValuePairs.Add(paramMap);
                IDictionary<string, string> orderdic = new Dictionary<string, string> { { "orderid", "request.OutTradeNumber" } };
                keyValuePairs.Add(orderdic);
                return Ok(keyValuePairs);
            }
            else
            {
                return BadRequest(response.ErrorMessage);
            }

    }
}
相关推荐
2501_9159184118 小时前
App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
说私域19 小时前
定制开发开源AI智能名片S2B2C商城小程序中的羊群效应应用研究
人工智能·小程序
一匹电信狗19 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
CsharpDev-奶豆哥20 小时前
微信小程序通过主键ID修改json数据的技术分享
微信小程序·小程序·json
汤姆yu20 小时前
基于微信小程序的防诈骗管理系统
微信小程序·小程序·防诈骗管理
2501_9160074720 小时前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
2501_9160088921 小时前
前端工具全景实战指南,从开发到调试的效率闭环
android·前端·小程序·https·uni-app·iphone·webview
一路向前的月光1 天前
uniapp(5)滚动列表scroll-view
前端·javascript·uni-app
2501_915921431 天前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹1 天前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友