在我的网站上,我们将交易保持为待处理状态,并在发货时捕获它们(通常我们需要更改金额/取消,这样做在会计方面更容易(。
当我们准备发货时,我们将所有指定的订单(具有指定的订单状态(与authorize中的发票#/订单id进行匹配。
问题是authorize.net API只允许1000个交易限额,因此当使用他们的GetUnsettledTransactionListRequest
函数时,会错过超过该金额的未结算交易。
我可以将分页限制设置为1000,也可以将偏移量设置为1000(或者999,还不确定是哪个(,所以我认为我需要做的是检查存储结果大小的数组是否为1000,如果是,则获取下一个1000个结果存储在数组中。但关于下一个循环,我必须手动说300040005000(我们没有那么多交易,但仍然有(。
这是我当前的代码:
function getUnsettledTransactionList()
{
//get orders that are in the exp status
$orders_pending_query = tep_db_query("select orders_id as invoice_number from " . TABLE_ORDERS . " where orders_status = '15' order by invoice_number");
$orders_pending = array();
while ($row = mysqli_fetch_array($orders_pending_query, MYSQLI_ASSOC)) {
$orders_pending[] = $row;
}
/* Create a merchantAuthenticationType object with authentication details
retrieved from the constants file */
$merchantAuthentication = new AnetAPIMerchantAuthenticationType();
$merchantAuthentication->setName(SampleCodeConstants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(SampleCodeConstants::MERCHANT_TRANSACTION_KEY);
// Set the transaction's refId
$refId = 'ref' . time();
$request = new AnetAPIGetUnsettledTransactionListRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$controller = new AnetControllerGetUnsettledTransactionListController($request);
$response = $controller->executeWithApiResponse(netauthorizeapiconstantsANetEnvironment::PRODUCTION);
$transactionArray = array();
$resulttrans = array();
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
if (null != $response->getTransactions()) {
foreach ($response->getTransactions() as $tx) {
$transactionArray[] = array(
'transaction_id' => $tx->getTransId(),
'invoice_number' => $tx->getInvoiceNumber()
);
// echo "TransactionID: " . $tx->getTransId() . "order ID:" . $tx->getInvoiceNumber() . "Amount:" . $tx->getSettleAmount() . "<br/>";
}
//match the array column by invoice mumber to find all the transaction ids for cards to capture
$invoiceNumbers = array_column($orders_pending, "invoice_number");
$result = array_filter($transactionArray, function ($x) use ($invoiceNumbers) {
return in_array($x["invoice_number"], $invoiceNumbers);
});
$resulttrans = array_column($result, "transaction_id");
print_r($resulttrans);
} else {
echo "No unsettled transactions for the merchant." . "n";
}
} else {
echo "ERROR : Invalid responsen";
$errorMessages = $response->getMessages()->getMessage();
echo "Response : " . $errorMessages[0]->getCode() . " " . $errorMessages[0]->getText() . "n";
}
return $resulttrans;
}
GetUnsettledTransactionListRequest
提供了对结果进行分页的功能。因此,在处理了前1000个结果后,您可以请求下1000个结果。
我不使用Authnet SDK,但看起来你可以使用AnetAPIPagingType()
为你处理页面:
$pagenum = 1;
$transactionArray = array();
do {
// ...code truncated...
$request = new AnetAPIGetUnsettledTransactionListRequest();
$request->setMerchantAuthentication($merchantAuthentication);
// Paging code
$paging = new AnetAPIPagingType();
$paging->setLimit("1000");
$paging->setOffset($pagenum);
$request->setPaging($paging);
$controller = new AnetControllerGetUnsettledTransactionListController($request);
// ...code truncated...
$numResults = (int) $response->getTotalNumInResultSet();
// ...code truncated...
$pagenum++;
} while ($numResults === 1000);
JSON如下所示:
{
"getUnsettledTransactionListRequest": {
"merchantAuthentication": {
"name": "",
"transactionKey": ""
},
"paging": {
"limit": "1000",
"offset": "1"
}
}
}