فهرست منبع

[WebApplicationSample] 新增 MiniProgramPay

Roc 4 سال پیش
والد
کامیت
08e32ec3f3

+ 52 - 0
samples/WebApplicationSample/Controllers/WeChatPayV3Controller.cs

@@ -214,6 +214,58 @@ namespace WebApplicationSample.Controllers
             return View();
         }
 
+        /// <summary>
+        /// 小程序支付-JSAPI下单
+        /// </summary>
+        [HttpGet]
+        public IActionResult MiniProgramPay()
+        {
+            return View();
+        }
+
+        /// <summary>
+        /// 小程序支付-JSAPI下单
+        /// </summary>
+        /// <param name="viewModel"></param>
+        [HttpPost]
+        public async Task<IActionResult> MiniProgramPay(WeChatPayPubPayV3ViewModel viewModel)
+        {
+            var model = new WeChatPayTransactionsJsApiBodyModel
+            {
+                AppId = _optionsAccessor.Value.AppId,
+                MchId = _optionsAccessor.Value.MchId,
+                Amount = new Amount { Total = viewModel.Total, Currency = "CNY" },
+                Description = viewModel.Description,
+                NotifyUrl = viewModel.NotifyUrl,
+                OutTradeNo = viewModel.OutTradeNo,
+                Payer = new PayerInfo { OpenId = viewModel.OpenId }
+            };
+
+            var request = new WeChatPayTransactionsJsApiRequest();
+            request.SetBodyModel(model);
+
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
+
+            if (response.StatusCode == 200)
+            {
+                var req = new WeChatPayMiniProgramSdkRequest
+                {
+                    Package = "prepay_id=" + response.PrepayId
+                };
+
+                var parameter = await _client.ExecuteAsync(req, _optionsAccessor.Value);
+
+                // 将参数(parameter)给 小程序端
+                // https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml
+                ViewData["parameter"] = JsonSerializer.Serialize(parameter);
+                ViewData["response"] = response.Body;
+                return View();
+            }
+
+            ViewData["response"] = response.Body;
+            return View();
+        }
+
         /// <summary>
         /// 微信支付订单号查询
         /// </summary>

+ 23 - 0
samples/WebApplicationSample/Models/WeChatPayV3ViewModel.cs

@@ -82,6 +82,29 @@ namespace WebApplicationSample.Models
         public string NotifyUrl { get; set; }
     }
 
+    public class WeChatPayMiniProgramPayV3ViewModel
+    {
+        [Required]
+        [Display(Name = "out_trade_no")]
+        public string OutTradeNo { get; set; }
+
+        [Required]
+        [Display(Name = "description")]
+        public string Description { get; set; }
+
+        [Required]
+        [Display(Name = "total")]
+        public int Total { get; set; }
+
+        [Required]
+        [Display(Name = "notify_url")]
+        public string NotifyUrl { get; set; }
+
+        [Required]
+        [Display(Name = "openid")]
+        public string OpenId { get; set; }
+    }
+    
     public class WeChatPayQueryByTransactionIdViewModel
     {
         [Required]

+ 46 - 40
samples/WebApplicationSample/Views/WeChatPayV3/Index.cshtml

@@ -20,150 +20,156 @@
     <tbody>
         <tr>
             <th scope="row">1</th>
+            <td>公众号支付-JSAPI下单</td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi</a></td>
+            <td><a asp-controller="WeChatPayV3" asp-action="PubPay">立即测试</a></td>
+        </tr>
+		<tr>
+            <th scope="row">2</th>
             <td>APP支付-App下单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/app</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/app</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="AppPay">立即测试</a></td>
         </tr>
-        <tr>
-            <th scope="row">2</th>
-            <td>公众号支付-JSAPI下单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_2.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi</a></td>
-            <td><a asp-controller="WeChatPayV3" asp-action="PubPay">立即测试</a></td>
+		<tr>
+            <th scope="row">3</th>
+            <td>H5支付-H5下单</td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/h5</a></td>
+            <td><a asp-controller="WeChatPayV3" asp-action="H5Pay">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">3</th>
+            <th scope="row">4</th>
             <td>扫码支付-Native下单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_3.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/native</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/native</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="QrCodePay">立即测试</a></td>
         </tr>
-        <tr>
-            <th scope="row">4</th>
-            <td>H5支付-H5下单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_4.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/h5</a></td>
-            <td><a asp-controller="WeChatPayV3" asp-action="H5Pay">立即测试</a></td>
+		<tr>
+            <th scope="row">5</th>
+            <td>小程序支付-JSAPI下单</td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi</a></td>
+            <td><a asp-controller="WeChatPayV3" asp-action="MiniProgramPay">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">5</th>
+            <th scope="row">6</th>
             <td>查询订单-微信支付订单号查询</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_5.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="QueryByTransactionId">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">6</th>
+            <th scope="row">7</th>
             <td>查询订单-商户订单号查询</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_5.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="QueryByOutTradeNo">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">7</th>
+            <th scope="row">8</th>
             <td>关闭订单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_6.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="OutTradeNoClose">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">8</th>
+            <th scope="row">9</th>
             <td>申请交易账单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/bill/chapter3_1.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/bill/tradebill</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_6.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/bill/tradebill</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="TradeBill">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">9</th>
+            <th scope="row">10</th>
             <td>申请资金账单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/bill/chapter3_2.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/bill/fundflowbill</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_7.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/bill/fundflowbill</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="FundflowBill">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">10</th>
+            <th scope="row">11</th>
             <td>下载账单</td>
-            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/bill/chapter3_3.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx</a></td>
+            <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="BillDownload">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">11</th>
+            <th scope="row">12</th>
             <td>退款申请</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/refund/domestic/refunds</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="Refund">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">12</th>
+            <th scope="row">13</th>
             <td>查询单笔退款</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_10.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/{out_refund_no}</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="RefundQuery">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">13</th>
+            <th scope="row">14</th>
             <td>支付分-创建支付分订单</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_14.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrder">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">14</th>
+            <th scope="row">15</th>
             <td>支付分-查询支付分订单</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_15.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderQuery">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">15</th>
+            <th scope="row">16</th>
             <td>支付分-取消支付分订单</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_16.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/cancel</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderCancel">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">16</th>
+            <th scope="row">17</th>
             <td>支付分-修改订单金额</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_17.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/modify</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderModify">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">17</th>
+            <th scope="row">18</th>
             <td>支付分-完结支付分订单</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_18.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/complete</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderComplete">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">18</th>
+            <th scope="row">19</th>
             <td>支付分-商户发起催收扣款</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_19.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/pay</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderPay">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">19</th>
+            <th scope="row">20</th>
             <td>支付分-同步服务订单信息</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_20.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/serviceorder/{out_order_no}/sync</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderSync">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">20</th>
+            <th scope="row">21</th>
             <td>支付分-创单结单合并</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_1.shtml" target="_blank">https://api.mch.weixin.qq.com/payscore/serviceorder/direct-complete</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="ServiceOrderDirectComplete">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">21</th>
+            <th scope="row">22</th>
             <td>支付分-商户预授权</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_2.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/permissions</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="Permissions">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">22</th>
+            <th scope="row">23</th>
             <td>支付分-查询用户授权记录(授权协议号)</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_3.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/permissions/authorization-code/{authorization_code}</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="PermissionsQueryForAuthCode">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">23</th>
+            <th scope="row">24</th>
             <td>支付分-解除用户授权关系(授权协议号)</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_4.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/permissions/authorization-code/{authorization_code}/terminate</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="PermissionsTerminateForAuthCode">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">24</th>
+            <th scope="row">25</th>
             <td>支付分-查询用户授权记录(openid)</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_5.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/permissions/openid/{openid}</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="PermissionsQueryForOpenId">立即测试</a></td>
         </tr>
         <tr>
-            <th scope="row">25</th>
+            <th scope="row">26</th>
             <td>支付分-解除用户授权关系(openid)</td>
             <td><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter6_1_6.shtml" target="_blank">https://api.mch.weixin.qq.com/v3/payscore/permissions/openid/{openid}/terminate</a></td>
             <td><a asp-controller="WeChatPayV3" asp-action="PermissionsTerminateForOpenId">立即测试</a></td>

+ 53 - 0
samples/WebApplicationSample/Views/WeChatPayV3/MiniProgramPay.cshtml

@@ -0,0 +1,53 @@
+@model WeChatPayMiniProgramPayV3ViewModel
+@{
+    ViewData["Title"] = "小程序支付-JSAPI下单";
+}
+<nav aria-label="breadcrumb">
+    <ol class="breadcrumb">
+        <li class="breadcrumb-item"><a asp-controller="WeChatPayV3" asp-action="Index">微信支付V3</a></li>
+        <li class="breadcrumb-item active" aria-current="page">@ViewData["Title"]</li>
+    </ol>
+</nav>
+<br />
+<div class="card">
+    <div class="card-body">
+        <form asp-controller="WeChatPayV3" asp-action="MiniProgramPay">
+            <div asp-validation-summary="All" class="text-danger"></div>
+            <div class="form-group">
+                <label asp-for="OutTradeNo"></label>
+                <input type="text" class="form-control" asp-for="OutTradeNo" value="@DateTime.Now.ToString("yyyyMMddHHmmssfff")">
+            </div>
+            <div class="form-group">
+                <label asp-for="Description"></label>
+                <input type="text" class="form-control" asp-for="Description" value="微信小程序支付测试">
+            </div>
+            <div class="form-group">
+                <label asp-for="Total"></label>
+                <input type="text" class="form-control" asp-for="Total" value="1">
+            </div>
+            <div class="form-group">
+                <label asp-for="NotifyUrl"></label>
+                <input type="text" class="form-control" asp-for="NotifyUrl" value="http://domain.com/wechatpay/v3/notify/transactions">
+            </div>
+            <div class="form-group">
+                <label asp-for="OpenId"></label>
+                <input type="text" class="form-control" asp-for="OpenId">
+            </div>
+            <button type="submit" class="btn btn-primary">提交请求</button>
+        </form>
+        <hr />
+        <form class="form-horizontal">
+            <div class="form-group">
+                <label>Response:</label>
+                <textarea class="form-control" rows="10">@ViewData["response"]</textarea>
+            </div>
+            <div class="form-group">
+                <label>Parameter:</label>
+                <textarea class="form-control" rows="3">@ViewData["parameter"]</textarea>
+            </div>
+        </form>
+    </div>
+</div>
+@section Scripts {
+    @{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
+}