$fetchedTxDateTime = new DateTime($fetchedTx->timestamp); if ($latestTxDateTime < $fetchedTxDateTime) { $latestTx = $fetchedTx; } } } } // if we are here, we got the latest transaction // We want to worry about only transactions in the New status - because these transactions have not // given next cycle's entitlements to the customer - so we want to make if ($latestTx != null) { print 'Latest Transaction ' . $latestTx->merchantTransactionId . 'has status = ' . $latestTx->statusLog[0]->status . PHP_EOL; if ($latestTx->statusLog[0]->status == 'New') { print 'Attempting to issue refund against pending transaction ID ' . $latestTx->merchantTransactionId . '.' . PHP_EOL; $refund = new Refund(); $refund->setAmount($latestTx->getAmount()); $refund->setTransaction($latestTx); $refund->setCurrency($latestTx->getCurrency()); $nowNewYork = new \DateTime('now', new \DateTimeZone('America/New_York')); $refund->setNote('Refunding due to subscription cancellation at ' . $nowNewYork->format('Y-m-d_h_i_s') . PHP_EOL); $refundAPI = new Refund(); logCall('refundAPI->perform ' . $latestTx->merchantTransactionId); $response = $refundAPI->perform(array($refund)); if (isCallSuccessful($response)) { $refunds = $response['data']->refunds; print 'Issued refund of ' . $refunds[0]->getAmount() . ' against pending transaction ID ' . $latestTx->getMerchantTransactionId() . PHP_EOL; } } } else { // We do not need to do anything since we could not // find a transaction in 'New' status for this AutoBill