2

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

我创建了一个文件并将 IPN 代码放在那里,但是我必须如何“运行表单发布”这个文件?我必须创建 API 吗?

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

但是,如果通过我网站中的货币支付按钮(使用 POST FIELDS 配置)进行付款,则即使将我网站的 IPN URL 也不会发生任何事情

有人能帮我吗?

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

        ?>

我的代码按钮 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>
4

3 回答 3

1

几天前我也面临同样的问题。我切换到 api 以获取 Tx 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";
 }
    ?> 

你应该得到数组中的结果。要获得 Json 输出,只需替换

 print_r ($result);

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

用不同的数组替换状态。我相信它可以解决您的问题,而不会在 IPN 中遇到麻烦。此方法还允许在您的网站而不是 Coinpayments 中进行交易。

于 2018-06-24T11:56:12.677 回答
1

确保使用服务器中的 url 更新表单中的 ipn_url 隐藏字段,并将其用作回调 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>

只要确保您没有从 localhost 运行它,否则投币支付服务器将无法从您的 localhost 计算机访问您的 ipn url。您需要在实时服务器上对此进行测试。

于 2019-12-03T19:21:37.890 回答
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

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

当任何交易发生时,coinpayments 应该将 IPN 发送到指定的 URL,您可以通过以下方式记录所有呼叫

$postData =file_get_contents("php://input");
file_put_contents("coinpayments.log", $postData, FILE_APPEND);
于 2019-07-03T11:22:23.920 回答