Skip to content

Commit 7c13d05

Browse files
Merge pull request #43 from whotooktwarden/master
Updating web-kit with examples for usage with rippled-sign-submit pac…
2 parents 87bc4d0 + 7d42a65 commit 7c13d05

File tree

5 files changed

+594
-0
lines changed

5 files changed

+594
-0
lines changed

README_IPN_FILES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The IPN Files are to be used in conjunction with the package found at https://github.com/whotooktwarden/rippled-sign-submit

btcipn.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
// Fill these in with the information from your CoinPayments.net account.
3+
$cp_merchant_id = 'CoinPayment.net Merchant ID';
4+
$cp_ipn_secret = 'Your Instant Payment Notification Secret';
5+
$cp_debug_email = 'Your Debug Email';
6+
$email_to = 'Your Email';
7+
$headers = 'From NXT IPN Script'; $headers = 'From BTC IPN Script';
8+
9+
function clean_string($string) {
10+
11+
$bad = array('content-type','bcc:','to:','cc:','href');
12+
13+
return str_replace($bad,'',$string);
14+
15+
}
16+
17+
//These would normally be loaded from your database, the most common way is to pass the Order ID through the 'custom' POST field.
18+
$order_currency = 'BTC';
19+
$order_minimum = 0.005;
20+
21+
function errorAndDie($error_msg) {
22+
global $cp_debug_email;
23+
if (!empty($cp_debug_email)) {
24+
$report = 'Error: '.$error_msg."\n\n";
25+
$report .= "POST Data\n\n";
26+
foreach ($_POST as $k => $v) {
27+
$report .= "|$k| = |$v|\n";
28+
}
29+
mail($cp_debug_email, 'CoinPayments IPN Error', $report);
30+
}
31+
die('IPN Error: '.$error_msg);
32+
}
33+
34+
if (!isset($_POST['ipn_mode']) || $_POST['ipn_mode'] != 'hmac') {
35+
errorAndDie('IPN Mode is not HMAC');
36+
}
37+
38+
if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
39+
errorAndDie('No HMAC signature sent.');
40+
}
41+
42+
$request = file_get_contents('php://input');
43+
if ($request === FALSE || empty($request)) {
44+
errorAndDie('Error reading POST data');
45+
}
46+
47+
if (!isset($_POST['merchant']) || $_POST['merchant'] != trim($cp_merchant_id)) {
48+
errorAndDie('No or incorrect Merchant ID passed');
49+
}
50+
51+
$hmac = hash_hmac("sha512", $request, trim($cp_ipn_secret));
52+
if ($hmac != $_SERVER['HTTP_HMAC']) {
53+
errorAndDie('HMAC signature does not match');
54+
}
55+
56+
if (!isset($_POST['custom']) || empty($_POST['custom'])) {
57+
errorAndDie('No ripple address sent.');
58+
}
59+
60+
/* VALIDATE RIPPLE ADDRESS */
61+
$rippleRegex = '/^r[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{27,35}$/';
62+
63+
if (preg_match($rippleRegex, $_POST['custom'], $matches) == 0 || FALSE){
64+
errorAndDie('Ripple address invalid');
65+
}
66+
67+
// HMAC Signature verified at this point, load some variables.
68+
69+
$txn_id = $_POST['txn_id'];
70+
$item_name = $_POST['item_name'];
71+
//$item_number = $_POST['item_number'];
72+
$amount1 = floatval($_POST['amount1']);
73+
$amount2 = floatval($_POST['amount2']);
74+
$currency1 = $_POST['currency1'];
75+
$currency2 = $_POST['currency2'];
76+
$status = intval($_POST['status']);
77+
$status_text = $_POST['status_text'];
78+
$receivedAmount = $_POST['received_amount'];
79+
$ripple = $_POST['custom'];
80+
$SF = floatval(0.00025);
81+
82+
//depending on the API of your system, you may want to check and see if the transaction ID,
83+
//$txn_id, has already been handled before at this point
84+
85+
//check if the currently incoming IPN is passing a completed transaction from this list, die if so
86+
//else the transaction has not been handled yet and the script can continue
87+
88+
$lines = file('https://xagate.com/completedtxns.out');
89+
90+
// Loop through our array to check for already processed payments
91+
foreach ($lines as $line_num => $line) {
92+
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
93+
if ($txn_id == trim((string)$line)) {
94+
echo "<b>Found a match!</b><br />";
95+
echo "Line variable is set to $line<br />";
96+
echo "Equal! ".$txn_id."<br />";
97+
die('This TXN_ID has already been confirmed! DO NOT SEND ANOTHER PAYMENT!!');
98+
}
99+
}
100+
// Then continue on with the original file from here...
101+
102+
if ($currency1 != $order_currency) {
103+
errorAndDie('Original currency mismatch!');
104+
}
105+
106+
// Check amount against order total
107+
if ($amount1 < $order_minimum) {
108+
errorAndDie('Amount is less than order total!');
109+
}
110+
111+
if ($status == 100) {
112+
/*
113+
This next section builds the command for executing the ruby script on the server
114+
This ruby script will take in the ripple address, the amount of IOUs to issue, a currency, and the txn_id
115+
*/
116+
$sendAmount = $amount1 - $SF;
117+
$data = $ripple." ".$sendAmount." ".$currency1." ".$txn_id;
118+
$command = "ruby submit.rb"." ".$data;
119+
$output = shell_exec("ruby /full_path_to/submit.rb"." ".$data);//executes the command, receives an array and integer for the return values
120+
echo "<pre>$output</pre>";
121+
122+
$email_message = 'An BTC deposit has been confirmed! ';
123+
124+
$email_message .= ' Ripple: '.clean_string($ripple).' ';
125+
126+
$email_message .= ' Currency1 '.clean_string($currency1).' ';
127+
128+
$email_message .= ' BTC Amount: '.clean_string(($amount1 - $SF)).' ';
129+
130+
$email_message .= ' Transaction ID: '.clean_string($txn_id).' ';
131+
132+
$email_message .= 'Received Amount: '.clean_string($receivedAmount).' ';
133+
134+
$email_message .= 'Currency2: '.clean_string($currency2).' ';
135+
136+
$email_message .= ' The script has completed. If anything went wrong, the client will be refunded in 6 hours or contact support.';
137+
138+
$email_message .= ' The output from submit.rb follows: '.$output.' ';
139+
140+
$subject = "BTC IPN: Script Completed";
141+
142+
'X-Mailer: PHP/' . phpversion();
143+
144+
@mail($email_to, $subject, $email_message, $headers);
145+
146+
die("IPN OK. Hit end of success block.");
147+
}
148+
?>

ethipn.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
// Fill these in with the information from your CoinPayments.net account.
3+
// Fill these in with the information from your CoinPayments.net account.
4+
$cp_merchant_id = 'CoinPayment.net Merchant ID';
5+
$cp_ipn_secret = 'Your Instant Payment Notification Secret';
6+
$cp_debug_email = 'Your Debug Email';
7+
$email_to = 'Your Email';
8+
$headers = 'From NXT IPN Script'; $headers = 'From ETH IPN Script';
9+
10+
function clean_string($string) {
11+
12+
$bad = array('content-type','bcc:','to:','cc:','href');
13+
14+
return str_replace($bad,'',$string);
15+
16+
}
17+
18+
//These would normally be loaded from your database, the most common way is to pass the Order ID through the 'custom' POST field.
19+
$order_currency = 'ETH';
20+
$order_minimum = 1;
21+
22+
function errorAndDie($error_msg) {
23+
global $cp_debug_email;
24+
if (!empty($cp_debug_email)) {
25+
$report = 'Error: '.$error_msg."\n\n";
26+
$report .= "POST Data\n\n";
27+
foreach ($_POST as $k => $v) {
28+
$report .= "|$k| = |$v|\n";
29+
}
30+
mail($cp_debug_email, 'CoinPayments IPN Error', $report);
31+
}
32+
die('IPN Error: '.$error_msg);
33+
}
34+
35+
if (!isset($_POST['ipn_mode']) || $_POST['ipn_mode'] != 'hmac') {
36+
errorAndDie('IPN Mode is not HMAC');
37+
}
38+
39+
if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
40+
errorAndDie('No HMAC signature sent.');
41+
}
42+
43+
$request = file_get_contents('php://input');
44+
if ($request === FALSE || empty($request)) {
45+
errorAndDie('Error reading POST data');
46+
}
47+
48+
if (!isset($_POST['merchant']) || $_POST['merchant'] != trim($cp_merchant_id)) {
49+
errorAndDie('No or incorrect Merchant ID passed');
50+
}
51+
52+
$hmac = hash_hmac("sha512", $request, trim($cp_ipn_secret));
53+
if ($hmac != $_SERVER['HTTP_HMAC']) {
54+
errorAndDie('HMAC signature does not match');
55+
}
56+
57+
if (!isset($_POST['custom']) || empty($_POST['custom'])) {
58+
errorAndDie('No ripple address sent.');
59+
}
60+
61+
/* VALIDATE RIPPLE ADDRESS */
62+
$rippleRegex = '/^r[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{27,35}$/';
63+
64+
if (preg_match($rippleRegex, $_POST['custom'], $matches) == 0 || FALSE){
65+
errorAndDie('Ripple address invalid');
66+
}
67+
68+
// HMAC Signature verified at this point, load some variables.
69+
70+
$txn_id = $_POST['txn_id'];
71+
$item_name = $_POST['item_name'];
72+
//$item_number = $_POST['item_number'];
73+
$amount1 = floatval($_POST['amount1']);
74+
$amount2 = floatval($_POST['amount2']);
75+
$currency1 = $_POST['currency1'];
76+
$currency2 = $_POST['currency2'];
77+
$status = intval($_POST['status']);
78+
$status_text = $_POST['status_text'];
79+
$receivedAmount = $_POST['received_amount'];
80+
$ripple = $_POST['custom'];
81+
$SF = floatval(0.00035);
82+
83+
//depending on the API of your system, you may want to check and see if the transaction ID,
84+
//$txn_id, has already been handled before at this point
85+
86+
//check if the currently incoming IPN is passing a completed transaction from this list, die if so
87+
//else the transaction has not been handled yet and the script can continue
88+
89+
$lines = file('https://xagate.com/completedtxns.out');
90+
91+
// Loop through our array to check for already processed payments
92+
foreach ($lines as $line_num => $line) {
93+
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
94+
if ($txn_id == trim((string)$line)) {
95+
echo "<b>Found a match!</b><br />";
96+
echo "Line variable is set to $line<br />";
97+
echo "Equal! ".$txn_id."<br />";
98+
die('This TXN_ID has already been confirmed! DO NOT SEND ANOTHER PAYMENT!!');
99+
}
100+
}
101+
// Then continue on with the original file from here...
102+
103+
if ($currency1 != $order_currency) {
104+
errorAndDie('Original currency mismatch!');
105+
}
106+
107+
// Check amount against order total
108+
if ($amount1 < $order_minimum) {
109+
errorAndDie('Amount is less than order total!');
110+
}
111+
112+
if ($status == 100) {
113+
/*
114+
This next section builds the command for executing the ruby script on the server
115+
This ruby script will take in the ripple address, the amount of IOUs to issue, a currency, and the txn_id
116+
*/
117+
$sendAmount = $amount1 - $SF;
118+
$data = $ripple." ".$sendAmount." ".$currency1." ".$txn_id;
119+
$command = "ruby submit.rb"." ".$data;
120+
$output = shell_exec("ruby /full_path_to/submit.rb"." ".$data);//executes the command, receives an array and integer for the return values
121+
echo "<pre>$output</pre>";
122+
123+
$email_message = 'An ETH deposit has been confirmed! ';
124+
125+
$email_message .= ' Ripple: '.clean_string($ripple).' ';
126+
127+
$email_message .= ' Currency1 '.clean_string($currency1).' ';
128+
129+
$email_message .= ' ETH Amount: '.clean_string(($amount1 - $SF)).' ';
130+
131+
$email_message .= ' Transaction ID: '.clean_string($txn_id).' ';
132+
133+
$email_message .= 'Received Amount: '.clean_string($receivedAmount).' ';
134+
135+
$email_message .= 'Currency2: '.clean_string($currency2).' ';
136+
137+
$email_message .= ' The script has completed. If anything went wrong, the client will be refunded in 6 hours or contact support.';
138+
139+
$email_message .= ' The output from submit.rb follows: '.$output.' ';
140+
141+
$subject = "ETH IPN: Script Completed";
142+
143+
'X-Mailer: PHP/' . phpversion();
144+
145+
@mail($email_to, $subject, $email_message, $headers);
146+
147+
die("IPN OK. Hit end of success block.");
148+
}
149+
?>

0 commit comments

Comments
 (0)