/** * Action to allow users to delete payment methods * * @return Mage_Core_Controller_Varien_Action * * @throws Exception */ public function removeAction() { // Check we've recieved a payment ID $token = $this->getRequest()->getParam('id'); if (!$token) { $this->_getSession()->addError('Please select a saved payment entry to remove.'); return $this->_redirectReferer(); } // Grab a new instance of the wrapper $wrapper = Mage::getModel('gene_braintree/wrapper_braintree'); // Init the braintree wrapper $wrapper->init(); // Load the payment method from Braintree try { $paymentMethod = Braintree_PaymentMethod::find($token); } catch (Exception $e) { $this->_getSession()->addError('The requested payment method cannot be found.'); return $this->_redirectReferer(); } // Check that this is the users payment method, we have to use a custom method as Braintree don't return the PayPal customer ID if (!$wrapper->customerOwnsMethod($paymentMethod)) { $this->_getSession()->addError('You do not have permission to modify this payment method.'); return $this->_redirectReferer(); } // Remove the payment method Braintree_PaymentMethod::delete($token); // Inform the user of the great news $this->_getSession()->addSuccess('Saved payment has been successfully deleted.'); return $this->_redirectReferer(); }
/** * Check whether a payment method exists * * @param $token * * @return bool */ public function checkPaymentMethod($token) { try { // Attempt to load the temporary payment method $paymentMethod = Braintree_PaymentMethod::find($token); if (isset($paymentMethod->token) && $paymentMethod->token == $token) { return true; } } catch (Exception $e) { return false; } }
function testErrorsOnFindWithWhitespaceCharacterArgument() { $this->setExpectedException('InvalidArgumentException'); Braintree_PaymentMethod::find('\\t'); }
function testDelete_worksWithPayPalAccounts() { $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand()); $customer = Braintree_Customer::createNoValidate(); $nonce = Braintree_HttpClientApi::nonceForPayPalAccount(array('paypal_account' => array('consent_code' => 'PAYPAL_CONSENT_CODE', 'token' => $paymentMethodToken))); $paypalAccountResult = Braintree_PaymentMethod::create(array('customerId' => $customer->id, 'paymentMethodNonce' => $nonce)); $this->assertTrue($paypalAccountResult->success); Braintree_PaymentMethod::delete($paymentMethodToken); $this->setExpectedException('Braintree_Exception_NotFound'); Braintree_PaymentMethod::find($paymentMethodToken); }
function testCreate_withOnetimePayPalAndDoesNotVault() { $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand()); $nonce = Braintree_HttpClientApi::nonceForPayPalAccount(array('paypal_account' => array('access_token' => 'PAYPAL_ACCESS_TOKEN', 'token' => $paymentMethodToken))); $result = Braintree_Transaction::sale(array('amount' => Braintree_Test_TransactionAmounts::$authorize, 'paymentMethodNonce' => $nonce, 'options' => array('storeInVault' => true))); $this->assertTrue($result->success); $transaction = $result->transaction; $this->assertEquals('*****@*****.**', $transaction->paypalDetails->payerEmail); $this->assertNotNull($transaction->paypalDetails->imageUrl); $this->assertNotNull($transaction->paypalDetails->debugId); $this->setExpectedException('Braintree_Exception_NotFound'); Braintree_PaymentMethod::find($paymentMethodToken); }
/** * Validate data * * @return Braintree_Payments_Model_Paymentmethod */ public function validate() { $info = $this->getInfoInstance(); if ($info instanceof Mage_Sales_Model_Order_Payment) { $billingCountry = $info->getOrder()->getBillingAddress()->getCountryId(); } else { $billingCountry = $info->getQuote()->getBillingAddress()->getCountryId(); } if (!$this->canUseForCountry($billingCountry)) { Mage::throwException(Mage::helper('braintree_payments')->__('Selected payment type is not allowed for billing country.')); } $ccType = false; if ($info->getCcType()) { $ccType = $info->getCcType(); } else { $token = $info->getAdditionalInformation('ccToken'); if ($token) { $ccType = false; $useCache = $this->getConfigData('usecache'); $cachedValues = $useCache ? Mage::app()->loadCache(self::CACHE_KEY_CREDIT_CARDS) : false; if ($cachedValues) { try { $data = unserialize($cachedValues); } catch (Exception $e) { $data = false; } if ($data && array_key_exists($token, $data)) { $ccType = $data[$token]; } } if (!$ccType) { try { $creditCard = Braintree_PaymentMethod::find($token); $this->_debug($token); $this->_debug($creditCard); $ccType = Mage::helper('braintree_payments')->getCcTypeCodeByName($creditCard->cardType); if ($cachedValues && $data) { $data = array_merge($data, array($token => $ccType)); } else { $data = array($token => $ccType); } if ($useCache) { Mage::app()->saveCache(serialize($data), self::CACHE_KEY_CREDIT_CARDS); } } catch (Exception $e) { Mage::logException($e); } } } } if ($ccType) { $error = $this->_canUseCcTypeForCountry($billingCountry, $ccType); if ($error) { Mage::throwException($error); } } return $this; }
} $result = Braintree_Transaction::sale(array('amount' => $_POST['amt'], 'paymentMethodToken' => $_POST['token'])); 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'] == 'updatePaymentMethod') { if (isset($_POST['billingAddressId']) & $_POST['billingAddressId'] != "") { $updateResult = Braintree_PaymentMethod::update($_POST['token'], array('billingAddressId' => $_POST['billingAddressId'])); } else { $updateResult = Braintree_PaymentMethod::update($_POST['token'], array('billingAddress' => array('streetAddress' => $_POST['streetAddress'], 'options' => array('updateExisting' => $_POST['updateExisting'])))); } print_r($updateResult); } else { if ($_POST['_act'] == 'findPaymentMethod') { $paymentMethod = Braintree_PaymentMethod::find($_POST['token']); print_r($paymentMethod); } else { if ($_POST['_act'] == 'deletePaymentMethod') { $search = (string) $_POST['token']; $file = './data/token.txt'; $contents = file_get_contents($file); echo $contents = str_replace($_POST['token'], trim((string) $_POST['token'] . "_DELETED\r\n"), $contents); file_put_contents($file, $contents); $paymentMethod = Braintree_PaymentMethod::delete($_POST['token']); print_r($paymentMethod); } else { if ($_POST['_act'] == 'createSubMerchant') { $result = Braintree_MerchantAccount::create(array('individual' => array('firstName' => $_POST['firstName'], 'lastName' => $_POST['lastName'], 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'dateOfBirth' => $_POST['dateOfBirth'], 'ssn' => $_POST['ssn'], 'address' => array('streetAddress' => $_POST['streetAddress'], 'locality' => $_POST['locality'], 'region' => $_POST['region'], 'postalCode' => $_POST['postalCode'])), 'business' => array('legalName' => $_POST['legalName'], 'dbaName' => $_POST['dbaName'], 'taxId' => $_POST['taxId'], 'address' => array('streetAddress' => $_POST['streetAddress2'], 'locality' => $_POST['locality2'], 'region' => $_POST['region2'], 'postalCode' => $_POST['postalCode2'])), 'funding' => array('descriptor' => $_POST['descriptor'], 'destination' => Braintree_MerchantAccount::FUNDING_DESTINATION_BANK, 'email' => $_POST['email'], 'mobilePhone' => $_POST['mobilePhone'], 'accountNumber' => $_POST['accountNumber'], 'routingNumber' => $_POST['routingNumber']), 'tosAccepted' => true, 'masterMerchantAccountId' => "qzmnkckz54r94r4f", 'id' => "mc_" . time())); $file = './data/subMerchant.txt'; //[ADD Customer ID to text file]
/** * Returns stored payment method by token * * @return Braintree_PaymentMethod | null */ public function storedPaymentMethod($token) { try { $ret = Braintree_PaymentMethod::find($token); $this->_debug($token); $this->_debug($ret); return $ret; } catch (Braintree_Exception $e) { Mage::logException($e); } }
/** * 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; }
public function a() { $result = Braintree_PaymentMethod::create(['customerId' => '28', 'paymentMethodNonce' => \Braintree\Test\Nonces::$transactable]); pr($result); die; $updateResult = Braintree_PaymentMethod::update('5ngfy6', ['paymentMethodNonce' => \Braintree\Test\Nonces::$transactable, 'options' => ['makeDefault' => true, 'verificationAmount' => '100000']]); pr($updateResult); $paymentMethod = Braintree_PaymentMethod::find('5ngfy6'); pr($paymentMethod); die; // $result = Braintree_PaymentMethod::create(['customerId' => $id_user, 'paymentMethodNonce' => 'fake-valid-visa-nonce', 'options' => ['verificationMerchantAccountId' => 'm5ph2g77wgfzdyy2', 'verificationAmount' => '100000']]); pr($result); die; $result = Braintree_Transaction::sale(['amount' => '100.00', 'orderId' => 'order id', 'merchantAccountId' => 'm5ph2g77wgfzdyy2', 'paymentMethodNonce' => nonceFromTheClient, 'customer' => ['firstName' => 'Drew', 'lastName' => 'Smith', 'company' => 'Braintree', 'phone' => '312-555-1234', 'fax' => '312-555-1235', 'website' => 'http://www.example.com', 'email' => '*****@*****.**'], 'billing' => ['firstName' => 'Paul', 'lastName' => 'Smith', 'company' => 'Braintree', 'streetAddress' => '1 E Main St', 'extendedAddress' => 'Suite 403', 'locality' => 'Chicago', 'region' => 'IL', 'postalCode' => '60622', 'countryCodeAlpha2' => 'US'], 'shipping' => ['firstName' => 'Jen', 'lastName' => 'Smith', 'company' => 'Braintree', 'streetAddress' => '1 E 1st St', 'extendedAddress' => 'Suite 403', 'locality' => 'Bartlett', 'region' => 'IL', 'postalCode' => '60103', 'countryCodeAlpha2' => 'US'], 'options' => ['submitForSettlement' => true]]); pr($result); die; $result = Braintree_PaymentMethod::create(['customerId' => $id_user, 'paymentMethodNonce' => nonceFromTheClient, 'options' => ['makeDefault' => true]]); // $result->paymentMethod->isDefault(); pr($result); die; $collection = Braintree_CreditCardVerification::search([Braintree_CreditCardVerificationSearch::customerId()->is($id_user)]); pr($collection); }