如何使用IPN CoinPayments?

时间:2018-06-21 17:25:32

标签: php

如何使用IPN进行货币付款? https://www.coinpayments.net/merchant-tools-ipn

我创建了一个文件,并将IPN代码放在此处,但是我该怎么做“运行表单发布”此文件?我必须创建一个API吗?

我想要IPN的是,当支付成功时,我将在SQL中执行一个函数。

但是,如果通过我的网站中的货币付款按钮付款(使用POST FIELDS配置),则什么也不会发生,甚至将我网站的IPN网址置于

有人可以帮我吗?

IPN代码:

 <?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'); 

        ?>

我的代码按钮付款:

<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 个答案:

答案 0 :(得分:1)

几天前我遇到了同样的问题。我切换到api以获取Tx ID的交易详细信息,该ID比IPN更为简单。 只需将以下代码粘贴到Coinpayments库coinpayments.inc.php

}

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

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

现在只需获取详细信息即可

   <?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";
 }
    ?> 

您应该在Array中得到结果。 要获得Json输出,只需替换

 print_r ($result);

使用

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

用不同的阵列替换状态。我相信它可以解决您的问题,而不会困扰IPN。此方法还提供了允许交易在您的网站中进行而不是在Coinpayments中进行的功能。

答案 1 :(得分:1)

请确保使用您服务器中要用作回调URL的URL更新表单中的ipn_url隐藏字段,就像下面的此表单一样。

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

只需确保您没有从本地主机运行它,否则coinpayment服务器将无法从本地主机计算机访问您的IPN网址。您需要在实时服务器上对此进行测试。

答案 2 :(得分:0)

有两种方法可以在coinPayments上注册IPN页面:

1:以表格形式

<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:设置IPN

您可以转到“帐户设置->商家设置-> IPN URL”并将其添加到此处, 这是一篇文章,将逐步指导您:

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

要测试您的IPN,您需要启用LTCT作为钱包中可接受的硬币,它们是LTC测试硬币,一文不值,您可以使用它来使用这些硬币进行购买/提现。

您可以阅读本文以了解操作方法:

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

要获取LTCT硬币,只需登录您的硬币支付帐户,然后转到

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

在“我如何获得Testnet硬币?”下页面的一部分有一个链接,它将为您提供20个LTCT进行测试。

发生任何交易时,应通过IPN将IPN发送到指定的网址,您可以通过

记录所有通话
$postData =file_get_contents("php://input");
file_put_contents("coinpayments.log", $postData, FILE_APPEND);