You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
2.6 KiB

  1. // Ignore Spelling: Zarin
  2. using Newtonsoft.Json;
  3. using RestSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ZarinPal
  10. {
  11. public static class ZarinPalHelper
  12. {
  13. public static NewPurchaseResult CreateRequest(string callbackUrl,long amount,string merchantId,string desc)
  14. {
  15. var client = new RestClient("https://www.zarinpal.com/pg/rest/WebGate/PaymentRequest.json");
  16. var request = new RestRequest("", Method.Post)
  17. {
  18. RequestFormat = RestSharp.DataFormat.Json,
  19. };
  20. request.AddJsonBody(new ZarinPalPaymentRequestModel
  21. {
  22. Amount = amount,
  23. MerchantID = merchantId,
  24. Description = desc,
  25. CallbackURL = callbackUrl
  26. });
  27. var response = client.Execute<ZarinpalPaymentStartResult>(request);
  28. var paymentStartResult = JsonConvert.DeserializeObject<ZarinpalPaymentStartResult>(response.Content);
  29. if (paymentStartResult.Status == 100)
  30. {
  31. return new NewPurchaseResult
  32. {
  33. Authority = paymentStartResult.Authority,
  34. Success = true
  35. };
  36. }
  37. return new NewPurchaseResult
  38. {
  39. Success = false
  40. };
  41. }
  42. public static ConfirmResult Confirm(long amount,string merchantId,string auth)
  43. {
  44. var client = new RestClient("https://www.zarinpal.com/pg/rest/WebGate/PaymentVerification.json");
  45. var request = new RestRequest("", Method.Post)
  46. {
  47. RequestFormat = RestSharp.DataFormat.Json,
  48. };
  49. request.AddJsonBody(new ZarinPalConfirmModel
  50. {
  51. Amount = amount,
  52. MerchantID = merchantId,
  53. Authority = auth,
  54. });
  55. var response = client.Execute<ZarinPalPaymentResponse>(request);
  56. ZarinPalPaymentResponse paymentStartResult = JsonConvert.DeserializeObject<ZarinPalPaymentResponse>(response.Content);
  57. if (paymentStartResult.Status == 100)
  58. {
  59. return new ConfirmResult
  60. {
  61. Ref = paymentStartResult.RefID,
  62. Successful = true
  63. };
  64. }
  65. return new ConfirmResult
  66. {
  67. Successful = false
  68. };
  69. }
  70. }
  71. }