This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestExtensions.cs
77 lines (66 loc) · 2.62 KB
/
RequestExtensions.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
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using PayPalHelper.Core.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace PayPalHelper.Core.Extensions
{
public enum PayPalEnvironment
{
Sandbox,
Live
}
public static class RequestExtensions
{
private static HttpClient _client = new HttpClient();
private static PayPalEnvironment _env;
public static async Task<VerificationResult> VerifyPayPalTransactionAsync(this HttpRequest request, PayPalEnvironment environment, ILogger logger = null)
{
var result = new VerificationResult() { };
try
{
var urls = new Dictionary<PayPalEnvironment, Uri>
{
{ PayPalEnvironment.Live, new Uri("https://ipnpb.paypal.com/") },
{ PayPalEnvironment.Sandbox, new Uri("https://ipnpb.sandbox.paypal.com/") }
};
if (_client.BaseAddress == null)
{
_client.BaseAddress = urls[environment];
_env = environment;
}
else
{
if (environment != _env) throw new Exception("Can't switch environments without restarting your app.");
}
_client.DefaultRequestHeaders.Accept.Clear();
var formValues = new Dictionary<string, string>();
formValues.Add("cmd", "_notify-validate");
foreach (var field in request.Form) formValues.Add(field.Key, field.Value);
var content = new FormUrlEncodedContent(formValues);
var response = await _client.PostAsync("cgi-bin/webscr", content);
if (response.IsSuccessStatusCode)
{
var responseText = await response.Content.ReadAsStringAsync();
result.IsVerified = responseText.Equals("VERIFIED");
if (result.IsVerified)
{
result.Transaction = request.Form.ParseForm<PayPalTransaction>();
}
}
else
{
throw new Exception($"PayPal API call failed: {response.Content}");
}
}
catch (Exception exc)
{
result.Exception = exc;
logger?.LogError(exc, $"Error in {nameof(VerifyPayPalTransactionAsync)}");
}
return result;
}
}
}