Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #220 from pieterpoorthuis/master
Browse files Browse the repository at this point in the history
Add IPN processor in tutorial
  • Loading branch information
Pieter Poorthuis authored May 9, 2017
2 parents b6c8940 + d5e8a02 commit a304951
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/tutorial/002_pair.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
/**
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/003_createInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

$buyer = new \Bitpay\Buyer();
$buyer
->setEmail('[email protected]')
->setEmail('[email protected]');

// Add the buyers info to invoice
$invoice->setBuyer($buyer);
Expand Down
71 changes: 71 additions & 0 deletions examples/tutorial/IPNlogger.php
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");
?>
30 changes: 22 additions & 8 deletions examples/tutorial/README.md
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
2 changes: 1 addition & 1 deletion src/Bitpay/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/Bitpay/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ public function getNotify()
{
return $this->notify;
}

/**
* @param bool $boolvalue
*
* @return User
*/
public function setNotify($boolvalue)
{
if (!empty($boolvalue)) {
Expand Down

1 comment on commit a304951

@tomaszkane
Copy link

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?

Please sign in to comment.