如何使用IPN进行货币支付? https://www.coinpayments.net/merchant-tools-ipn
我创建了一个文件并将 IPN 代码放在那里,但是我该怎么做"运行表单发布"这个文件?我必须创建 API 吗?
我想要的IPN是,当支付成功时,我将在SQL中执行一个函数。
但是,如果付款是通过我的站点中的货币付款按钮(配置了POST字段(进行的,则即使放置了我网站的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."nn";
$report .= "POST Datann";
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>
确保使用来自服务器的 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>
只要确保您没有从本地主机运行它,否则Coinpayment服务器将无法从本地主机计算机访问您的IPN URL。您需要在实时服务器上对此进行测试。
几天前我遇到了同样的问题。我切换到 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中。
有两种方法可以在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应该向指定的URL发送IPN,您可以通过执行以下操作来记录所有调用
$postData =file_get_contents("php://input");
file_put_contents("coinpayments.log", $postData, FILE_APPEND);