Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ZarinPal_Pay_Lib
- {
- public class ZarinPay
- {
- private string _MerchantID, _Description, _CallbackURL, _Email, _Mobile;
- private int _Amount;
- public delegate void PArgs(object sender, PayArgs e);
- public event PArgs OnPaymentAction;
- /// <summary>
- /// Initilize a payment
- /// </summary>
- /// <param name="MerchantID">Reciever Unique ID</param>
- /// <param name="Amount">Amount to send</param>
- /// <param name="Description"></param>
- /// <param name="CallbackURL">Where to redirect after finish</param>
- /// <param name="Email">Sender mail address ( optionl )</param>
- /// <param name="Mobile">Sender mobile number ( optional )</param>
- /// <returns>Returns authority id</returns>
- public ZarinPay(string MerchantID, int Amount, string Description, string CallbackURL, string Email = "", string Mobile = "")
- {
- _MerchantID = MerchantID;
- _Amount = Amount;
- _Description = Description;
- _CallbackURL = CallbackURL;
- _Email = Email;
- _Mobile = Mobile;
- }
- /// <summary>
- /// Starts payment
- /// </summary>
- /// <returns>Returns payment autohority id</returns>
- public string StartPay()
- {
- ZarinPal.PaymentGatewayImplementationServicePortTypeClient request = new ZarinPal.PaymentGatewayImplementationServicePortTypeClient();
- string autohority = string.Empty;
- int value = request.PaymentRequest(_MerchantID, _Amount, _Description, _Email, _Mobile, _CallbackURL, out autohority);
- if (value > 0)
- {
- new Thread(() =>
- {
- CheckPaymentStatus(autohority);
- }).Start();
- return autohority;
- }
- else
- {
- return null;
- }
- }
- private void CheckPaymentStatus(string autohority)
- {
- ZarinPal.PaymentGatewayImplementationServicePortTypeClient request = new ZarinPal.PaymentGatewayImplementationServicePortTypeClient();
- long refID = -1;
- while (true)
- {
- int verf = request.PaymentVerification(_MerchantID, autohority, _Amount, out refID);
- if (verf > 0)
- {
- if(OnPaymentAction != null)
- {
- OnPaymentAction(this, new PayArgs(verf, autohority, refID));
- }
- break;
- }
- else
- {
- if (verf != -21)
- {
- if (OnPaymentAction != null)
- {
- OnPaymentAction(this, new PayArgs(verf, autohority, refID));
- }
- break;
- }
- }
- }
- }
- public string MerchantID
- {
- get { return _MerchantID; }
- }
- public string Description
- {
- get { return _Description; }
- }
- public string CallbackURL
- {
- get { return _CallbackURL; }
- }
- public string Email
- {
- get { return _Email; }
- }
- public string Mobile
- {
- get { return _Mobile; }
- }
- public int Amount
- {
- get { return _Amount; }
- }
- public class PayArgs
- {
- private int _Status;
- private string _Autohority;
- private long _RefID;
- public PayArgs(int Status, string Autohority, long RefID)
- {
- _Status = Status;
- _Autohority = Autohority;
- _RefID = RefID;
- }
- public int Status
- {
- get { return _Status; }
- }
- public string Autohority
- {
- get { return _Autohority; }
- }
- public long RefID
- {
- get { return _RefID; }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement