/// <summary> /// 创建代金券批次接口 /// <para>调用此接口创建微信支付代金券批次,创建完成后将获得代金券批次id。</para> /// <para>更多详细请参考 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_1.shtml </para> /// <para>提示:使用此功能必须在后台【产品中心】开通【预充值代金券】功能!</para> /// </summary> /// <param name="data">微信支付需要POST的Data数据</param> /// <param name="timeOut">超时时间,单位为ms </param> /// <returns></returns> public async Task <CreateStockReturnJson> CreateStockAsync(CreateStockRequsetData data, int timeOut = Config.TIME_OUT) { const string STOCK_TYPE = "NORMAL"; if (data.stock_type == STOCK_TYPE && data.coupon_use_rule.fixed_normal_coupon == null) { throw new TenpayApiRequestException($"当 {nameof(data.stock_type)} 为 {STOCK_TYPE} 时,{nameof(data.coupon_use_rule.fixed_normal_coupon)} 必填!"); } if (data.stock_use_rule.max_amount != data.stock_use_rule.max_coupons * data.coupon_use_rule.fixed_normal_coupon.coupon_amount) { throw new TenpayApiRequestException($"{nameof(data.stock_use_rule.max_amount)} 必须等于 {nameof(data.stock_use_rule.max_coupons)} 乘以 {nameof(data.coupon_use_rule.fixed_normal_coupon.coupon_amount)}!"); } if (data.coupon_use_rule.fixed_normal_coupon.coupon_amount > data.coupon_use_rule.fixed_normal_coupon.transaction_minimum) { throw new TenpayApiRequestException($"{nameof(data.coupon_use_rule.fixed_normal_coupon.coupon_amount)} 必须小于等于 {nameof(data.coupon_use_rule.fixed_normal_coupon.transaction_minimum)}!"); } var url = ReurnPayApiUrl(Senparc.Weixin.Config.TenPayV3Host + "/{0}v3/marketing/favor/coupon-stocks"); TenPayApiRequest tenPayApiRequest = new(_tenpayV3Setting); return(await tenPayApiRequest.RequestAsync <CreateStockReturnJson>(url, data, timeOut)); }
public void CreateStockAsyncTest() { var key = TenPayHelper.GetRegisterKey(Config.SenparcWeixinSetting); var TenPayV3Info = TenPayV3InfoCollection.Data[key]; // 代金券批次发放使用规则 发放10张优惠券 预算1000分 每个用户限领10张 开启防刷 var stock_use_rule = new CreateStockRequsetData.Stock_Use_Rule(2, 4, null, 2, false, true); // 代金券使用规则 指明可使用本批次代金券的商户号 var coupon_use_rule = new CreateStockRequsetData.Coupon_Use_Rule(null, null, null, null, null, null, null, new string[] { TenPayV3Info.MchId /*"1610625015"*/ }); // TODO:流水号?这样是否有效? var out_request_no = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"), TenPayV3Util.BuildRandomStr(6)); var requestData = new CreateStockRequsetData("单元测试代金券批次", "用于单元测试", TenPayV3Info.MchId, new TenpayDateTime(DateTime.Now.AddMinutes(1)), new TenpayDateTime(DateTime.Now.AddMinutes(30)), stock_use_rule, null, coupon_use_rule, true, "NORMAL", out_request_no); /* 提示: * 使用此功能必须在后台【产品中心】开通【预充值代金券】功能! */ var marketingApis = new MarketingApis(); try { // stock_type = "NORMAL" 的情况 createStockResult = marketingApis.CreateStockAsync(requestData).GetAwaiter().GetResult(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(TenpayApiRequestException)); Console.WriteLine(ex.Message); Assert.IsTrue(ex.Message.Contains("必填")); } try { //修改参数 requestData.coupon_use_rule.fixed_normal_coupon = new CreateStockRequsetData.Coupon_Use_Rule.Fixed_Normal_Coupon(6, 1); createStockResult = marketingApis.CreateStockAsync(requestData).GetAwaiter().GetResult(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(TenpayApiRequestException)); Console.WriteLine(ex.Message); Assert.IsTrue(ex.Message.Contains("必须等于")); } try { //修改参数 requestData.coupon_use_rule.fixed_normal_coupon.coupon_amount = 2;// coupon_amount = max_coupons * max_coupons_per_user createStockResult = marketingApis.CreateStockAsync(requestData).GetAwaiter().GetResult(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(TenpayApiRequestException)); Console.WriteLine(ex.Message); Assert.IsTrue(ex.Message.Contains("必须小于等于")); } //修改参数 requestData.coupon_use_rule.fixed_normal_coupon.transaction_minimum = 2;// coupon_amount = max_coupons * max_coupons_per_user createStockResult = marketingApis.CreateStockAsync(requestData).GetAwaiter().GetResult(); Assert.IsTrue(createStockResult.ResultCode.Additional.Contains("可用商户不符合规则,请检查")); //修改参数 requestData.no_cash = false;//只有当参数为 false 时,可以使用本商户,否则只能使用其他商户 createStockResult = marketingApis.CreateStockAsync(requestData).GetAwaiter().GetResult(); Console.WriteLine("微信支付 V3 创建代金券接口批次结果:" + createStockResult.ToJson(true)); Assert.IsNotNull(createStockResult); Assert.IsTrue(createStockResult.ResultCode.Success); Assert.IsTrue(createStockResult.VerifySignSuccess == true);//通过验证 }