public function voidTransaction() { $transaction = Braintree_Transaction::void($transaction_id); if ($transaction) { return $transaction; } else { return false; } }
/** * Refund transaction * * @param $transaction_id * @param null $amount * @param bool $void * @return object */ public function refund($transaction_id, $amount = NULL, $void = TRUE) { //try void if ($void == TRUE) { $result = Braintree_Transaction::void($transaction_id); if ($result->success == TRUE) { return $result; } } //if already settled, do refund $result = Braintree_Transaction::refund($transaction_id, $amount); return $result; }
function test_rangeNode_voidedAt() { $transaction = Braintree_Transaction::saleNoValidate(array('amount' => '1000.00', 'creditCard' => array('number' => '4111111111111111', 'expirationDate' => '05/12'))); $transaction = Braintree_Transaction::void($transaction->id)->transaction; $twenty_min_ago = date_create("now -20 minutes", new DateTimeZone("UTC")); $ten_min_ago = date_create("now -10 minutes", new DateTimeZone("UTC")); $ten_min_from_now = date_create("now +10 minutes", new DateTimeZone("UTC")); $collection = Braintree_Transaction::search(array(Braintree_TransactionSearch::id()->is($transaction->id), Braintree_TransactionSearch::voidedAt()->between($twenty_min_ago, $ten_min_ago))); $this->assertEquals(0, $collection->maximumCount()); $collection = Braintree_Transaction::search(array(Braintree_TransactionSearch::id()->is($transaction->id), Braintree_TransactionSearch::voidedAt()->between($ten_min_ago, $ten_min_from_now))); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($transaction->id, $collection->firstItem()->id); }
<?php require 'template/header.php'; ?> <?php require 'credentials.php'; $id = $_GET['id']; $method = $_GET['method']; switch ($method) { case 'capture': $result = Braintree_Transaction::submitForSettlement($id); break; case 'void': $result = Braintree_Transaction::void($id); break; case 'refund': $result = Braintree_Transaction::refund($id); break; default: die('invalid request'); break; } ?> <div class="page-header"> <h1><?php echo $method . " " . $id; ?> </h1> </div>
/** * @param string $transactionId * @return \Braintree_Result_Successful|\Braintree_Result_Error */ public function void($transactionId) { return \Braintree_Transaction::void($transactionId); }
function testVoid_withValidationError() { $transaction = Braintree_Transaction::saleNoValidate(array('amount' => '100.00', 'creditCard' => array('number' => '5105105105105100', 'expirationDate' => '05/12'))); $this->assertEquals(Braintree_Transaction::AUTHORIZED, $transaction->status); $voided = Braintree_Transaction::voidNoValidate($transaction->id); $this->assertEquals(Braintree_Transaction::VOIDED, $voided->status); $result = Braintree_Transaction::void($transaction->id); $this->assertEquals(false, $result->success); $errors = $result->errors->forKey('transaction')->onAttribute('base'); $this->assertEquals(Braintree_Error_Codes::TRANSACTION_CANNOT_BE_VOIDED, $errors[0]->code); }
/** * Makes method specific manipulations after authorize/clone transaction success * * @param Varien_Object $payment $payment * @param array $result */ protected function _processMethodSpecificTransactionSuccess($payment, $result) { // Saving token if applicable, additional manipulations for multishipping if (isset($result->transaction->creditCard['token']) && $result->transaction->creditCard['token']) { $token = $result->transaction->creditCard['token']; $payment->setTransactionAdditionalInfo('token', $token); if ($payment->getIsMultishipping()) { if (!Mage::registry(self::REGISTER_NAME)) { Mage::register(self::REGISTER_NAME, $token); } if (Mage::getSingleton('checkout/session')->getBraintreeDeleteCard() === true) { Mage::getSingleton('checkout/session')->setBraintreeDeleteCard($token); } } } // Advanced fraud protection data if (isset($result->transaction->riskData) && $this->getConfigData('fraudprotection')) { $decision = $result->transaction->riskData->decision; $helper = Mage::helper('braintree_payments'); if ($decision == self::RISK_DATA_NOT_EVALUATED || $decision == self::RISK_DATA_REVIEW) { $payment->setIsTransactionPending(true); $payment->setIsFraudDetected(true); } else { if ($decision == self::RISK_DATA_DECLINE) { Braintree_Transaction::void($result->transaction->id); throw new Mage_Payment_Model_Info_Exception($helper->__('Try another card')); } } $system = $this->getConfigData('kount_id') ? 'Kount' : $helper->__('Braintree Advanced Fraud Protection Tool'); $id = ''; if ($result->transaction->riskData->id) { $id = $helper->__(', ID is "%s"', $result->transaction->riskData->id); } $comment = $helper->__('Transaction was evaluated with %s: decision is "%s"', $system, $decision) . $id; $payment->getOrder()->addStatusHistoryComment($comment); } }
//echo "<a href='?_act=settlement&id=".$result -> transaction -> _attributes['id']."'>Do the settlement</a>"; echo '<input type="hidden" name="_act" value="clone" />'; echo '</br>Transaction ID: <input type="text" name="id" value="' . $result->transaction->_attributes['id'] . '" />'; echo '</br><input type="submit" name="" value="Proceed to clone transaction " />'; echo "</form>"; echo "--------------------------------------------------------------------------------------------------------------------------"; exit; } else { if ($_POST['_act'] == 'settlement') { $result = Braintree_Transaction::submitForSettlement($_POST['id'], $_POST['amt']); echo "<div style='height:300px; overflow-y:scroll; background-color:#fff;'> <h3>API response</h3>"; echo json_encode($result, JSON_PRETTY_PRINT); echo "</div>"; } else { if ($_POST['_act'] == 'void') { $result = Braintree_Transaction::void($_POST['id']); print_r($result); } else { if ($_REQUEST['_act'] == 'refund') { echo "</br></br><form method='POST' action=''><h2>Proceed to refund</h2></br>"; echo '<input type="hidden" name="_act" value="refund_process" />'; echo '</br>Transaction ID: <input type="text" name="id" value="" />'; echo '</br>Amount: <input type="text" name="amt" value="" />'; echo '</br><input type="submit" name="" value="Proceed to Refund " />'; echo "</form>"; echo "--------------------------------------------------------------------------------------------------------------------------"; } else { if ($_REQUEST['_act'] == 'refund_process') { $result = Braintree_Transaction::refund((string) $_POST['id'], (string) $_POST['amt']); echo "<div style='height:300px; overflow-y:scroll; background-color:#fff;'> <h3>API response</h3>"; echo json_encode($result, JSON_PRETTY_PRINT);
/** * Voids transaction * * @param Varien_Object $payment * @return Braintree_Payments_Model_Paymentmethod */ public function void(Varien_Object $payment) { $transactionIds = array(); $invoice = Mage::registry('current_invoice'); $message = false; if ($invoice && $invoice->getId() && $invoice->getTransactionId()) { $transactionIds[] = Mage::helper('braintree_payments')->clearTransactionId($invoice->getTransactionId()); } else { $collection = Mage::getModel('sales/order_payment_transaction')->getCollection()->addFieldToSelect('txn_id')->addOrderIdFilter($payment->getOrder()->getId())->addTxnTypeFilter(array(Mage_Sales_Model_Order_Payment_Transaction::TYPE_AUTH, Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE)); $fetchedIds = $collection->getColumnValues('txn_id'); foreach ($fetchedIds as $transactionId) { $txnId = Mage::helper('braintree_payments')->clearTransactionId($transactionId); if (!in_array($txnId, $transactionIds)) { $transactionIds[] = $txnId; } } } foreach ($transactionIds as $transactionId) { $transaction = Braintree_Transaction::find($transactionId); if ($transaction->status !== Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT && $transaction->status !== Braintree_Transaction::AUTHORIZED) { $message = Mage::helper('braintree_payments')->__('Some transactions are already settled or voided and cannot be voided.'); throw new Mage_Core_Exception($message, self::VOID_ALREADY_SETTLED_EXCEPTION); } if ($transaction->status === Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { $message = Mage::helper('braintree_payments')->__('Voided capture.'); } } $errors = ''; foreach ($transactionIds as $transactionId) { $this->_debug('void-' . $transactionId); $result = Braintree_Transaction::void($transactionId); $this->_debug($result); if (!$result->success) { $errors .= ' ' . Mage::helper('braintree_payments/error')->parseBraintreeError($result); } else { if ($message) { $payment->setMessage($message); } } } if ($errors) { Mage::throwException(Mage::helper('braintree_payments')->__('There was an error voiding the transaction.') . $errors); } else { $match = true; foreach ($transactionIds as $transactionId) { $collection = Mage::getModel('sales/order_payment_transaction')->getCollection()->addFieldToFilter('parent_txn_id', array('eq' => $transactionId))->addFieldToFilter('txn_type', Mage_Sales_Model_Order_Payment_Transaction::TYPE_VOID); if ($collection->getSize() < 1) { $match = false; } } if ($match) { $payment->setIsTransactionClosed(1); } } return $this; }
/** * Updates an existing record via the API * * @param object $model * @param array $fields * @param array $values * @return bool */ public function update(Model $model, $fields = null, $values = null, $conditions = null) { $to_save = $this->_createSaveArray($fields, $values); if (!empty($to_save['id'])) { $model->id = $to_save['id']; unset($to_save['id']); } if (empty($model->id)) { false; } $entity = $this->_getModelEntity($model); try { switch ($entity) { case 'Customer': $result = Braintree_Customer::update($model->id, $to_save); break; case 'Transaction': $transaction = $this->read($model, array('conditions' => array($model->alias . '.' . $model->primaryKey => $model->id))); if (empty($transaction)) { return false; } $exploded = explode('|', $model->id); $braintree_transaction_id = isset($exploded[1]) ? $exploded[1] : $model->id; if (!empty($to_save['status']) && $to_save['status'] == 'voided') { if ($transaction[0][$model->alias]['status'] != 'authorized' && $transaction[0][$model->alias]['status'] != 'submitted_for_settlement') { $this->showError(__('A transaction can only be VOIDED when the status is AUTHORIZED or SUBMITTED FOR SETTLEMENT.', true)); return false; } $result = Braintree_Transaction::void($braintree_transaction_id); if (!$result->success) { $this->showError($result->message); return false; } } elseif (!empty($to_save['status']) && $to_save['status'] == 'submitted_for_settlement') { if ($transaction[0][$model->alias]['status'] != 'authorized') { $this->showError(__('A transaction can only be SUBMITTED FOR SETTLEMENT when the status is AUTHORIZED.', true)); return false; } if (!empty($to_save['amount'])) { $result = Braintree_Transaction::submitForSettlement($braintree_transaction_id, $to_save['amount']); } else { $result = Braintree_Transaction::submitForSettlement($braintree_transaction_id); } if (!$result->success) { $this->showError($result->message); return false; } } else { $this->showError(__('The only update that can be made to a transaction is a VOID.', true)); return false; } break; case 'CreditCard': return false; break; case 'Address': return false; break; default: $result = false; break; } } catch (Exception $e) { $this->showError(print_r($e, true)); return false; } $success = $result->success; if (!$success) { return false; } return $success; }
/** * Refund specified amount for payment * * @param \Varien_Object $payment * @param float $amount * * @return $this * @throws \Mage_Core_Exception */ public function refund(Varien_Object $payment, $amount) { try { // Attempt to load the invoice /* @var $invoice Mage_Sales_Model_Order_Invoice */ $invoice = $payment->getCreditmemo()->getInvoice(); if (!$invoice) { Mage::throwException('Unable to load invoice from credit memo.'); } // Init the environment $this->_getWrapper()->init($payment->getOrder()->getStoreId()); // Convert the refund amount $refundAmount = $this->_getWrapper()->getCaptureAmount($payment->getOrder(), $amount); // Retrieve the transaction ID $transactionId = $this->_getWrapper()->getCleanTransactionId($invoice->getTransactionId()); // Load the transaction from Braintree $transaction = Braintree_Transaction::find($transactionId); // If the transaction hasn't yet settled we can't do partial refunds if ($transaction->status === Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { // If we're doing a partial refund and it's not settled it's a no go if ($transaction->amount != $refundAmount) { Mage::throwException($this->_getHelper()->__('This transaction has not yet settled, please wait until the transaction has settled to process a partial refund.')); } } // Swap between refund and void $result = $transaction->status === Braintree_Transaction::SETTLED || $transaction->status == Braintree_Transaction::SETTLING || isset($transaction->paypal) && isset($transaction->paypal['paymentId']) && !empty($transaction->paypal['paymentId']) ? Braintree_Transaction::refund($transactionId, $refundAmount) : Braintree_Transaction::void($transactionId); // If it's a success close the transaction if ($result->success) { // Pass over the transaction ID $payment->getCreditmemo()->setRefundTransactionId($result->transaction->id); // Only close the transaction once the if ($transaction->amount == $refundAmount) { $payment->setIsTransactionClosed(1); // Mark the invoice as canceled if the invoice was completely refunded $invoice->setState(Mage_Sales_Model_Order_Invoice::STATE_CANCELED); } } else { if ($result->errors->deepSize() > 0) { Mage::throwException($this->_getWrapper()->parseErrors($result->errors->deepAll())); } else { Mage::throwException('An unknown error has occurred whilst trying to process the transaction'); } } } catch (Exception $e) { Mage::throwException($this->_getHelper()->__('An error occurred whilst trying to process the refund: ') . $e->getMessage()); } return $this; }
public function void(Varien_Object $payment) { try { $result = Braintree_Transaction::void($payment->getCcTransId()); if ($result->success) { $payment->setIsTransactionClosed(1); } else { Mage::throwException($result->message); } } catch (Exception $e) { Mage::throwException(sprintf('There was an error voiding the transaction. (%s)', $e->getMessage())); } return $this; }
/** * Void payment abstract method * * @param Varien_Object $payment * * @return Mage_Payment_Model_Abstract */ public function void(Varien_Object $payment) { try { // Init the environment $this->_getWrapper()->init($payment->getOrder()->getStoreId()); // Retrieve the transaction ID $transactionId = $this->_getWrapper()->getCleanTransactionId($payment->getLastTransId()); // Load the transaction from Braintree $transaction = Braintree_Transaction::find($transactionId); // If the transaction hasn't yet settled we can't do partial refunds if ($transaction->status !== Braintree_Transaction::AUTHORIZED || $transaction->status !== Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { Mage::throwException($this->_getHelper()->__('You can only void authorized/submitted for settlement payments, please setup a credit memo if you wish to refund this order.')); } // Swap between refund and void $result = Braintree_Transaction::void($transactionId); // If it's a success close the transaction if ($result->success) { $payment->setIsTransactionClosed(1); } else { if ($result->errors->deepSize() > 0) { Mage::throwException($this->_getWrapper()->parseErrors($result->errors->deepAll())); } else { Mage::throwException('Unknown'); } } } catch (Exception $e) { Mage::throwException($this->_getHelper()->__('An error occurred whilst trying to void the transaction: ') . $e->getMessage()); } return $this; }
public function voidTransaction() { if (Efiwebsetting::getData('checkOAuth') == 'yes') { IMBAuth::checkOAuth(); } $id_order = isset($_POST['id_order']) ? addslashes($_POST['id_order']) : ""; if (!$id_order) { Generic::errorMsg("ID Order not found!"); } $transaction_id = isset($_POST['transaction_id']) ? addslashes($_POST['transaction_id']) : ""; if (!$transaction_id) { Generic::errorMsg("Transaction ID not found"); } $json = array(); $result = Braintree_Transaction::void($transaction_id); if ($result->success) { $json = array(); $json['status_code'] = 1; $orderObj = new MasterOrderModel(); $orderObj->getByID($id_order); $orderObj->transaction_id = ""; $orderObj->nonce_cc = ""; $orderObj->load = 1; $orderObj->save(); $json['results'] = "Success!"; } else { Generic::errorMsg($result->errors); } echo json_encode($json); die; }
/** * @param $transactionId * * @return array */ public function voidTransaction($transactionId) { try { $transaction = \Braintree_Transaction::find($transactionId); } catch (\Exception $e) { return false; } if ($transaction) { if ($transaction->status == \Braintree_Transaction::AUTHORIZED || $transaction->status == \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { $result = \Braintree_Transaction::void($transactionId); } else { $result = \Braintree_Transaction::refund($transactionId); } if ($result->success == true) { return true; } } return false; }
public function refundReward($id) { /*if (!$this->request->is('post')) { throw new NotFoundException(__d('billing', 'Incorrect request type')); }*/ $this->loadModel('InvestSponsor'); $this->InvestSponsor->Behaviors->load('Containable'); $sponsor = $this->InvestSponsor->find('first', array('fields' => array('InvestSponsor.*', 'InvestReward.*', 'InvestProject.user_id'), 'contain' => array('InvestReward', 'InvestProject'), 'conditions' => array('InvestSponsor.id' => $id))); if (!empty($sponsor)) { throw new NotFoundException('Could not find funds transfer'); } if (!in_array($this->currUserID, array($sponsor['InvestSponsor']['user_id'], $sponsor['InvestProject']['user_id']))) { throw new NotFoundException('Could not find funds transfer'); } if (!empty($sponsor['InvestSponsor']['remote_transaction_id'])) { $transaction = Braintree_Transaction::find($sponsor['InvestSponsor']['remote_transaction_id']); if (!$transaction) { throw new NotFoundException('Could not find your funds transfer'); } if ($transaction->status == Braintree_Transaction::SETTLED || $transaction->status == Braintree_Transaction::SETTLING) { $result = Braintree_Transaction::refund($transaction->id); } else { $result = Braintree_Transaction::void($transaction->id); } //TODO: little buggy cancelation if ($result->success || $transaction->status) { $this->InvestSponsor->id = $id; $this->InvestSponsor->saveField('canceled', 1); } } $this->redirect(array('action' => 'view', $sponsor['InvestReward']['project_id'])); }