예제 #1
0
        public async Task <IActionResult> Index(QrCodePayViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(nameof(model), "invalid form data");
                return(View(model));
            }
            var req = new GetQrCodeRequest
            {
                TotalAmount      = model.totalAmount,
                BillDate         = model.billDate,
                BillDesc         = model.billDesc,
                BillNo           = model.billNo,
                RequestTimestamp = model.requestTimestamp,
                NotifyUrl        = model.notifyUrl,
                ReturnUrl        = model.returnUrl
            };
            var res = await _client.ExecuteAsync(req);

            if (string.IsNullOrWhiteSpace(res.ErrCode) && string.IsNullOrWhiteSpace(res.ErrMsg))
            {
                throw new Exception("请求失败:" + res.Body);
            }
            return(RedirectToAction(nameof(QrCode), new { code = res.BillQrCode }));
        }
예제 #2
0
        public override Task <GetQrCodeResponse> GetQrCode(GetQrCodeRequest request, ServerCallContext context)
        {
            var resp        = new GetQrCodeResponse();
            var qrCodeUrl   = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
            var accessToken = _redis.StringGet(CacheKey.UserAccessTokenPrefix + request.AppId);

            if (accessToken.HasValue)
            {
                var result = WxWebApi.CreateQrCode(accessToken, request.Data);
                if (result.ErrCode == 0)
                {
                    resp.Expired   = Util.GetTimestamp() + result.ExpireSeconds;
                    resp.ImageUrl  = result.Url;
                    resp.QrcodeUrl = qrCodeUrl + result.Ticket;
                }
                else
                {
                    resp.Error = new Error
                    {
                        ErrCode = result.ErrCode,
                        ErrMsg  = result.ErrMsg
                    };
                }
            }
            else
            {
                resp.Error = new Error
                {
                    ErrCode = 99999,
                    ErrMsg  = "AccessToken Missing"
                };
            }
            return(Task.FromResult(resp));
        }