Example #1
0
        public static ZarinpalModelV4.Verify.Response Verify(ZarinpalModelV4.Verify.Request request)
        {
            HttpClient.DefaultRequestHeaders.Accept.Clear();
            HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var serializeObject = JsonConvert.SerializeObject(request, GetSerializerSetting());

            var stringContent = new StringContent(serializeObject, Encoding.UTF8, "application/json");

            var httpResponseMessage =
                HttpClient.PostAsync($"{BaseApiUrl}/pg/v4/payment/verify.json", stringContent).Result;

            if (httpResponseMessage.StatusCode == HttpStatusCode.BadGateway)
            {
                throw new ZarinpalException(httpResponseMessage.StatusCode, "Cannot contact Zarinpal Server");
            }
            if ((int)httpResponseMessage.StatusCode >= 400 && (int)httpResponseMessage.StatusCode < 500)
            {
                throw new ZarinpalException(httpResponseMessage.StatusCode,
                                            "Cannot process the request due to bad request error.");
            }
            if ((int)httpResponseMessage.StatusCode >= 500)
            {
                throw new ZarinpalException(httpResponseMessage.StatusCode, "Zarinpal responded with an unknown error");
            }

            var result = httpResponseMessage.Content.ReadAsStringAsync().Result;

            return(JsonConvert.DeserializeObject <ZarinpalModelV4.Verify.Response>(result, GetSerializerSetting()));
        }
Example #2
0
        /// <summary>
        /// The URL of which the Zarinpal will call after a successful or failure payment operation
        /// </summary>
        /// <param name="id">ProductID: It is the ID you previously send to the Zarinpal, Like Factor ID, Order ID, an ID to track what user is paying for.<br/>
        /// Here we used Product ID, which in real scenario mostly will be wrong, unless you are showing something to user that is disposable.</param>
        /// <param name="authority">
        /// A unique 32 characters length identifier of type `UUID` (Universal Unique Identifier) that Zarinpal
        /// Sent to client for each payment request. The Identifier always start with 'A' character.
        /// Sample: A 36 character lenght string, starting with A, like: A00000000000000000000000000217885159
        /// </param>
        /// <param name="status">
        /// Either `OK` or `NOK`, of which the `OK` represent the successful payment and `NOK` represent a failure. <br />
        /// Whenever the status is `OK`, and only when it is `OK`, we should also verify the incoming request with Zarinpal, Otherwise it may be an attacker issuing false request
        /// </param>
        /// <returns></returns>
        public IActionResult CallbackV4(int id, string authority, string status)
        {
            var viewModel = new MessageModel();

            if (status == "NOK")
            {
                viewModel.IsError = true;
                viewModel.Text    = "Transaction unsuccessful.";
            }
            else if (status == "OK")
            {
                var product = TestDatabase.GetById(id);
                var request = new ZarinpalModelV4.Verify.Request
                {
                    MerchantId = TestMerchantIdV4,
                    Authority  = authority,
                    Amount     = product.Amount * 10
                };

                var response = RestApiV4.Verify(request);

                if (response.Data.Code == 100) // Successful
                {
                    viewModel.IsError = false;
                    viewModel.Text    = $"Transaction successful. RefId: {response.Data.RefId}";
                }
                else if (response.Data.Code == 101) // Repeated successful
                {
                    viewModel.IsError = false;
                    viewModel.Text    = $"Transaction repeated with success response. RefId: {response.Data.RefId}";
                }
                else // Error
                {
                    viewModel.IsError = true;
                    viewModel.Text    = $"Transaction unsuccessful. Status: {response.Data.Code}";
                }
            }

            return(RedirectToAction("ShowResult", viewModel));
        }