コード例 #1
0
 /**
  * Store the generated customer ID if it's present in the session
  *
  * @param \Varien_Event_Observer $observer
  *
  * @return $this
  */
 public function completeCheckout(Varien_Event_Observer $observer)
 {
     // Do we have a customer ID within the session?
     if (Mage::getSingleton('checkout/session')->getBraintreeCustomerId() && Mage::getSingleton('checkout/type_onepage')->getCheckoutMethod() == Mage_Checkout_Model_Type_Onepage::METHOD_CUSTOMER) {
         // Get the customer
         $customer = Mage::getSingleton('customer/session')->getCustomer();
         // Save the Braintree customer ID
         $customer->setBraintreeCustomerId(Mage::getSingleton('checkout/session')->getBraintreeCustomerId())->save();
     }
     // Perform any cleaning up required
     Gene_Braintree_Model_Wrapper_Braintree::cleanUp();
     // Unset the ID from the session
     Mage::getSingleton('checkout/session')->unsetData('braintree_customer_id');
     return $this;
 }
コード例 #2
0
 /**
  * Process capturing of a payment
  *
  * @param Varien_Object $payment
  * @param float         $amount
  *
  * @return Mage_Payment_Model_Abstract|void
  */
 public function capture(Varien_Object $payment, $amount)
 {
     // Has the payment already been authorized?
     if ($payment->getCcTransId()) {
         // Convert the capture amount to the correct currency
         $captureAmount = $this->_getWrapper()->getCaptureAmount($payment->getOrder(), $amount);
         // Has the authorization already been settled? Partial invoicing
         if ($this->authorizationUsed($payment)) {
             // Set the token as false
             $token = false;
             // Was the original payment created with a token?
             if ($additionalInfoToken = $payment->getAdditionalInformation('token')) {
                 try {
                     // Init the environment
                     $this->_getWrapper()->init($payment->getOrder()->getStoreId());
                     // Attempt to find the token
                     Braintree_PaymentMethod::find($additionalInfoToken);
                     // Set the token if a success
                     $token = $additionalInfoToken;
                 } catch (Exception $e) {
                     $token = false;
                 }
             }
             // If we managed to find a token use that for the capture
             if ($token) {
                 // Stop processing the rest of the method
                 // We pass $amount instead of $captureAmount as the authorize function contains the conversion
                 $this->_authorize($payment, $amount, true, $token);
                 return $this;
             } else {
                 // Attempt to clone the transaction
                 $result = $this->_getWrapper()->init($payment->getOrder()->getStoreId())->cloneTransaction($payment->getLastTransId(), $captureAmount);
             }
         } else {
             // Init the environment
             $result = $this->_getWrapper()->init($payment->getOrder()->getStoreId())->submitForSettlement($payment->getCcTransId(), $captureAmount);
             // Log the result
             Gene_Braintree_Model_Debug::log(array('capture:submitForSettlement' => $result));
         }
         if ($result->success) {
             $this->_processSuccessResult($payment, $result, $amount);
         } else {
             if ($result->errors->deepSize() > 0) {
                 // Clean up
                 Gene_Braintree_Model_Wrapper_Braintree::cleanUp();
                 Mage::throwException($this->_getWrapper()->parseErrors($result->errors->deepAll()));
             } else {
                 // Clean up
                 Gene_Braintree_Model_Wrapper_Braintree::cleanUp();
                 Mage::throwException($result->transaction->processorSettlementResponseCode . ': ' . $result->transaction->processorSettlementResponseText);
             }
         }
     } else {
         // Otherwise we need to do an auth & capture at once
         $this->_authorize($payment, $amount, true);
     }
     return $this;
 }