From 20708da4deb794111beff3af0a326f352f1ff989 Mon Sep 17 00:00:00 2001 From: Pieter Poorthuis Date: Tue, 9 May 2017 16:22:37 +0200 Subject: [PATCH] Add IPN processor in tutorial --- examples/tutorial/002_pair.php | 1 + examples/tutorial/003_createInvoice.php | 2 +- examples/tutorial/IPNlogger.php | 71 +++++++++++++++++++++++++ examples/tutorial/README.md | 38 ++++++++----- src/Bitpay/Client/Client.php | 2 +- src/Bitpay/User.php | 6 +++ 6 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 examples/tutorial/IPNlogger.php diff --git a/examples/tutorial/002_pair.php b/examples/tutorial/002_pair.php index 348314f7..779f9a26 100644 --- a/examples/tutorial/002_pair.php +++ b/examples/tutorial/002_pair.php @@ -79,6 +79,7 @@ * decided that it makes more sense to allow your application to handle * this exception since each app is different and has different requirements. */ + echo "Pairing failed. Please check whether you're trying to pair a production pairing code on test."; $request = $client->getRequest(); $response = $client->getResponse(); /** diff --git a/examples/tutorial/003_createInvoice.php b/examples/tutorial/003_createInvoice.php index 084a9ff3..5e3cb2c8 100644 --- a/examples/tutorial/003_createInvoice.php +++ b/examples/tutorial/003_createInvoice.php @@ -44,7 +44,7 @@ $buyer = new \Bitpay\Buyer(); $buyer - ->setEmail('buyeremail@test.com') + ->setEmail('buyeremail@test.com'); // Add the buyers info to invoice $invoice->setBuyer($buyer); diff --git a/examples/tutorial/IPNlogger.php b/examples/tutorial/IPNlogger.php new file mode 100644 index 00000000..4bd09d70 --- /dev/null +++ b/examples/tutorial/IPNlogger.php @@ -0,0 +1,71 @@ + id)) { + fwrite($myfile, $date . " : Error. Invalid Bitpay payment notification message received - did not receive invoice ID.\n"); + fclose($myfile); + throw new \Exception('Invalid Bitpay payment notification message received - did not receive invoice ID.'); +} + +// Now fetch the invoice from BitPay +// This is needed, since the IPN does not contain any authentication + +$client = new \Bitpay\Client\Client(); +$network = new \Bitpay\Network\Testnet(); +//$network = new \Bitpay\Network\Livenet(); +$adapter = new \Bitpay\Client\Adapter\CurlAdapter(); +$client->setNetwork($network); +$client->setAdapter($adapter); + +$token = new \Bitpay\Token(); +$client->setToken($token); + +/** + * This is where we will fetch the invoice object + */ +$invoice = $client->getInvoice($ipn->id); +$invoiceId = $invoice->getId(); +$invoiceStatus = $invoice->getStatus(); +$invoiceExceptionStatus = $invoice->getExceptionStatus(); +$invoicePrice = $invoice->getPrice(); + +fwrite($myfile, $date . " : IPN received for BitPay invoice ".$invoiceId." . Status = " .$invoiceStatus." / exceptionStatus = " . $invoiceExceptionStatus." Price = ". $invoicePrice. "\n"); +fwrite($myfile, "Raw IPN: ". $raw_post_data."\n"); + +//Respond with HTTP 200, so BitPay knows the IPN has been received correctly +//If BitPay receives <> HTTP 200, then BitPay will try to send the IPN again with increasing intervals for two more hours. +header("HTTP/1.1 200 OK"); +?> \ No newline at end of file diff --git a/examples/tutorial/README.md b/examples/tutorial/README.md index 37184270..a34774b0 100644 --- a/examples/tutorial/README.md +++ b/examples/tutorial/README.md @@ -1,20 +1,34 @@ -Tutorial - Introduction +# Tutorial ========================== -In this tutorial I will give an example of going through all the steps required -to get up and running with BitPay using the PHP SDK with code examples. +This tutorial contains four scripts: -The code that you find here SHOULD run needing only to modify a few values. +Script 001 & 002 need to be executed once, to properly configure your local installation. -001_generateKeys.php : generates the private/public keys to sign the communication with BitPay. The private/public keys are stored in your filesystem for later usage. -002_pair.php : pairs your private/public keys to your BitPay merchant account. The result is an API token that can be used to create invoices permanently. -^^ the above two scripts need to be executed only once. +Actual BitPay invoice creation is done in script 003; this script can be run permanently. -003_createInvoice.php : creates a BitPay invoice. Please make sure to fill in the API token received from 003_createInvoice.php -^^ this last script can be executed permanently using the API token. +The last script (IPNLogger) processes BitPay's Instant Payment Notifications. +## Script 1 & 2: configuring your local installation +The following two scripts need to be executed once. These scripts will generate your private/public keys and pair them to your BitPay merchant account: +1. 001_generateKeys.php : generates the private/public keys to sign the communication with BitPay. The private/public keys are stored in your filesystem for later usage. +2. 002_pair.php : pairs your private/public keys to your BitPay merchant account. The result is an API token that can be used to create invoices permanently. -* Testing * + +# Script 3: creating BitPay invoices +3. 003_createInvoice.php : creates a BitPay invoice. Please make sure to fill in the API token received from 003_createInvoice.php +This last script (003_createInvoice.php) can be executed permanently using the API token. + +# Script 4: process IPNs +IPNLogger.php processes IPNs (Instant Payment Notifications). This script should be put on your server and be reachable from the internet. Your should put the URL of IPNLogger.php in your invoice creation script, e.g.: +`$invoice + // You will receive IPN's at this URL, should be HTTPS for security purposes! + ->setNotificationUrl('http://yourserver.com/IPNlogger.php'); +` +For more information about IPNs, please see https://bitpay.com/docs/invoice-callbacks + + +## Testing To begin, please visit https://test.bitpay.com and register for a test account. If you are looking for a testnet wallet to test with, please visit https://bitpay.com/wallet and create a new wallet. @@ -22,9 +36,5 @@ create a new wallet. For more information about testing, please see https://bitpay.com/docs/testing -Once you have an account and a testnet wallet, you should begin to go through -this tutorial starting from file `001.php` and continuing on to the next -files in order. - Examples (c) 2014-2017 BitPay diff --git a/src/Bitpay/Client/Client.php b/src/Bitpay/Client/Client.php index 86e88c22..21f88189 100644 --- a/src/Bitpay/Client/Client.php +++ b/src/Bitpay/Client/Client.php @@ -493,7 +493,7 @@ public function getTokens() public function createToken(array $payload = array()) { if (isset($payload['pairingCode']) && 1 !== preg_match('/^[a-zA-Z0-9]{7}$/', $payload['pairingCode'])) { - throw new \ArgumentException("pairing code is not legal"); + throw new \InvalidArgumentException("pairing code is not legal"); } $this->request = $this->createNewRequest(); diff --git a/src/Bitpay/User.php b/src/Bitpay/User.php index 442e60e9..35cae15b 100644 --- a/src/Bitpay/User.php +++ b/src/Bitpay/User.php @@ -292,6 +292,12 @@ public function getNotify() { return $this->notify; } + + /** + * @param bool $boolvalue + * + * @return User + */ public function setNotify($boolvalue) { if (!empty($boolvalue)) {