Skip to content

Commit

Permalink
Updating greenfield library; bumping version.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet committed Dec 27, 2022
1 parent 6c3da16 commit 2a1e698
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 92 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function createPullPayment()
$paymentCurrency = 'BTC';
$paymentPeriod = null;
$boltExpiration = 1;
$autoApproveClaims = false;
$startsAt = null;
$expiresAt = null;
$paymentMethods = ['BTC'];
Expand All @@ -54,6 +55,7 @@ public function createPullPayment()
$paymentCurrency,
$paymentPeriod,
$boltExpiration,
$autoApproveClaims,
$startsAt,
$expiresAt,
$paymentMethods
Expand Down Expand Up @@ -109,13 +111,29 @@ public function markPayoutAsPaid()
}
}

public function approvePayout()
{
$payoutId ='';
try {
$client = new PullPayment($this->host, $this->apiKey);
var_dump($client->approvePayout(
$this->storeId,
$payoutId,
0,
null
));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}

public function getPullPayment()
{
$pullPaymentId = '';

try {
$client = new PullPayment($this->host, $this->apiKey);
var_dump($client->markPayoutAsPaid(
var_dump($client->getPullPayment(
$this->storeId,
$pullPaymentId
));
Expand All @@ -131,7 +149,7 @@ public function getPayouts()

try {
$client = new PullPayment($this->host, $this->apiKey);
var_dump($client->markPayoutAsPaid(
var_dump($client->getPayouts(
$pullPaymentId,
$includeCancelled
));
Expand All @@ -149,7 +167,7 @@ public function createPayout()

try {
$client = new PullPayment($this->host, $this->apiKey);
var_dump($client->markPayoutAsPaid(
var_dump($client->createPayout(
$pullPaymentId,
$destination,
$amount,
Expand All @@ -167,7 +185,7 @@ public function getPayout()

try {
$client = new PullPayment($this->host, $this->apiKey);
var_dump($client->markPayoutAsPaid(
var_dump($client->getPayout(
$pullPaymentId,
$payoutId
));
Expand All @@ -183,6 +201,7 @@ public function getPayout()
//$pp->archivePullPayment();
//$pp->cancelPayout();
//$pp->markPayoutAsPaid();
//$pp->approvePayout();
//$pp->getPullPayment();
//$pp->getPayouts();
//$pp->createPayout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,169 @@

// Import Invoice client class.
use BTCPayServer\Client\Invoice;

// Fill in with your BTCPay Server data.
$apiKey = '';
$host = ''; // e.g. https://your.btcpay-server.tld
$storeId = '';
$secret = ""; // webhook secret configured in the BTCPay UI

$myfile = fopen("BTCPay.log", 'ab');

$raw_post_data = file_get_contents('php://input');

$date = date('m/d/Y h:i:s a');

if (false === $raw_post_data) {
fwrite($myfile, $date . " : Error. Could not read from the php://input stream or invalid BTCPayServer payload received.\n");
fclose($myfile);
throw new \Exception('Could not read from the php://input stream or invalid BTCPayServer payload received.');
}

$payload = json_decode($raw_post_data, false, 512, JSON_THROW_ON_ERROR);

if (true === empty($payload)) {
fwrite($myfile, $date . " : Error. Could not decode the JSON payload from BTCPay.\n");
fclose($myfile);
throw new \Exception('Could not decode the JSON payload from BTCPay.');
}

// verify hmac256
$headers = getallheaders();
$sig = $headers['Btcpay-Sig'];

if ($sig !== "sha256=" . hash_hmac('sha256', $raw_post_data, $secret)) {
fwrite($myfile, $date . " : Error. Invalid Signature detected! \n was: " . $sig . " should be: " . hash_hmac('sha256', $raw_post_data, $secret) . "\n");
fclose($myfile);
throw new \Exception('Invalid BTCPayServer payment notification message received - signature did not match.');
}

if (true === empty($payload->invoiceId)) {
fwrite($myfile, $date . " : Error. Invalid BTCPayServer payment notification message received - did not receive invoice ID.\n");
fclose($myfile);
throw new \Exception('Invalid BTCPayServer payment notification message received - did not receive invoice ID.');
}

try {
$client = new Invoice($host, $apiKey);
$invoice = $client->getInvoice($storeId, $payload->invoiceId);
} catch (\Throwable $e) {
fwrite($myfile, "Error: " . $e->getMessage());
throw $e;
use BTCPayServer\Client\Webhook;

class WebhookExample
{
public $apiKey;
public $host;
public $storeId;
public $secret;
public $webhookId;

public function __construct()
{
// Fill in with your BTCPay Server data.
$this->apiKey = '';
$this->host = ''; // e.g. https://your.btcpay-server.tld
$this->storeId = '';
$this->secret = ''; // webhook secret as shown in the webhook UI / returned by createWebhook()
$this->webhookId = ''; // only needed for the updateWebhook() example.
}

public function processWebhook()
{
$myfile = fopen("BTCPay.log", 'ab');
$raw_post_data = file_get_contents('php://input');

$date = date('m/d/Y h:i:s a');

if (false === $raw_post_data) {
fwrite(
$myfile,
$date . " : Error. Could not read from the php://input stream or invalid BTCPayServer payload received.\n"
);
fclose($myfile);
throw new \RuntimeException(
'Could not read from the php://input stream or invalid BTCPayServer payload received.'
);
}

$payload = json_decode($raw_post_data, false, 512, JSON_THROW_ON_ERROR);

if (empty($payload)) {
fwrite(
$myfile,
$date . " : Error. Could not decode the JSON payload from BTCPay.\n"
);
fclose($myfile);
throw new \RuntimeException('Could not decode the JSON payload from BTCPay.');
}

// verify hmac256
$headers = getallheaders();
$sig = $headers['Btcpay-Sig'];

$webhookClient = new Webhook($this->host, $this->apiKey);

if ($webhookClient->isIncomingWebhookRequestValid($raw_post_data, $sig, $this->secret)) {
fwrite(
$myfile,
$date . " : Error. Invalid Signature detected! \n was: " . $sig . " should be: " . hash_hmac(
'sha256',
$raw_post_data,
$this->secret
) . "\n"
);
fclose($myfile);
throw new \RuntimeException(
'Invalid BTCPayServer payment notification message received - signature did not match.'
);
}

if (true === empty($payload->invoiceId)) {
fwrite(
$myfile,
$date . " : Error. Invalid BTCPayServer payment notification message received - did not receive invoice ID.\n"
);
fclose($myfile);
throw new \RuntimeException(
'Invalid BTCPayServer payment notification message received - did not receive invoice ID.'
);
}

// Load an existing invoice with the provided invoiceId.
// Most of the time this is not needed as you can listen to specific webhook events
// See: https://docs.btcpayserver.org/API/Greenfield/v1/#tag/Webhooks/paths/InvoiceCreated/post
try {
$client = new Invoice($this->host, $this->apiKey);
$invoice = $client->getInvoice($this->storeId, $payload->invoiceId);
} catch (\Throwable $e) {
fwrite($myfile, "Error: " . $e->getMessage());
throw $e;
}

// optional: check whether your webhook is of the desired type
if ($payload->type !== "InvoiceSettled") {
throw new \RuntimeException(
'Invalid payload message type. Only InvoiceSettled is supported, check the configuration of the webhook.'
);
}

$invoicePrice = $invoice->getData()['amount'];
$buyerEmail = $invoice->getData()['metadata']['buyerEmail'];

fwrite(
$myfile,
$date . " : Payload received for BtcPay invoice " . $payload->invoiceId . " Type: " . $payload->type . " Price: " . $invoicePrice . " E-Mail: " . $buyerEmail . "\n"
);
fwrite($myfile, "Raw payload: " . $raw_post_data . "\n");

// your own processing code goes here!

echo 'OK';
}

public function createWebhook()
{
$url = 'https://createdurl.test.example.com/webhook';
$specificEvents = [
'InvoiceExpired',
'InvoiceSettled',
'InvoiceInvalid'
];

try {
$client = new \BTCPayServer\Client\Webhook($this->host, $this->apiKey);
var_dump($client->createWebhook($this->storeId, $url, $specificEvents, null));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}

public function updateWebhook()
{
$url = 'https://updatedurl.test.example.com/webhook';
$specificEvents = [
'InvoiceReceivedPayment',
'InvoicePaymentSettled',
'InvoiceProcessing',
'InvoiceExpired',
'InvoiceSettled',
'InvoiceInvalid'
];

try {
$client = new \BTCPayServer\Client\Webhook($this->host, $this->apiKey);
var_dump($client->updateWebhook($this->storeId, $url, $this->webhookId, $specificEvents));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}

public function getWebhook()
{
try {
$client = new \BTCPayServer\Client\Webhook($this->host, $this->apiKey);
var_dump($client->getWebhook($this->storeId, $this->webhookId));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}
}

// optional: check whether your webhook is of the desired type
if ($payload->type !== "InvoiceSettled") {
throw new \Exception('Invalid payload message type. Only InvoiceSettled is supported, check the configuration of the webhook.');
}

$invoicePrice = $invoice->getData()['amount'];
$buyerEmail = $invoice->getData()['metadata']['buyerEmail'];

fwrite($myfile, $date . " : Payload received for BtcPay invoice " . $payload->invoiceId . " Type: " . $payload->type . " Price: " . $invoicePrice . " E-Mail: " . $buyerEmail . "\n");
fwrite($myfile, "Raw payload: " . $raw_post_data . "\n");

// your own processing code goes here!

echo 'OK';
$wh = new WebhookExample();
//$wh->processWebhook();
//$wh->createWebhook();
//$wh->getWebhook();
//$wh->updateWebhook();
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function createPullPayment(
string $currency,
?int $period,
?int $BOLT11Expiration,
?bool $autoApproveClaims = false,
?int $startsAt,
?int $expiresAt,
array $paymentMethods
Expand All @@ -60,6 +61,7 @@ public function createPullPayment(
'currency' => $currency,
'period' => $period,
'BOLT11Expiration' => $BOLT11Expiration,
'autoApproveClaims' => $autoApproveClaims,
'startsAt' => $startsAt,
'expiresAt' => $expiresAt,
'paymentMethods' => $paymentMethods
Expand Down Expand Up @@ -156,7 +158,7 @@ public function markPayoutAsPaid(
): bool {
$url = $this->getApiUrl() . 'stores/' .
urlencode($storeId) . '/' . 'payouts/' .
urlencode($payoutId);
urlencode($payoutId) . '/mark-paid';

$headers = $this->getRequestHeaders();
$method = 'POST';
Expand Down
Loading

0 comments on commit 2a1e698

Please sign in to comment.