-
Notifications
You must be signed in to change notification settings - Fork 3
/
BTCPayServerPaymentPlugin.cs
153 lines (130 loc) · 11.7 KB
/
BTCPayServerPaymentPlugin.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Common.Configuration;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Directory;
using Grand.Infrastructure.Plugins;
namespace Payments.BTCPayServer
{
public class BTCPayServerPaymentPlugin : BasePlugin, IPlugin
{
private readonly ITranslationService _translationService;
private readonly ILanguageService _languageService;
private readonly ISettingService _settingService;
private readonly ICurrencyService _currencyService;
public BTCPayServerPaymentPlugin(
ITranslationService translationService,
ILanguageService languageService,
ICurrencyService currencyService,
ISettingService settingService)
{
_translationService = translationService;
_currencyService = currencyService;
_languageService = languageService;
_settingService = settingService;
}
public override string ConfigurationUrl()
{
return "/Admin/BTCPayServer/Configure";
}
private async Task AddBTCCurrency()
{
try
{
await _currencyService.InsertCurrency(new Currency {
DisplayLocale = "en-US",
Name = "Bitcoin",
CurrencyCode = "BTC",
CustomFormatting = "{0} ₿",
Published = true,
DisplayOrder = 1,
});
}
catch
{
// ignored
}
}
public async override Task Install()
{
await _settingService.SaveSetting(new BtcPaySettings {
BtcPayUrl = "",
ApiKey = "",
BtcPayStoreID = "",
WebHookSecret = ""
});
await AddBTCCurrency();
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFee", "Additional fee");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFee.Hint", "The additional fee.");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFeePercentage", "Additional fee. Use percentage");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFeePercentage.Hint", "Determines whether to apply a percentage additional fee to the order total. If not enabled, a fixed value is used.");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.Instructions", "<div class=\"mb-1\"><b>BTCPay Plugin for GrandNode</b></div>" +
"<div class=\"mb-1\">The plugin configuration can be done automatically or manually.</div>" +
"<div class=\"mb-1\"><br/><b>Automatic Configuration:</b></div>" +
"<ul>" +
" <li>Enter the \"BTCPay Url\" parameter.</li>" +
" <li>Click on the \"Configure automatically\" button to be redirected to the key creation page on your BTCPay server.</li>" +
" <li>The \"API Key\", \"BTCPay Store ID\" and \"WebHook Secret\" parameters will be automatically filled. Save.</li>" +
"</ul>" +
"<div class=\"mb-1\"><br/><b>Manual Configuration:</b></div>" +
"<ul>" +
" <li>The \"BTCPay Url,\" \"API Key,\" \"BTCPay Store ID,\" and \"WebHook Secret\" fields must be filled out.</li>" +
" <li>To create the BTCPay API key, <a href =\"https://docs.btcpayserver.org/VirtueMart/#22-create-an-api-key-and-configure-permissions\" target=\"_blank\">read this</a>.<br/>" +
" <i>Note: If you want to use the Refund feature, you must also add the \"Modify your stores\" permission.<br/>" +
" After a refund, an order note is created, indicating the BTCPay link where the customer can request a refund." +
" </i>" +
" </li>" +
" <li>To create the BTCPay WebHook, <a href =\"https://docs.btcpayserver.org/VirtueMart/#23-create-a-webhook-on-btcpay-server\" target=\"_blank\">read this</a>. (use the default secret code generated by BTCPay)</li>" +
"</ul>");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentMethodDescription", "Pay your order in bitcoins");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentMethodDescription2", "After completing the order you will be redirected to the merchant BTCPay instance, where you can make the Bitcoin payment for your order.");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentError", "Error processing the payment. Please try again and contact us if the problem persists.");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayUrl", "BTCPay Url");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayUrl.Hint", "The url of your BTCPay instance");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.CreateApiKey", "Create API key automatically");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.ApiKey", "API Key");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.ApiKey.Hint", "The API Key value generated in your BTCPay instance");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayStoreID", "BTCPay Store ID");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayStoreID.Hint", "The BTCPay Store ID");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.CreateWebhook", "Create webhook automatically");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookUrl", "WebHook Url");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookSecret", "WebHook Secret");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookSecret.Hint", "The WebHook Secret value generated in your BTCPay instance");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookInfo", "Here is the URL to set for the WebHook creation in BTCPay : ");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookNote", "Note: when testing the webhook from BTCPay, you should get an HTTP 422 error.<br/>" +
"This is because BTCPay sends empty data while the GrandNode plugin expects real data.<br/>" +
"This error therefore indicates that the webhook is indeed accessible from BTCPay.<br/>" +
"With a real transaction, you can therefore expect correct operation.");
await this.AddOrUpdatePluginTranslateResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.NoteRefund", "Please visit the following link to claim your refund: ");
await base.Install();
}
public async override Task Uninstall()
{
//settings
await _settingService.DeleteSetting<BtcPaySettings>();
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFee");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFee.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFeePercentage");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.AdditionalFeePercentage.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.Instructions");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentMethodDescription");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentMethodDescription2");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.PaymentError");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayUrl");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayUrl.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.CreateApiKey");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.ApiKey");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.ApiKey.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayStoreID");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.BtcPayStoreID.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.CreateWebhook");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookUrl");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookSecret");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookSecret.Hint");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookInfo");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.WebHookNote");
await this.DeletePluginTranslationResource(_translationService, _languageService, "Plugins.Payments.BTCPayServer.NoteRefund");
await base.Uninstall();
}
}
}