-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHomeController.cs
More file actions
88 lines (72 loc) · 4.1 KB
/
HomeController.cs
File metadata and controls
88 lines (72 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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;
namespace NewZarinPal.Controllers
{
public class HomeController : Controller
{
private static readonly HttpClient client = new HttpClient();
public IActionResult Index()
{
return View();
//return RedirectToAction("RequestPayment", "Home");
}
public async Task<IActionResult> RequestPayment()
{
//SandBox Mode
//var _url = "https://sandbox.zarinpal.com/pg/rest/WebGate/PaymentRequest.json";
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.
{ "Email", "CUSTOMER-Email-Address" }, //Email Address will be shown in the transactions list of the wallet as a separate field.
{ "Description", "This is a test payment" }
};
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();
//SandBox Mode
//return Redirect("https://sandbox.zarinpal.com/pg/StartPay/"+_zarinPalResponseModel.Authority/*+"/Sad"*/);
// [/ُ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"*/);
}
public async Task<IActionResult> VerifyPayment(string Authority)
{
//SandBox Mode
//var _url = "https://sandbox.zarinpal.com/pg/rest/WebGate/PaymentVerification.json";
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();
}
}
}