-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPaymentController.cs
103 lines (80 loc) · 3.49 KB
/
PaymentController.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.AspNetCore.Mvc;
using Parbad.AspNetCore;
using Parbad.Sample.Shared;
using System.Threading.Tasks;
namespace Parbad.Sample.AspNetCore.Controllers;
public class PaymentController : Controller
{
private readonly IOnlinePayment _onlinePayment;
public PaymentController(IOnlinePayment onlinePayment)
{
_onlinePayment = onlinePayment;
}
[HttpGet]
public IActionResult Pay()
{
return View(new PayViewModel());
}
[HttpPost]
public async Task<IActionResult> Pay(PayViewModel viewModel)
{
var callbackUrl = Url.Action("Verify", "Payment", null, Request.Scheme);
var result = await _onlinePayment.RequestAsync(invoice =>
{
invoice.SetCallbackUrl(callbackUrl)
.SetAmount(viewModel.Amount)
.SetGateway(viewModel.SelectedGateway.ToString());
if (viewModel.GenerateTrackingNumberAutomatically)
{
invoice.UseAutoIncrementTrackingNumber();
}
else
{
invoice.SetTrackingNumber(viewModel.TrackingNumber);
}
});
// Note: Save the result.TrackingNumber in your database.
if (result.IsSucceed)
{
return result.GatewayTransporter.TransportToGateway();
}
return View("PayRequestError", result);
}
[HttpGet, HttpPost]
public async Task<IActionResult> Verify()
{
var invoice = await _onlinePayment.FetchAsync();
// Check if the invoice is new, or it's already processed before.
if (invoice.Status != PaymentFetchResultStatus.ReadyForVerifying)
{
// You can also see if the invoice is already verified before.
var isAlreadyVerified = invoice.IsAlreadyVerified;
return Content("The payment was not successful.");
}
// This is an example of cancelling an invoice when you think that the payment process must be stopped.
if (!Is_There_Still_Product_In_Shop(invoice.TrackingNumber))
{
var cancelResult = await _onlinePayment.CancelAsync(invoice, cancellationReason: "Sorry, We have no more products to sell.");
return View("CancelResult", cancelResult);
}
var verifyResult = await _onlinePayment.VerifyAsync(invoice);
// Note: Save the verifyResult.TransactionCode in your database.
return View(verifyResult);
}
[HttpGet]
public IActionResult Refund()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Refund(RefundViewModel viewModel)
{
var result = await _onlinePayment.RefundCompletelyAsync(viewModel.TrackingNumber);
return View("RefundResult", result);
}
private static bool Is_There_Still_Product_In_Shop(long trackingNumber)
{
// Yes, we still have products :)
return true;
}
}