|
| 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