public ActionResult AppletPay(string out_trade_no, int total_amount, string body, string open_id)
        {
            var request = new AppletPayRequest();

            request.AddGatewayData(new AppletPayModel()
            {
                Body        = body,
                OutTradeNo  = out_trade_no,
                TotalAmount = total_amount,
                OpenId      = open_id
            });

            var response = _gateway.Execute(request);

            return(Json(response));
        }
Beispiel #2
0
        public async Task <RequestPaymentResponseDTO> RequestPayment(RequestPaymentRequestDTO model)
        {
            var orderDTO = await _orderServiceProxy.GetById(model.OrderId);

            if (orderDTO == null)
            {
                return(null);
            }

            var request = new AppletPayRequest();

            var body   = "";
            var orgStr = (orderDTO.OrderItems.FirstOrDefault()?.Title ?? "-");

            if (orgStr.Length > 20)
            {
                body = orgStr.Substring(0, 20);
            }
            else
            {
                body = orgStr;
            }

            request.AddGatewayData(new AppletPayModel()
            {
                Body        = body,
                OutTradeNo  = orderDTO.OrderNo,
                TotalAmount = (int)(orderDTO.PayAmount * 100),
                OpenId      = model.OpenId
            });

            IGateway gateway = null;

            var organizationId = "";

            organizationId = orderDTO.OrganizationId ?? "";
            WeChatAppConfiguration appConfig = null;

            if (!string.IsNullOrEmpty(organizationId))
            {
                appConfig = _wechatAppConfigurationRepository.GetFiltered(o => o.OrganizationId == organizationId).FirstOrDefault();
            }

            if (appConfig != null)
            {
                var wechatpayMerchant = new PaySharp.Wechatpay.Merchant
                {
                    AppId           = appConfig.AppId,
                    MchId           = appConfig.MchId,
                    Key             = appConfig.Key,
                    AppSecret       = appConfig.AppSecret,
                    SslCertPath     = appConfig.SslCertPath,
                    SslCertPassword = appConfig.SslCertPassword,
                    NotifyUrl       = appConfig.NotifyUrl
                };

                gateway = new WechatpayGateway(wechatpayMerchant);
            }
            else
            {
                gateway = _gateways.Get <WechatpayGateway>();
            }

            var response = gateway.Execute(request);

            var ns   = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 20);
            var ts   = GetTimeStamp();
            var pkg  = $"prepay_id={response?.PrepayId}";
            var sign = GetSign(((WechatpayGateway)gateway).Merchant.AppId, ns, response?.PrepayId, ts.ToString(), ((WechatpayGateway)gateway).Merchant.Key);

            var result = new RequestPaymentResponseDTO
            {
                NonceStr  = ns,
                Package   = pkg,
                PaySign   = sign,
                TimeStamp = ts.ToString()
            };

            return(result);
        }