2

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>
wowkin2
  • 5,895
  • 5
  • 23
  • 66
Leonardo Araujo
  • 21
  • 1
  • 1
  • 4
  • Considering this is just used for the callback from CoinPayments to give you the status of the payment, you'll probably want to look at your form code for submitting the payment to make sure everything there looks correct. If it's not hitting CoinPayments at all (look inside your account to see if the transaction attempt shows), then the submission is definitely your problem. – aynber Jun 21 '18 at 17:31
  • In my account settings, CoinPayments are like this: http://prntscr.com/jxpnkb What I understand is that once the person pays, he will run the IPN URL (in the case of the above code), right? – Leonardo Araujo Jun 21 '18 at 17:37
  • Once the person pays, CoinPayments will send the status to the IPN URL, not the customer. – aynber Jun 21 '18 at 17:38
  • Okay, so it is not working as it does not run any SQL function, nothing happens, if I access my IPN URL it returns this: http://prntscr.com/jxprcv – Leonardo Araujo Jun 21 '18 at 17:43
  • Does the transaction show within the CoinPayments console? Does the payer get an error message on POST? – aynber Jun 21 '18 at 17:49
  • Did you mean it with the CoinPayments Console?: http://prntscr.com/jxpwb0 or this in my email (when I access the IPN): http://prntscr.com/jxpwwr So I guess after the person pays, it seems that the IPN does not "execute" or returns. The payments I made for testing – Leonardo Araujo Jun 21 '18 at 17:55
  • Make sure that the IPN url is accessible from the outside world. If it's still not passing the data to your URL, then you may need to contact CoinPayments. – aynber Jun 21 '18 at 18:12
  • My IPN is in a folder named index.php, does it have any conflicts? – Leonardo Araujo Jun 21 '18 at 18:15
  • Make sure you're passing the full url to the IPN, not just the relative path. The full URL must be world-accessible, ie not localhost. – aynber Jun 21 '18 at 18:16
  • I'm sure I'm passing the URL FULL, and the site is hosted – Leonardo Araujo Jun 21 '18 at 18:55
  • Double-check the hidden elements in the browser source. If it doesn't start with `http`, then you're not passing the full url. – aynber Jun 21 '18 at 18:56
  • https:// mysite/panel/gateway/cryptocurrencys/notification/index.php << IPN URL in Account Settings CoinPayments, and Form – Leonardo Araujo Jun 21 '18 at 18:59
  • I realized that if I use the IPN re-send checked CoinPayments, it sends me an email with this error: http://prntscr.com/jxqx56 – Leonardo Araujo Jun 21 '18 at 19:16

3 Answers3

2

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.

Benjamin
  • 133
  • 7
1

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.

  • are you using the Payment ID: created by coin payments? that you can find on the status page? if not how and where are you getting the 'The_TX_ID' – Dynamite Media Sep 08 '19 at 22:00
  • Error: This API Key does not have permission to use that command! – Espector May 27 '20 at 20:41
  • Hope someone finds it useful to the question date, but I believe 'The_TX_ID' will have to be replaced on your codebase to either $txn_id or a $_GET['txn_id'] - this will depend on how and where you are calling GetTransactionInformation function. – gabisajr Apr 10 '21 at 11:06
1

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);
AAA
  • 11
  • 1