-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDefault.aspx.cs
67 lines (59 loc) · 3.01 KB
/
Default.aspx.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
using System;
using System.Globalization;
using System.IO;
using System.Net;
using Yandex.Checkout.V3;
namespace AspNetSample
{
public partial class ycheckout : System.Web.UI.Page
{
// It's best to have only one instance of Yandex.Checkout.V3.Client for the lifetime of an application
// (same as with HttpClient). Hence using static.
static readonly Client _client = new Client("501156", "test_As0OONRn1SsvFr0IVlxULxst5DBIoWi_tyVaezSRTEI");
protected void submit_YandexPay_Click(object sender, EventArgs e)
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
File.Delete(Server.MapPath("log.txt"));
// 1. Создайте платеж и получите ссылку для оплаты
decimal amount = decimal.Parse(sum.Text, CultureInfo.InvariantCulture.NumberFormat);
var newPayment = new NewPayment
{
Amount = new Amount { Value = amount, Currency = "RUB" },
Confirmation = new Confirmation { Type = ConfirmationType.Redirect, ReturnUrl = Request.Url.AbsoluteUri }
};
Payment payment = _client.CreatePayment(newPayment);
// 2. Перенаправьте пользователя на страницу оплаты
string url = payment.Confirmation.ConfirmationUrl;
Response.Redirect(url);
}
// 3. Дождитесь уведомления о платеже
protected void Page_Load(object sender, EventArgs e)
{
// Чтобы получить это уведомление, нужно указать адрес этой страницы
// в настройках магазина (https://yookassa.ru/my/merchant/integration/http-notifications).
try
{
Log($"Page_Load: Request.HttpMethod={Request.HttpMethod}, Request.ContentType={Request.ContentType}, Request.InputStream has {Request.InputStream.Length} bytes");
Notification notification = Client.ParseMessage(Request.HttpMethod, Request.ContentType, Request.InputStream);
if (notification is PaymentWaitingForCaptureNotification paymentWaitingForCaptureNotification)
{
Payment payment = paymentWaitingForCaptureNotification.Object;
if (payment.Paid)
{
Log($"Got message: payment.id={payment.Id}, payment.paid={payment.Paid}");
// 4. Подтвердите готовность принять платеж
_client.CapturePayment(payment.Id);
}
}
}
catch (Exception exception)
{
Log(exception.ToString());
}
}
void Log(string msg)
{
File.AppendAllLines(Server.MapPath("log.txt"), new[] { $"{DateTime.UtcNow} {msg}" });
}
}
}