Advertisement
jlalt

ouch

Aug 11th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace ZarinPal_Pay_Lib
  9. {
  10.     public class ZarinPay
  11.     {
  12.         private string _MerchantID, _Description, _CallbackURL, _Email, _Mobile;
  13.         private int _Amount;
  14.         public delegate void PArgs(object sender, PayArgs e);
  15.         public event PArgs OnPaymentAction;
  16.         /// <summary>
  17.         /// Initilize a payment
  18.         /// </summary>
  19.         /// <param name="MerchantID">Reciever Unique ID</param>
  20.         /// <param name="Amount">Amount to send</param>
  21.         /// <param name="Description"></param>
  22.         /// <param name="CallbackURL">Where to redirect after finish</param>
  23.         /// <param name="Email">Sender mail address ( optionl )</param>
  24.         /// <param name="Mobile">Sender mobile number ( optional )</param>
  25.         /// <returns>Returns authority id</returns>
  26.         public ZarinPay(string MerchantID, int Amount, string Description, string CallbackURL, string Email = "", string Mobile = "")
  27.         {
  28.             _MerchantID = MerchantID;
  29.             _Amount = Amount;
  30.             _Description = Description;
  31.             _CallbackURL = CallbackURL;
  32.             _Email = Email;
  33.             _Mobile = Mobile;
  34.         }
  35.         /// <summary>
  36.         /// Starts payment
  37.         /// </summary>
  38.         /// <returns>Returns payment autohority id</returns>
  39.         public string StartPay()
  40.         {
  41.             ZarinPal.PaymentGatewayImplementationServicePortTypeClient request = new ZarinPal.PaymentGatewayImplementationServicePortTypeClient();
  42.             string autohority = string.Empty;
  43.             int value = request.PaymentRequest(_MerchantID, _Amount, _Description, _Email, _Mobile, _CallbackURL, out autohority);
  44.             if (value > 0)
  45.             {
  46.                 new Thread(() =>
  47.                 {
  48.                     CheckPaymentStatus(autohority);
  49.                 }).Start();
  50.                 return autohority;
  51.             }
  52.             else
  53.             {
  54.                 return null;
  55.             }
  56.         }
  57.         private void CheckPaymentStatus(string autohority)
  58.         {
  59.             ZarinPal.PaymentGatewayImplementationServicePortTypeClient request = new ZarinPal.PaymentGatewayImplementationServicePortTypeClient();
  60.             long refID = -1;
  61.             while (true)
  62.             {
  63.                 int verf = request.PaymentVerification(_MerchantID, autohority, _Amount, out refID);
  64.                 if (verf > 0)
  65.                 {
  66.                     if(OnPaymentAction != null)
  67.                     {
  68.                         OnPaymentAction(this, new PayArgs(verf, autohority, refID));
  69.                     }
  70.                     break;
  71.                 }
  72.                 else
  73.                 {
  74.                     if (verf != -21)
  75.                     {
  76.                         if (OnPaymentAction != null)
  77.                         {
  78.                             OnPaymentAction(this, new PayArgs(verf, autohority, refID));
  79.                         }
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         public string MerchantID
  86.         {
  87.             get { return _MerchantID; }
  88.         }
  89.         public string Description
  90.         {
  91.             get { return _Description; }
  92.         }
  93.         public string CallbackURL
  94.         {
  95.             get { return _CallbackURL; }
  96.         }
  97.         public string Email
  98.         {
  99.             get { return _Email; }
  100.         }
  101.         public string Mobile
  102.         {
  103.             get { return _Mobile; }
  104.         }
  105.         public int Amount
  106.         {
  107.             get { return _Amount; }
  108.         }
  109.         public class PayArgs
  110.         {
  111.             private int _Status;
  112.             private string _Autohority;
  113.             private long _RefID;
  114.             public PayArgs(int Status, string Autohority, long RefID)
  115.             {
  116.                 _Status = Status;
  117.                 _Autohority = Autohority;
  118.                 _RefID = RefID;
  119.             }
  120.             public int Status
  121.             {
  122.                 get { return _Status; }
  123.             }
  124.             public string Autohority
  125.             {
  126.                 get { return _Autohority; }
  127.             }
  128.             public long RefID
  129.             {
  130.                 get { return _RefID; }
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement