/** For authorization **/ public function authorize(Varien_Object $payment, $amount) { Mage::log('authorize method', null, 'cmss.log'); #getOrderid $order = $payment->getOrder(); $billing = $order->getBillingAddress(); #creating request object for stripe $request = array('amount' => $amount * 100, 'currency' => strtolower($order->getBaseCurrencyCode()), 'card' => array('number' => $payment->getCcNumber(), 'exp_month' => sprintf('%02d', $payment->getCcExpMonth()), 'exp_year' => $payment->getCcExpYear(), 'cvc' => $payment->getCcCid(), 'name' => $billing->getName(), 'address_line1' => $billing->getStreet(1), 'address_line2' => $billing->getStreet(2), 'address_zip' => $billing->getPostcode(), 'address_state' => $billing->getRegion(), 'address_country' => $billing->getCountry()), 'description' => sprintf('#%s, %s', $order->getIncrementId(), $order->getCustomerEmail())); #request to stripe to charge the card try { //Stripe::setApiKey('sk_test_4SaT1bB6uEuFF5POEUnsSZHJ'); $response = Stripe_Charge::create($request); Mage::log('authorize method' . print_r($response, true), null, 'cmss.log'); //$fmessage=$response->failure_code; if ($response->paid) { $result = 1; } else { $result = false; } } catch (Exception $e) { $this->debugData($e->getMessage()); Mage::throwException(Mage::helper('paygate')->__($e->getMessage())); } if ($result === false) { $errorCode = 'Invalid Data'; $errorMsg = $this->_getHelper()->__('Error Processing the request'); } else { Mage::log($result, null, $this->getCode() . '.log'); //process result here to check status etc as per payment gateway. // if invalid status throw exception if ($result == 1) { //Mage::log('sdsd'.$response->id, null, 'cmss.log'); $payment->setTransactionId($response->id); $payment->setIsTransactionClosed(1); $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array('key1' => 'value1', 'key2' => 'value2')); // } else { Mage::throwException($errorMsg); } // Add the comment and save the order } if ($errorMsg) { Mage::throwException($errorMsg); } $this->capture($payment, $order->getGrandTotal()); // Create invoice $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice(array()); $invoice->register(); $invoice->setCanVoidFlag(true); $invoice->getOrder()->setIsInProcess(true); $invoice->setState(2); if (Mage::helper('sales')->canSendNewInvoiceEmail($order->getStoreId())) { $invoice->setEmailSent(true); $invoice->sendEmail(); } $invoice->save(); $order->setTotalPaid($order->getBaseGrandTotal()); $order->addStatusHistoryComment('Captured online amount of R$' . $order->getBaseGrandTotal(), false); $order->save(); //Mage::log($response->id, null, 'mss.log'); return $this; }
/** * 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); } }
/** * Void previously *authorized* payment. * * @param Varien_Object $payment * @return Mage_Payment_Model_Abstract */ public function void(Varien_Object $payment) { list($transactionId, ) = explode('-', $payment->getParentTransactionId()); $parameters = array('transactionId' => $transactionId); $result = $this->_apiCall('CancelTransaction', $parameters); $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array_merge($result, $parameters)); return $this; }
/** * The transaction is captured with the specified amount. * * @param Varien_Object $payment The payment object containing all the payment specific data for the order. * @param unknown_type $amount The amount to capture. This may be less than the amount of the order but not more. * @return Customweb_SaferpayCw_Model_Method */ public function capture(Varien_Object $payment, $amount) { $invoice = Mage::registry('current_invoice'); try { $transaction = $this->getHelper()->loadTransactionByPayment($payment->getId()); $order = $payment->getOrder(); Customweb_SaferpayCw_Model_ConfigurationAdapter::setStore($order); $adapter = $this->getHelper()->createContainer()->getBean('Customweb_Payment_BackendOperation_Adapter_Service_ICapture'); if ($transaction->getTransactionObject()->isCapturePossible()) { if ($transaction->getTransactionObject()->isPartialCapturePossible()) { if ($invoice instanceof Mage_Sales_Model_Order_Invoice) { $items = $this->getInvoiceItems($invoice); } else { $items = Customweb_Util_Invoice::getItemsByReductionAmount($transaction->getTransactionObject()->getTransactionContext()->getOrderContext()->getInvoiceItems(), $amount, $transaction->getTransactionObject()->getCurrencyCode()); } $adapter->partialCapture($transaction->getTransactionObject(), $items, true); } else { $adapter->capture($transaction->getTransactionObject()); } $transaction->save(); } if ($transaction->getTransactionObject()->isCaptured()) { $payment->setIsTransactionClosed(false); $captures = $transaction->getTransactionObject()->getCaptures(); $capture = end($captures); $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array('TRANSACTIONID' => $transaction->getTransactionObject()->getPaymentId(), 'CAPTUREID' => $capture->getCaptureId(), 'AMOUNT' => $capture->getAmount(), 'AMT' => $capture->getAmount(), 'STATUS' => $capture->getStatus())); $this->_generateTransactionId($payment, Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE); return $this; } else { $this->getHelper()->log("Capture failed. Transaction error messages : " . print_r($transaction->getTransactionObject()->getErrorMessages(), true)); $messages = $transaction->getTransactionObject()->getErrorMessages(); Mage::throwException($this->getHelper()->__('The invoice could not be captured and processed. Reason: ') . end($messages)); } } catch (Exception $e) { $this->getHelper()->log("Exception in Method::capture() : " . $e->getMessage()); Mage::getSingleton('core/session')->addError($e->getMessage()); throw $e; } }
/** * Authorize or Capture payment * * @param Varien_Object|Mage_Sales_Model_Order_Payment $payment * @param float $amount * @param bool $capture * @return $this */ private function _authorize(Varien_Object $payment, $amount, $capture) { $order = $payment->getOrder(); /* @var $order Mage_Sales_Model_Order */ $multiToken = false; $cardData = null; $additionalData = new Varien_Object($payment->getAdditionalData() ? unserialize($payment->getAdditionalData()) : null); $secureToken = $additionalData->getSecuresubmitToken() ? $additionalData->getSecuresubmitToken() : null; $saveCreditCard = !!(bool) $additionalData->getCcSaveFuture(); $customerId = $additionalData->getCustomerId(); $giftService = $this->_getGiftService(); $giftCardNumber = $additionalData->getGiftcardNumber(); if ($giftCardNumber) { // 1. check balance $giftcard = new HpsGiftCard(); $giftcard->number = $giftCardNumber; $giftResponse = $giftService->balance($giftcard); // 2. is balance > amount? if ($giftResponse->balanceAmount > $amount) { // 2.yes. process full to gift try { if (strpos($this->getConfigData('secretapikey'), '_cert_') !== false) { $giftresp = $giftService->sale($giftcard, 10.0); } else { $giftresp = $giftService->sale($giftcard, $amount); } $order->addStatusHistoryComment('Used Heartland Gift Card ' . $giftCardNumber . ' for amount $' . $amount . '. [full payment]'); $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array('gift_card_number' => $giftCardNumber, 'gift_card_transaction' => $giftresp->transactionId, 'gift_card_amount_charged' => $amount)); $payment->setStatus(self::STATUS_APPROVED); $payment->setAmount($amount); $payment->setLastTransId($response->transactionId); $payment->setTransactionId($response->transactionId); $payment->setIsTransactionClosed(0); return $this; } catch (Exception $e) { Mage::logException($e); $payment->setStatus(self::STATUS_ERROR); $this->throwUserError($e->getMessage(), null, true); } } else { // 2.no. process full gift card amt and card process remainder $giftresp = $giftService->sale($giftcard, $giftResponse->balanceAmount); $order->addStatusHistoryComment('Used Heartland Gift Card ' . $giftCardNumber . ' for amount $' . $giftResponse->balanceAmount . '. [partial payment]')->save(); $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array('gift_card_number' => $giftCardNumber, 'gift_card_transaction' => $giftresp->transactionId, 'gift_card_amount_charged' => $giftResponse->balanceAmount)); $payment->setAmount($giftResponse->balanceAmount)->save(); $amount = $amount - $giftResponse->balanceAmount; // remainder // 3. TODO: if the card payment fails later, refund the gift transaction } } if ($saveCreditCard) { $multiToken = true; $cardData = new HpsCreditCard(); $cardData->number = $payment->getCcLast4(); $cardData->expYear = $payment->getCcExpYear(); $cardData->expMonth = $payment->getCcExpMonth(); } $chargeService = $this->_getChargeService(); $cardHolder = $this->_getCardHolderData($order); $details = $this->_getTxnDetailsData($order); $cardOrToken = new HpsTokenData(); $cardOrToken->tokenValue = $secureToken; try { if ($capture) { if ($payment->getCcTransId()) { $response = $chargeService->capture($payment->getCcTransId(), $amount); } else { $response = $chargeService->charge($amount, strtolower($order->getBaseCurrencyCode()), $cardOrToken, $cardHolder, $multiToken, $details); } } else { $response = $chargeService->authorize($amount, strtolower($order->getBaseCurrencyCode()), $cardOrToken, $cardHolder, $multiToken, $details); } $this->_debugChargeService($chargeService); $payment->setStatus(self::STATUS_APPROVED); $payment->setAmount($amount); $payment->setLastTransId($response->transactionId); $payment->setCcTransId($response->transactionId); $payment->setTransactionId($response->transactionId); $payment->setIsTransactionClosed(0); if ($giftCardNumber) { $order->addStatusHistoryComment('Remaining amount to be charged to credit card ' . $this->_formatAmount($amount) . '. [partial payment]')->save(); } if ($multiToken) { $tokenData = $response->tokenData; /* @var $tokenData HpsTokenData */ if ($tokenData->responseCode == '0') { if ($customerId > 0) { Mage::helper('hps_securesubmit')->saveMultiToken($response->tokenData->tokenValue, $cardData, $response->cardType, $customerId); } else { Mage::helper('hps_securesubmit')->saveMultiToken($response->tokenData->tokenValue, $cardData, $response->cardType); } } else { Mage::log('Requested multi token has not been generated for the transaction # ' . $response->transactionId, Zend_Log::WARN); } } } catch (HpsCreditException $e) { Mage::logException($e); $this->getFraudSettings(); $this->_debugChargeService($chargeService, $e); // refund gift (if used) if ($giftCardNumber) { $order->addStatusHistoryComment('Reversed Heartland Gift Card ' . $giftCardNumber . ' for amount $' . $giftResponse->balanceAmount . '. [full reversal]')->save(); $giftResponse = $giftService->reverse($giftcard, $giftResponse->balanceAmount); } if ($this->_allow_fraud && $e->getCode() == HpsExceptionCodes::POSSIBLE_FRAUD_DETECTED) { // we can skip the card saving if it fails for possible fraud there will be no token. if ($this->_email_fraud && $this->_fraud_address != '') { // EMAIL THE PEOPLE $this->sendEmail($this->_fraud_address, $this->_fraud_address, 'Suspicious order (' . $order->getIncrementId() . ') allowed', 'Hello,<br><br>Heartland has determined that you should review order ' . $order->getRealOrderId() . ' for the amount of ' . $amount . '.'); } $payment->setStatus(self::STATUS_APPROVED); $payment->setAmount($amount); $payment->setIsTransactionClosed(0); } else { $payment->setStatus(self::STATUS_ERROR); if ($e->getCode() == HpsExceptionCodes::POSSIBLE_FRAUD_DETECTED) { $this->throwUserError($this->_fraud_text, null, true); } else { $this->throwUserError($e->getMessage(), null, true); } } } catch (HpsException $e) { $this->_debugChargeService($chargeService, $e); $payment->setStatus(self::STATUS_ERROR); $this->throwUserError($e->getMessage(), null, true); } catch (Exception $e) { $this->_debugChargeService($chargeService, $e); Mage::logException($e); $payment->setStatus(self::STATUS_ERROR); $this->throwUserError($e->getMessage()); } return $this; }
/** * Makes method specific manipulations after authorize/clone transaction success * * @param Varien_Object $payment $payment * @param array $result */ protected function _processMethodSpecificTransactionSuccess($payment, $result) { if (isset($result->transaction->paypal['token']) && $result->transaction->paypal['token']) { $token = $result->transaction->paypal['token']; $payment->setTransactionAdditionalInfo('token', $token); } }