如何使用IPN CoinPayments?

问题描述 投票:0回答:4

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

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

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

但是,如果通过我网站中的货币付款按钮(配置了帖子字段)进行付款,即使输入我网站的 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'); 

        ?>

我的代码按钮硬币付款:

<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>
php coinpayments-api
4个回答
2
投票

确保使用来自服务器的 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>

只需确保您没有从本地主机运行它,否则硬币支付服务器将无法从本地主机计算机访问您的 IPN URL。您需要在实时服务器上进行测试。


1
投票

几天前我也遇到了同样的问题。我切换到 API 来获取 Tx id 的交易详细信息,这比 IPN 简单得多。 只需将以下代码粘贴到 Coin payment 库 coin payments.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 的困扰。此方法还允许在您的网站上进行交易,而不是硬币支付。


1
投票

有 2 种方法可以在 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.coin payments.net/tutorials/integration/integrating-coin payments-step-1-account-setup

要测试您的IPN,您需要在您的钱包中启用LTCT作为可接受的代币,它们是LTC测试代币并且一文不值,您可以使用它来使用这些代币从自己那里购买/提取。

您可以按照这篇文章看看如何:

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

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

https://www.coin payments.net/help-testnet

“我怎样才能获得测试网硬币?”页面的一部分有一个链接,它将为您提供 20 个 LTCT 进行测试。

当发生任何交易时,硬币支付应将 IPN 发送到指定的 URL,您可以通过执行以下操作记录所有调用

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

0
投票

嗨,我来这里是为了寻找同一问题的解决方案,我花了很多功夫才找到任何解决方案。但最终我自己解决了问题。您可以简单地使用下面的代码来更新您的数据库状态。

按照以下步骤操作 1- 将以下代码粘贴到您的callback.php、webhook.php 或handleIPN.php 文件中。 2- 创建转移支付订单。通过您的付款页面。通过使用 coin payment-inc.php api

3-然后转到您的硬币支付帐户并验证是否创建了任何待处理的转账? 2-如果是的话https://www.coin payments.net/acct-ipn-history 并检查您的 IPN 历史记录。

如果发送成功列显示yes,这意味着您的回调 ipn handler php 代码工作正常并收到了 coin payment 的响应。 No 表示您的 IPN 处理程序代码不起作用。或者有什么错误。

并且否否(剩余 10 次尝试)(剩余 4 次尝试)如果您看到此消息,这意味着已经尝试了 coin payment 回调,但您的 ipn 处理程序代码中有错误。

这是代码,希望它能帮助您解决问题。

<?php    

$merchant_id = 'your_merchant';
$secret = 'your_ip_secret';

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

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

if ($merchant != $merchant_id) {
  die("Invalid Merchant ID");
}

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

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


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

$ipn_type = $_POST['ipn_type'];
$txn_id = $_POST['txn_id'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$amount1 = floatval($_POST['amount1']);
$amount2 = floatval($_POST['amount2']);
$currency1 = $_POST['currency1'];
$currency2 = $_POST['currency2'];
$status = intval($_POST['status']);
$tx_hash = $_POST['send_tx'];



$new_status = '';
        if ($status >= 100) {
            $new_status = 'Completed'; // Payment completed successfully
            $tx_hash = $_POST['send_tx'];
            } elseif ($status == 0) {
            $new_status = 'Pending'; // Pending Payment
            } elseif ($status == 2) {
            $new_status = 'Processing'; // Queued for nightly payout, waiting for Confirmations
            } elseif ($status == -1) {
            $new_status = 'Cancelled'; // Cancelled / Timeout
            } elseif ($status == 1) {
            $new_status = 'Confirmed'; // We have confirmed coin reception from the buyer
            } else {
            $new_status = 'Pending'; // Default status for other pending cases
            $tx_hash = 'empty';
        }

//depending on the API of your system, you may want to check and see if the transaction ID $txn_id has already been handled before at this point

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

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

// 示例:更新数据库中用户的存款状态 include_once('dbconnection.php');

//您可以在此处编写代码以根据状态更新数据库表。

        // Close the database connection
        $conn->close();
        //================END UPDATE===================================

die('IPN OK');

?>

© www.soinside.com 2019 - 2024. All rights reserved.