Skip to content

Commit

Permalink
Merge pull request #24 from ndeet/automatic-webhook
Browse files Browse the repository at this point in the history
Automatically setup webhook
  • Loading branch information
ndeet authored Aug 22, 2023
2 parents d752bbd + e9c5f2e commit 1a2e707
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
32 changes: 29 additions & 3 deletions btcpay-greenfield-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://btcpayserver.org
* Text Domain: btcpay-greenfield-for-woocommerce
* Domain Path: /languages
* Version: 2.2.2
* Version: 2.2.3
* Requires PHP: 7.4
* Tested up to: 6.3
* Requires at least: 5.2
Expand All @@ -19,13 +19,14 @@
use BTCPayServer\WC\Gateway\DefaultGateway;
use BTCPayServer\WC\Gateway\SeparateGateways;
use BTCPayServer\WC\Helper\GreenfieldApiAuthorization;
use BTCPayServer\WC\Helper\GreenfieldApiWebhook;
use BTCPayServer\WC\Helper\SatsMode;
use BTCPayServer\WC\Helper\GreenfieldApiHelper;
use BTCPayServer\WC\Helper\Logger;

defined( 'ABSPATH' ) || exit();

define( 'BTCPAYSERVER_VERSION', '2.2.2' );
define( 'BTCPAYSERVER_VERSION', '2.2.3' );
define( 'BTCPAYSERVER_VERSION_KEY', 'btcpay_gf_version' );
define( 'BTCPAYSERVER_PLUGIN_FILE_PATH', plugin_dir_path( __FILE__ ) );
define( 'BTCPAYSERVER_PLUGIN_URL', plugin_dir_url(__FILE__ ) );
Expand Down Expand Up @@ -356,9 +357,20 @@ function init_btcpay_greenfield() {
$btcPaySettingsUrl = admin_url('admin.php?page=wc-settings&tab=btcpay_settings');

$rawData = file_get_contents('php://input');
Logger::debug('Redirect payload: ' . print_r($rawData, true));

$data = json_decode( $rawData, true );

// Seems data does get submitted with url-encoded payload, so parse $_POST here.
// Check if the payload api key comes from the actually requested server. Abort if not.
$storedUrl = get_option('btcpay_gf_url');
if (!GreenfieldApiHelper::checkApiKeyWorks($storedUrl, sanitize_text_field($_POST['apiKey']))) {
$messageAbort = __('Error on verifiying redirected API wey with stored BTCPay Server url. Aborting API wizard. Please try again or do a manual setup.', 'btcpay-greenfield-for-woocommerce');
Logger::debug($messageAbort);
Notice::addNotice('error', $messageAbort);
wp_redirect($btcPaySettingsUrl);
}

// Data does get submitted with url-encoded payload, so parse $_POST here.
if (!empty($_POST)) {
$data['apiKey'] = sanitize_html_class($_POST['apiKey'] ?? null);
if (is_array($_POST['permissions'])) {
Expand All @@ -375,6 +387,20 @@ function init_btcpay_greenfield() {
update_option('btcpay_gf_store_id', $apiData->getStoreID());
update_option('btcpay_gf_connection_details', 'yes');
Notice::addNotice('success', __('Successfully received api key and store id from BTCPay Server API. Please finish setup by saving this settings form.', 'btcpay-greenfield-for-woocommerce'));

// Register a webhook.
if (GreenfieldApiWebhook::registerWebhook($storedUrl, $apiData->getApiKey(), $apiData->getStoreID())) {
$messageWebhookSuccess = __( 'Successfully registered a new webhook on BTCPay Server.', 'btcpay-greenfield-for-woocommerce' );
Notice::addNotice('success', $messageWebhookSuccess, true );
Logger::debug( $messageWebhookSuccess );
} else {
$messageWebhookError = __( 'Could not register a new webhook on the store.', 'btcpay-greenfield-for-woocommerce' );
Notice::addNotice('error', $messageWebhookError );
Logger::debug($messageWebhookError, true);
// Cleanup existing conf.
delete_option('btcpay_gf_webhook');
}

wp_redirect($btcPaySettingsUrl);
} else {
Notice::addNotice('error', __('Please make sure you only select one store on the BTCPay API authorization page.', 'btcpay-greenfield-for-woocommerce'));
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: bitcoin, btcpay, BTCPay Server, btcpayserver, WooCommerce, payment gateway
Requires at least: 5.2
Tested up to: 6.3
Requires PHP: 7.4
Stable tag: 2.2.2
Stable tag: 2.2.3
License: MIT
License URI: https://github.com/btcpayserver/woocommerce-greenfield-plugin/blob/master/license.txt

Expand Down Expand Up @@ -104,6 +104,9 @@ You'll find extensive documentation and answers to many of your questions on [BT

== Changelog ==

= 2.2.3 :: 2023-08-22 =
* Automatically create webhook after redirect.

= 2.2.2 :: 2023-08-22 =
* Fix edgecase JS error on payment method selection.

Expand Down
18 changes: 15 additions & 3 deletions src/Helper/GreenfieldApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,26 @@ public static function getConfig(): array {
}
}

public static function checkApiConnection(): bool {
if ($config = self::getConfig()) {
// todo: replace with server info endpoint.
public static function checkApiKeyWorks(string $url = null, string $apiKey = null): bool {
$config = [];

if ($url && $apiKey) {
$config['url'] = $url;
$config['api_key'] = $apiKey;
} else {
$config = self::getConfig();
}

if ($config) {
$client = new Store($config['url'], $config['api_key']);
if (!empty($stores = $client->getStores())) {
return true;
} else {
Logger::debug('Could not fetch stores from BTCPay Server with the given API key.');
return false;
}
}

return false;
}

Expand Down

0 comments on commit 1a2e707

Please sign in to comment.