How to use IPN CoinPayments?

10.6k Views Asked by At

How can I use IPN for currency payments? https://www.coinpayments.net/merchant-tools-ipn

I created a file and put the IPN code there, but what do I have to do "run form post" this file? Do I have to create an API?

What I wanted with IPN was that when the payout was successful, I would execute a function in SQL.

But if the payment is made by the button of payments of currencies in my site (configured with POST FIELDS) nothing happens, even putting IPN URL of my site

Can someone help me?

IPN code:

 <?php

    $merchant_id = 'mymerchantid';
    $secret = 'mysecretipn';

    $cp_debug_email = 'myemaildebug';

    function errorAndDie($error_msg) {
        global $cp_debug_email;
        if (!empty($cp_debug_email)) {
            $report = 'Error: '.$error_msg."\n\n";
            $report .= "POST Data\n\n";
            foreach ($_POST as $k => $v) {
                $report .= "|$k| = |$v|\n";
            }
            mail($cp_debug_email, 'CoinPayments IPN Error', $report);
        }
        die('IPN Error: '.$error_msg);
    }

    if (!isset($_POST['ipn_mode']) || $_POST['ipn_mode'] != 'hmac') { 
        $ipnmode = $_POST['ipn_mode'];
        errorAndDie("IPN Mode is not HMAC $ipnmode"); 
    } 

    if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
        errorAndDie("No HMAC signature sent");
    }

    $merchant = isset($_POST['merchant']) ? $_POST['merchant']:'';
    if (empty($merchant)) {
        errorAndDie("No Merchant ID passed");
    }

    if (!isset($_POST['merchant']) || $_POST['merchant'] != trim($merchant_id)) {
        errorAndDie('No or incorrect Merchant ID passed');
    }

    $request = file_get_contents('php://input');
    if ($request === FALSE || empty($request)) {
        errorAndDie("Error reading POST data");
    }

    $hmac = hash_hmac("sha512", $request, $secret);
    if ($hmac != $_SERVER['HTTP_HMAC']) {
        errorAndDie("HMAC signature does not match");
    }


        // HMAC Signature verified at this point, load some variables. 


        $status = intval($_POST['status']); 
        $status_text = $_POST['status_text'];

        $txn_id = $_POST['txn_id'];
        $currency1 = $_POST['currency1']; 
        $currency2 = $_POST['currency2'];

        $amount1 = floatval($_POST['amount1']); 
        $amount2 = floatval($_POST['amount2']); 

        $order_currency = 'USD'; 
        $order_total = $amount1;

        $subtotal = $_POST['subtotal'];
        $shipping = $_POST['shipping'];


        ///////////////////////////////////////////////////////////////


        // Check the original currency to make sure the buyer didn't change it. 
        if ($currency1 != $order_currency) { 
            errorAndDie('Original currency mismatch!'); 
        }     

        if ($amount1 < $order_total) { 
            errorAndDie('Amount is less than order total!'); 
        } 

        if ($status >= 100 || $status == 2) { 
           //my code SQL
            }
        } else if ($status < 0) { 
            //my code SQL

        } else { 
//my code SQL
            }
        } 
        die('IPN OK'); 

        ?>

My code BUTTON COINPAYMENTS:

<form action="https://www.coinpayments.net/index.php" method="post">

                        <input type="hidden" name="cmd" value="_pay_simple">

                        <input type="hidden" name="reset" value="1">

                        <input type="hidden" name="merchant" value="mymerchant">

                        <input type="hidden" name="currency" value="USD">

                        <input type="hidden" name="custom" value="<?php echo $value?>">

                        <input type="hidden" name="amountf" value="<?php echo $value?>">

                        <input type="hidden" name="item_name" value="Testing"?>">

                        <input type="hidden" name="invoice" value="Testing">

                        <input type="hidden" name="allow_amount" value="1">

                        <input type="hidden" name="allow_currency" value="1">

                        <input type="hidden" name="allow_currencies" value="BTC,LTC,DOGE,ETH,BCH,DASH,ETC,BCN,POT,XVG,ZEC,ZEN,PPC,BLK,CURE,CRW,DCR,GLD,CLUB,BITB,BRK,CLOAK,DGB,EBST,EXP,FLC,GRS,KMD,KRS,LEC,LSK,MUE,NAV,NEO,NMC,NXT,PINK,PIVX,POA,PROC,QTUM,SMART,SNBL,SOXAX,STEEM,STRAT,SYS,TPAY,TRIG,UBQ,UNIT,VTC,WAVES,XCP,XEM,XMR,XSN,XZC">

                        <input type="hidden" name="success_url" value="mysuccesurl">

                        <input type="hidden" name="cancel_url" value="mycancelurl">

                        <input type="hidden" name="ipn_url" value="myipnurl"> 

                        <input type="hidden" name="email" value="<?php echo getEmail($login)?>">

                        <input type="hidden" name="first_name" value="<?php echo getName($login)?>">

                        <input type="hidden" name="last_name" value="<?php echo getLastName($login)?>">

                        <br>
                        <br>
                        <div align="center">
                            <button class="btn btn-success" type="submit">SUBMIT</button><br>
                        </div>
                    </form>
3

There are 3 best solutions below

3
natasha krishnan On

I was facing the same problem few days ago. I switched to the api for getting the transactions details for a Tx id which was much simpler than IPN. Just paste the following code in Coinpayments library coinpayments.inc.php

}

public function GetTransactionInformation($txId) {      
    $req = array(
        'txid' => $txId,

    );
    return $this->api_call('get_tx_info', $req);
}

Now for getting details just do

   <?php
  require('./coinpayments.inc.php');
    $cps = new CoinPaymentsAPI();
   $cps->Setup('Your_Private_Key', 'Your_Public_Key');
   $result = $cps->GetTransactionInformation('The_TX_ID');
    //get the array info of transaction
    if ($result['error'] == 'ok') {
    print_r ($result);
 } else {
    print 'Error: '.$result['error']."\n";
 }
    ?> 

You should get result in Array. For getting in Json output just replace

 print_r ($result);

With

print $result['result']['status']

Replace status with different arrays. I believe it solved you'r problem without getting hassled in IPN. This method also provides allows transactions to happen in you'r website instead of Coinpayments.

0
AAA On

There are 2 ways to register your IPN page on coinPayments:

1: put it in the form

<form action='https://www.coinpayments.net/index.php' method='post' id='form'>
    .....
    <input type="hidden" name="ipn_url" value="https://$domain/myIpnPage.php">
    .....
</form>

2: setting IPN

You can go to your 'account settings ->merchant settings -> IPN URL' and add it there, here is an article that will guide you step by step:

https://blog.coinpayments.net/tutorials/integration/integrating-coinpayments-step-1-account-setup

To test you IPN, you will need to enable LTCT as an accepted coin in your wallet, they are LTC Test coins and worth nothing, and you can use that to purchase/withdraw from yourself using these coins.

you can follow this article to see how:

https://blog.coinpayments.net/tutorials/integration/integrating-coinpayments-step-4-testing-integration

To get LTCT coins just log into your account on coinpayments, then go to

https://www.coinpayments.net/help-testnet

under "How can I get Testnet coins?" part of the page there is a link where it will give you 20 LTCT to test with.

when any transaction occurs coinpayments should send an IPN to the specified URL, you can log all calls by doing

$postData =file_get_contents("php://input");
file_put_contents("coinpayments.log", $postData, FILE_APPEND);
0
Benjamin On

Make sure you update the ipn_url hidden field in your form with a url from your server which you want to serve as your callback url:) just like this form below.

<form action='https://www.coinpayments.net/index.php' method='post' id='form'>
.....
<input type="hidden" name="ipn_url" value="https://$domain/myIpnPage.php">
.....</form>

just make sure you are not running it from localhost else the coinpayment server will not be able to access your ipn url from your localhost computer. You need to test this on a live server.