Exemplo n.º 1
0
        public IActionResult PaymentV4(int id)
        {
            var product = TestDatabase.GetById(id);

            var request = new ZarinpalModelV4.Payment.Request
            {
                MerchantId  = TestMerchantIdV4,
                Amount      = product.Amount * 10, // V4 in Rials
                CallbackUrl = $"{Request.Scheme}://{Request.Host}/Home/CallbackV4/{product.Id}",
                Description = $"Pay for {product.Name} product.",
                Metadata    = new ZarinpalModelV4.Payment.RequestMetadata // Either none, or all
                {
                    Email  = "*****@*****.**",
                    Mobile = null
                }
            };

            var response = RestApiV4.PaymentRequest(request);

            if (response.Data.Code == 100)
            {
                var gatewayLink = RestApiV4.GenerateGatewayLink(response.Data.Authority);
                return(Redirect(gatewayLink));
            }

            TempData["Message"] = response.Data.Code;
            return(View("Index"));
        }
Exemplo n.º 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));
        }