Skip to content

ZarinPal-Lab/SampleCode-Csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SampleCode-Csharp

Sample Code for C# .net And Asp Core

There are two samples for C# (REST and SOAP)

The Rest sample is preferred by Zarinpal, by the way you are free to choose between REST and SOAP

How It works simply 🚴

  1. Install Package
  • Newtonsoft.Json

You can install Newtonsoft.Json with this command for VSCode :

Dotnet Add Package Newtonsoft.Json

for Visual studio 2019 through the nuget or the command below :

Install-Package Newtonsoft.Json
  1. Namespaces
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NewZarinPal.Models;
  1. PaymentRequest
 public async Task<IActionResult> RequestPayment()
        {
            var _url = "https://www.zarinpal.com/pg/rest/WebGate/PaymentRequest.json";
            var _values = new Dictionary<string, string>
                {
                    { "MerchantID", "YOUR-ZARINPAL-MERCHANT-CODE" }, //Change This To work, some thing like this : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
                    { "Amount", "500" }, //Toman
                    { "CallbackURL", "http://localhost:5000/Home/VerifyPayment" },
                    { "Mobile", "CUSTOMER-MOBLIE-NUMBER" }, //Mobile number will be shown in the transactions list of the wallet as a separate field.
                    { "Description", "خرید تست" }
                };

            var _paymentRequestJsonValue = JsonConvert.SerializeObject(_values);
            var content = new StringContent(_paymentRequestJsonValue, Encoding.UTF8, "application/json");

            var _response = await client.PostAsync(_url, content);
            var _responseString = await _response.Content.ReadAsStringAsync();

            ViewBag.StatusCode = _response.StatusCode;
            ViewBag._responseString = _responseString;

            ZarinPalRequestResponseModel _zarinPalResponseModel =
             JsonConvert.DeserializeObject<ZarinPalRequestResponseModel>(_responseString);

            if (_response.StatusCode != System.Net.HttpStatusCode.OK) // Post Error
                return View();

            if (_zarinPalResponseModel.Status != 100) //Zarinpal Did not Accepted the payment
                return View();

            // [/ُSad] will redirect to the sadad gateway if you already have zarin gate enabled, let's read here
            // https://www.zarinpal.com/blog/زرین-گیت،-درگاهی-اختصاصی-به-نام-وبسایت/
            return Redirect("https://www.zarinpal.com/pg/StartPay/"+_zarinPalResponseModel.Authority/*+"/Sad"*/); 
        }
  1. VerifyPayment
public async Task<IActionResult> VerifyPayment(string Authority)
        {
            var _url = "https://www.zarinpal.com/pg/rest/WebGate/PaymentVerification.json";
            var _values = new Dictionary<string, string>
                {
                    { "MerchantID", "YOUR-ZARINPAL-MERCHANT-CODE" }, //Change This To work, some thing like this : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
                    { "Authority", Authority },
                    { "Amount", "500" } //Toman
                };

            var _paymenResponsetJsonValue = JsonConvert.SerializeObject(_values);
            var content = new StringContent(_paymenResponsetJsonValue, Encoding.UTF8, "application/json");

            var _response = await client.PostAsync(_url, content);
            var _responseString = await _response.Content.ReadAsStringAsync();

            ViewBag.StatusCode = _response.StatusCode;
            ViewBag.responseString = _responseString;

            ZarinPalVerifyResponseModel _zarinPalResponseModel =
             JsonConvert.DeserializeObject<ZarinPalVerifyResponseModel>(_responseString);

            return View();
        }
  1. Models
public class ZarinPalRequestResponseModel
     {
         public int Status { get; set; }
         public string Authority { get; set; }
     }
public class ZarinPalVerifyResponseModel
     {
         public int Status { get; set; }
         public string RefID { get; set; }
     }
  1. View for both PaymentRequest And VerifyPayment
<div class="text-center">
    <h6>@ViewBag.StatusCode</h6>
    <h6>@ViewBag.responseString</h6>
</div>
  1. How to use SandBox mode

replace

https://www.zarinpal.com

with

https://sandbox.zarinpal.com

every where

Build Instructions for REST sample project 🔨

Prerequistics 📃

To run sample of REST you must have installed these prerequestics

These Edittors are tested :

  • Visual Studio 2019
  • VSCode

If you desier to run The Rest sample in VSCode remember ro run this command in the terminal after openning the project

Dotnet Restore

This packages are Installed :

  • Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  • Newtonsoft.Json

But only Newtonsoft.Json is neccessary

The Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation make the below line works is the Startup.cs file and is not neccessary in your project:

  • services.AddControllersWithViews().AddRazorRuntimeCompilation();

For Visual Studio from the solution explorer simply right click on the project and click build

Do not Forget to set the MerchantID in the HomeController before start.

Contributing and Questions 👬

If you had any question or suggestion feel free to use pull requests or issues, we are glad to be in touch with you.