This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from pieterpoorthuis/master
Add IPN processor in tutorial
- Loading branch information
Showing
6 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
|
||
$buyer = new \Bitpay\Buyer(); | ||
$buyer | ||
->setEmail('[email protected]') | ||
->setEmail('[email protected]'); | ||
|
||
// Add the buyers info to invoice | ||
$invoice->setBuyer($buyer); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2014-2017 BitPay | ||
* | ||
* 004 - IPN logger | ||
* | ||
* Requirements: | ||
* - Account on https://test.bitpay.com | ||
* - Baisic PHP Knowledge | ||
* - Private and Public keys from 001.php | ||
* - Token value obtained from 002.php | ||
* - Invoice created & paid | ||
*/ | ||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
|
||
$myfile = fopen("/tmp/BitPayIPN.log", "a"); | ||
|
||
$raw_post_data = file_get_contents('php://input'); | ||
|
||
$date = date('m/d/Y h:i:s a', time()); | ||
|
||
if (false === $raw_post_data) { | ||
fwrite($myfile, $date . " : Error. Could not read from the php://input stream or invalid Bitpay IPN received.\n"); | ||
fclose($myfile); | ||
throw new \Exception('Could not read from the php://input stream or invalid Bitpay IPN received.'); | ||
} | ||
|
||
$ipn = json_decode($raw_post_data); | ||
|
||
if (true === empty($ipn)) { | ||
fwrite($myfile, $date . " : Error. Could not decode the JSON payload from BitPay.\n"); | ||
fclose($myfile); | ||
throw new \Exception('Could not decode the JSON payload from BitPay.'); | ||
} | ||
|
||
if (true === empty($ipn -> 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"); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
# Tutorial | ||
========================== | ||
|
||
This tutorial contains four scripts: | ||
|
||
Script 001 & 002 need to be executed once, to properly configure your local installation. | ||
|
||
Actual BitPay invoice creation is done in script 003; this script can be run permanently. | ||
|
||
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. | ||
|
||
^^ the above two scripts need to be executed only once. | ||
These first two scripts need to be executed only once. | ||
|
||
## Script 3: creating BitPay invoices | ||
3. 003_createInvoice.php : creates a BitPay invoice. Please make sure to fill in the API token received from 002_pair.php | ||
|
||
3. 003_createInvoice.php : creates a BitPay invoice. Please make sure to fill in the API token received from 003_createInvoice.php | ||
## 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.: | ||
``` | ||
// You will receive IPN's at this URL, should be HTTPS for security purposes! | ||
$invoice->setNotificationUrl('https://yourserver.com/IPNlogger.php'); | ||
``` | ||
|
||
^^ this last script (003_createInvoice.php) can be executed permanently using the API token. | ||
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. | ||
To begin, please visit https://test.bitpay.com and register for a test merchant account. | ||
If you are looking for a testnet wallet to test with, please visit https://bitpay.com/wallet and | ||
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_generateKeys.php` and continuing on to the next | ||
files in order. | ||
|
||
|
||
Examples (c) 2014-2017 BitPay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a304951
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, how about update VERSION file?