/** * 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(); 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 ($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->_debugChargeService($chargeService, $e); $payment->setStatus(self::STATUS_DECLINED); $this->throwUserError($e->getMessage(), $e->resultText, 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; }
/** * 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; }