-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayPalPaymentWebHook.cs
83 lines (72 loc) · 3.82 KB
/
PayPalPaymentWebHook.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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Configuration;
namespace com.businesscentral
{
public static class PayPalPaymentWebHook
{
static string TAG = "PayPalPaymentWebHook";
[FunctionName("PayPalPaymentWebHook")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest req,
ILogger log,
ExecutionContext context)
{
log.LogInformation("Request notified");
if (!req.Method.Equals("GET") && !req.Method.Equals("POST"))
return new BadRequestObjectResult("PayPalPaymentWebHook - Unexpected " + req.Method + " request");
// Validation token for webhook registration
// reply token to accept webhook subscription
var validationToken = req.Query["validationToken"].ToString();
if (!String.IsNullOrEmpty(validationToken))
{
dynamic data = JsonConvert.SerializeObject(validationToken);
return new ContentResult { Content = data, ContentType = "application/json; charset=utf-8", StatusCode = 200 };
}
// Load configuration
var configBuilder = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var config = new ConnectorConfig(configBuilder);
var composer = new MessageComposer(config, log);
var messenger = new MessageConnector(config, log);
var centralConnector = new BusinessCentralConnector(config, log);
// Get webhook and process it
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
log.LogInformation(TAG + " Request notified from webhook : " + requestBody);
// Message sent
var messageEmail = messenger.SendTestMail(requestBody);
log.LogInformation(String.Format(TAG + " SMS/Email Message result : {0}", messageEmail.Status));
// Get webhook and process it
var ev = !String.IsNullOrEmpty(requestBody) ? JsonConvert.DeserializeObject<PayPalWebHook>(requestBody) : null;
log.LogInformation(TAG + " Request notified from webhook : " + requestBody);
// If the webhook signals a payment
if (ev.EventType.Equals("PAYMENT.CAPTURE.COMPLETED"))
{
log.LogInformation(String.Format(TAG + " PAYMENT.CAPTURE.COMPLETED : {0}", ev.Resource.Id));
var order = await centralConnector.GetPayPalOrderByExternalDocumentNumber(ev.Resource.Id);
if (order != null)
{
log.LogInformation(String.Format(TAG + " GetPayPalOrderByExternalDocumentNumber : {0}", order.Number));
await centralConnector.UpdatePayPalOrderById(order, order.ExternalDocumentNumber, config.paymentTermsIdPayPalPaid);
log.LogInformation(String.Format(TAG + " Order updated : {0}", order.Number));
var html = composer.DataBindEmailPayment(order);
await messenger.SendAdminMail(html);
}
else
log.LogError(String.Format(TAG + " PAYMENT.CAPTURE.COMPLETED Order not found {0}", ev.Resource.Id));
}
return new StatusCodeResult(200);
}
}
}