首页 > 解决方案 > 如何使用 IPN CoinPayments?

问题描述

如何使用 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>

标签: phpcoinpayments-api

解决方案


几天前我也面临同样的问题。我切换到 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 中进行交易。


推荐阅读