Example #1
0
        public ZarinpalProvider(IOptionsSnapshot <ZarinpalConfiguration> options, HttpClient httpClient)
        {
            _httpClient = httpClient;

            if (_httpClient == null)
            {
                throw new ArgumentNullException(nameof(_httpClient));
            }

            var zarinpalConfiguration = options;

            if (zarinpalConfiguration == null)
            {
                throw new ArgumentNullException(nameof(zarinpalConfiguration));
            }

            _configuration = zarinpalConfiguration.Value;
            if (_configuration == null)
            {
                throw new ArgumentNullException(nameof(_configuration));
            }

            _requestUrl = ZarinpalUrlConfig.GetPaymentRequestUrl(_configuration.UseSandbox);
            _verifyUrl  = ZarinpalUrlConfig.GetVerificationUrl(_configuration.UseSandbox);
        }
Example #2
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));
        }
Example #3
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);
        }