示例#1
0
        public async Task <ZarinpalResult <ZarinpalPaymentResponseModel> > PayAsync(ZarinpalPaymentRequestModel model)
        {
            var errors = new List <ZarinpalError>();

            model.ValidateModel(errors);
            if (errors.Any())
            {
                return(ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(errors.ToArray()));
            }

            model.MerchantId = _configuration.Token;
            var t = await PostRequestBase <ZarinpalPaymentResponseModel, ZarinpalPaymentRequestModel>(
                model,
                _requestUrl);

            if (!t.Succeeded)
            {
                return(ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(errors.ToArray()));
            }

            t.Result.PaymentUrl = _configuration.UseZarinLink ?
                                  ZarinpalUrlConfig.GetWebGateRequestUrl(t.Result.Authority, false) :
                                  ZarinpalUrlConfig.GetPaymenGatewayUrl(t.Result.Authority, false);
            t.Result.Validate(errors);

            return(errors.Any()
                ? ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(errors.ToArray())
                : ZarinpalResult <ZarinpalPaymentResponseModel> .Invoke(t.Result));
        }
示例#2
0
        public async Task <ZarinpalResult <ZarinpalPaymentResponseModel> > InvokePaymentAsync(ZarinpalPaymentRequestModel model)
        {
            var errors = new List <ZarinpalError>();

            model.ValidateModel(errors);
            if (errors.Any())
            {
                return(ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(errors.ToArray()));
            }

            model.MerchantId = _configuration.Token;

            //var x = await PostRequestBase<ZarinpalPaymentResponseModel, ZarinpalPaymentRequestModel>(model, _requestUrl);

            var request = await StartPaymentAsync(model, ZarinpalRequestMethod.Post, _requestUrl);

            if (!request.Succeeded)
            {
                return(ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(request.Errors.ToArray()));
            }

            var response = JsonConvert.DeserializeObject <ZarinpalPaymentResponseModel>(request.Result);

            if (response == null)
            {
                return(ZarinpalResult <ZarinpalPaymentResponseModel> .Failed());
            }
            response.PaymentUrl = ZarinpalUrlConfig.GetPaymenGatewayUrl(response.Authority, _configuration.UseSandbox);
            var t = ZarinpalResult <ZarinpalPaymentResponseModel> .Invoke(response);

            t.Result.Validate(errors);

            return(errors.Any() ? ZarinpalResult <ZarinpalPaymentResponseModel> .Failed(errors.ToArray()) : t);
        }