/** * Transaction Settlement * * @param $trans_id * @return object */ public function transSettlement($trans_id) { $transaction = Braintree_Transaction::find($trans_id); if ($transaction->status == Braintree_Transaction::SETTLED) { $result = array('status' => $transaction->status, 'is_settlement' => true); return $result; } else { $result = array('status' => $transaction->status, 'is_settlement' => false); return $result; } }
/** * Transaction settlement method * for cronjob * @param $trans_id * @return object */ public function transactionSettlement($trans_id) { $transaction = Braintree_Transaction::find($trans_id); //return $transaction; if ($transaction->status == Braintree_Transaction::SETTLED) { $result = array('is_settlement' => true); } else { $result = array('is_settlement' => false); } $result['recurring'] = $transaction->recurring; $result['status'] = $transaction->status; $result['package_id'] = $transaction->planId; $result['subscription_id'] = $transaction->subscriptionId; return $result; }
/** * @param string $id * @return \Braintree_Transaction */ public function find($id) { return \Braintree_Transaction::find($id); }
function testIncludeProcessorSettlementResponseForSettlementPendingTransaction() { $result = Braintree_Transaction::sale(array("paymentMethodNonce" => Braintree_Test_Nonces::$paypalFuturePayment, "amount" => "100", "options" => array("submitForSettlement" => true))); $this->assertTrue($result->success); $transaction = $result->transaction; Braintree_TestHelper::settlementPending($transaction->id); $inline_transaction = Braintree_Transaction::find($transaction->id); $this->assertEquals($inline_transaction->status, Braintree_Transaction::SETTLEMENT_PENDING); $this->assertEquals($inline_transaction->processorSettlementResponseCode, "4002"); $this->assertEquals($inline_transaction->processorSettlementResponseText, "Settlement Pending"); }
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); echo "</div>"; exit; } else { if ($_POST['_act'] == 'find') { $result = Braintree_Transaction::find($_POST['id']); print_r($result); } else { if ($_POST['_act'] == 'clone') { $result = Braintree_Transaction::cloneTransaction($_POST['id'], array('amount' => '10.00', 'options' => array('submitForSettlement' => true))); print_r($result); } else { if ($_POST['_act'] == 'createCustomer') { if (isset($_POST['data'])) { $postData = explode("&", urldecode($_POST['data'])); //[SETTING UP POST DATA TO SUPPORT AJAX] foreach ($postData as $key => $value) { $postValue = explode("=", $value); $_POST[$postValue[0]] = $postValue[1]; } unset($_POST['data']);
/** * 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; }
function test_rangeNode_settledAt() { $transaction = Braintree_Transaction::saleNoValidate(array('amount' => '1000.00', 'creditCard' => array('number' => '4111111111111111', 'expirationDate' => '05/12'), 'options' => array('submitForSettlement' => true))); Braintree_Http::put('/transactions/' . $transaction->id . '/settle'); $transaction = Braintree_Transaction::find($transaction->id); $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::settledAt()->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::settledAt()->between($ten_min_ago, $ten_min_from_now))); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($transaction->id, $collection->firstItem()->id); }
public function refund(Varien_Object $payment, $amount) { try { $transaction = Braintree_Transaction::find($payment->getCcTransId()); if ($transaction->amount != $amount && $transaction->status === Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { Mage::throwException('This refund is for a partial amount but the Transaction has not settled. Please wait 24 hours before trying to issue a partial refund.'); } $result = $transaction->status === Braintree_Transaction::SETTLED ? Braintree_Transaction::refund($payment->getCcTransId(), $amount) : 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 refunding the transaction. (%s)', $e->getMessage())); } return $this; }
/** * Reads from the API * * @param object $model * @param array $queryData * @return array */ public function read(Model $model, $queryData = array(), $recursive = NULL) { $queryData = array_merge(array('conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => 1, 'offset' => null, 'order' => array(0 => null), 'page' => 1, 'group' => null, 'callbacks' => 1, 'contain' => false, 'recursive' => -1), $queryData); extract($queryData); if (!empty($fields) && is_string($fields) && $fields == 'count') { $is_count = true; } else { $is_count = false; } if (!$this->_readErrors($model, $queryData, array('is_count' => $is_count))) { return false; } if (!empty($conditions[$model->alias . '.' . $model->primaryKey]) && ($limit == 1 || empty($limit) && $is_count)) { $entity = $this->_getModelEntity($model); try { switch ($entity) { case 'Customer': $customer = Braintree_Customer::find($conditions[$model->alias . '.' . $model->primaryKey]); $result = array(0 => array($model->alias => array('id' => $customer->id, 'first_name' => $customer->firstName, 'last_name' => $customer->lastName, 'company' => $customer->company, 'email' => $customer->email, 'phone' => $customer->phone, 'fax' => $customer->fax, 'website' => $customer->website, 'created' => $customer->createdAt->format('Y-m-d H:i:s'), 'modified' => $customer->updatedAt->format('Y-m-d H:i:s')))); break; case 'Transaction': $exploded = explode('|', $conditions[$model->alias . '.' . $model->primaryKey]); $braintree_transaction_id = isset($exploded[1]) ? $exploded[1] : $conditions[$model->alias . '.' . $model->primaryKey]; $transaction = Braintree_Transaction::find($braintree_transaction_id); $result = array(0 => array($model->alias => array('id' => $transaction->customer['id'] . '|' . $transaction->id, 'customer_id' => $transaction->customer['id'], 'payment_method_token' => $transaction->creditCard['token'], 'type' => $transaction->type, 'amount' => $transaction->amount, 'status' => $transaction->status, 'created' => $transaction->createdAt->format('Y-m-d H:i:s'), 'modified' => $transaction->updatedAt->format('Y-m-d H:i:s')))); $result[0][$model->alias . 'Status'] = array(); $count = 0; foreach ($transaction->statusHistory as $status) { $result[0][$model->alias . 'Status'][$count] = array('status' => $status->status, 'amount' => $status->amount, 'user' => $status->user, 'transaction_source' => $status->transactionSource, 'created' => $status->timestamp->format('Y-m-d H:i:s')); $count++; } break; case 'CreditCard': $credit_card = Braintree_CreditCard::find($conditions[$model->alias . '.' . $model->primaryKey]); $result = array(0 => array($model->alias => array('token' => $credit_card->token, 'customer_id' => $credit_card->customerId, 'cardholder_name' => $credit_card->cardholderName, 'card_type' => $credit_card->cardType, 'masked_number' => $credit_card->maskedNumber, 'expiration_date' => date('Y-m', strtotime($credit_card->expirationDate)) . '-01', 'is_default' => $credit_card->default, 'created' => $credit_card->createdAt->format('Y-m-d H:i:s'), 'modified' => $credit_card->updatedAt->format('Y-m-d H:i:s')))); break; case 'Address': $exploded = explode('|', $conditions[$model->alias . '.' . $model->primaryKey]); if (count($exploded) != 2) { return false; } list($customer_id, $address_id) = $exploded; $address = Braintree_Address::find($customer_id, $address_id); $result = array(0 => array($model->alias => array('id' => $address->customerId . '|' . $address->id, 'first_name' => $address->firstName, 'last_name' => $address->lastName, 'company' => $address->company, 'street_address' => $address->streetAddress, 'extended_address' => $address->extendedAddress, 'locality' => $address->locality, 'region' => $address->region, 'postal_code' => $address->postalCode, 'country_code_alpha_2' => $address->countryCodeAlpha2, 'country_code_alpha_3' => $address->countryCodeAlpha3, 'country_code_numeric' => $address->countryCodeNumeric, 'country_name' => $address->countryName, 'created' => $address->createdAt->format('Y-m-d H:i:s'), 'modified' => $address->updatedAt->format('Y-m-d H:i:s')))); break; default: $result = false; break; } } catch (Exception $e) { $result = false; } if ($is_count) { return array(0 => array(0 => array('count' => $result ? 1 : 0))); } return $result; } if (empty($conditions)) { try { $all_customers = Braintree_Customer::all(); } catch (Exception $e) { $this->showError(print_r($e, true)); return array(); } $return = array(); $count = 0; foreach ($all_customers->_ids as $id) { $return[$count][$model->alias]['id'] = $id; $count++; } return $return; } }
/** * wrapper for getting subscriptions from braintree * @param string - id of the transaction * @param object - user * * @return object - the searched transaction */ private static function getTransaction($transactionId, $user) { self::setBraintreeCredentials($user); $transaction = Braintree_Transaction::find($transactionId); return $transaction; }
/** * 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; }
/** * @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; }
function braintree_refund() { $result = Braintree_Transaction::refund('fbysy6'); $result->success; # true print_r($result); exit; $refund = $result->transaction; $refund->type; # 'credit' $refund->refundedTransactionId; # original transaction ID Braintree_Transaction::find($transaction->id)->refundId; # ID of refund associated to a transaction, if any /* $result = Braintree_Customer::create(array( 'creditCard' => array( 'number' => '4111111111111111' ) )); foreach($result->errors->deepAll() AS $error) { print_r($error->code . ": " . $error->message . "\n"); } */ }
function testMultipleRefundsWithPartialAmounts() { $transaction = $this->createTransactionToRefund(); $transaction1 = Braintree_Transaction::refund($transaction->id, '50.00')->transaction; $this->assertEquals(Braintree_Transaction::CREDIT, $transaction1->type); $this->assertEquals("50.00", $transaction1->amount); $transaction2 = Braintree_Transaction::refund($transaction->id, '50.00')->transaction; $this->assertEquals(Braintree_Transaction::CREDIT, $transaction2->type); $this->assertEquals("50.00", $transaction2->amount); $transaction = Braintree_Transaction::find($transaction->id); $expectedRefundIds = array($transaction1->id, $transaction2->id); $refundIds = $transaction->refundIds; sort($expectedRefundIds); sort($refundIds); $this->assertEquals($expectedRefundIds, $refundIds); }
public function get_transaction($id) { return Braintree_Transaction::find($id); }
public function getTransactionById($transaction_id) { try { $transaction = Braintree_Transaction::find($transaction_id); /* echo "<pre>"; print_r( $transaction); echo "</pre>"; */ return array('transaction_id' => $transaction->id, 'amount' => $transaction->amount, 'currency_code' => $transaction->currencyIsoCode, 'type' => $transaction->type, 'escrow_status' => $transaction->escrowStatus, 'service_fee' => $transaction->serviceFeeAmount); } catch (Exception $e) { return array('success' => 0, 'message' => 'Customer with ' . $transaction_id . " is not found"); } /* eg:Array ( [success] => 1 [customer_id] => 68012283 ) */ }
function testErrorsWhenFindWithWhitespaceString() { $this->setExpectedException('InvalidArgumentException'); Braintree_Transaction::find('\\t'); }
/** * 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; }
/** * Find a transaction within Braintree * * @param $transactionId * * @return object */ public function findTransaction($transactionId) { return Braintree_Transaction::find($transactionId); }
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'])); }