/**
  * Capture payment
  *
  * @param Mage_Sales_Model_Order_Payment $payment
  * @param float $amount
  * @return Mage_Paypal_Model_Express
  */
 public function capture(Varien_Object $payment, $amount)
 {
     $authorizationTransaction = $payment->getAuthorizationTransaction();
     $authorizationPeriod = abs(intval($this->getConfigData('authorization_honor_period')));
     $maxAuthorizationNumber = abs(intval($this->getConfigData('child_authorization_number')));
     $order = $payment->getOrder();
     $isAuthorizationCreated = false;
     if ($payment->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
         $voided = false;
         if (!$authorizationTransaction->getIsClosed() && $this->_isTransactionExpired($authorizationTransaction, $authorizationPeriod)) {
             //Save payment state and configure payment object for voiding
             $isCaptureFinal = $payment->getShouldCloseParentTransaction();
             $captureTrxId = $payment->getTransactionId();
             $payment->setShouldCloseParentTransaction(false);
             $payment->setParentTransactionId($authorizationTransaction->getTxnId());
             $payment->unsTransactionId();
             $payment->setVoidOnlyAuthorization(true);
             $payment->void(new Varien_Object());
             //Revert payment state after voiding
             $payment->unsAuthorizationTransaction();
             $payment->unsTransactionId();
             $payment->setShouldCloseParentTransaction($isCaptureFinal);
             $voided = true;
         }
         if ($authorizationTransaction->getIsClosed() || $voided) {
             if ($payment->getAdditionalInformation($this->_authorizationCountKey) > $maxAuthorizationNumber - 1) {
                 Mage::throwException(Mage::helper('paypal')->__('The maximum number of child authorizations is reached.'));
             }
             $api = $this->_callDoAuthorize($amount, $payment, $authorizationTransaction->getParentTxnId());
             //Adding authorization transaction
             $this->_pro->importPaymentInfo($api, $payment);
             $payment->setTransactionId($api->getTransactionId());
             $payment->setParentTransactionId($authorizationTransaction->getParentTxnId());
             $payment->setIsTransactionClosed(false);
             $formatedPrice = $order->getBaseCurrency()->formatTxt($amount);
             if ($payment->getIsTransactionPending()) {
                 $message = Mage::helper('paypal')->__('Authorizing amount of %s is pending approval on gateway.', $formatedPrice);
             } else {
                 $message = Mage::helper('paypal')->__('Authorized amount of %s.', $formatedPrice);
             }
             $transaction = $payment->addTransaction(Mage_Sales_Model_Order_Payment_Transaction::TYPE_AUTH, null, true, $message);
             $payment->setParentTransactionId($api->getTransactionId());
             $isAuthorizationCreated = true;
         }
         //close order transaction if needed
         if ($payment->getShouldCloseParentTransaction()) {
             $orderTransaction = $payment->lookupTransaction(false, Mage_Sales_Model_Order_Payment_Transaction::TYPE_ORDER);
             if ($orderTransaction) {
                 $orderTransaction->setIsClosed(true);
                 $order->addRelatedObject($orderTransaction);
             }
         }
     }
     if (false === $this->_pro->capture($payment, $amount)) {
         $this->_placeOrder($payment, $amount);
     }
     if ($isAuthorizationCreated && isset($transaction)) {
         $transaction->setIsClosed(true);
     }
     return $this;
 }
Example #2
0
 public function createOrder(Varien_Object $payment, $amount, $authorize)
 {
     $store_id = Mage::app()->getStore()->getStoreId();
     $logger = Mage::helper('worldpay/logger');
     if ($payment->getOrder()) {
         $orderId = $payment->getOrder()->getIncrementId();
         $order = $payment->getOrder();
     } else {
         $quote = $payment->getQuote();
         $orderId = $quote->getReservedOrderId();
         $quote->save();
     }
     $session = Mage::getSingleton('core/session');
     $token = $session->getData('payment_token');
     $savedCard = $session->getData('saved_card');
     $logger->log('Begin create order');
     $session->setData('wp_3dsSuccess', false);
     $session->setData('wp_orderCode', false);
     $worldpay = $this->setupWorldpay();
     $checkout = Mage::getSingleton('checkout/session')->getQuote();
     $billing = $checkout->getBillingAddress();
     $order_description = Mage::getStoreConfig('payment/worldpay/description', $store_id);
     if (!$order_description) {
         $order_description = "Order";
     }
     $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
     $name = $billing->getName();
     $billing_address = array("address1" => $billing->getStreet(1), "address2" => $billing->getStreet(2), "address3" => $billing->getStreet(3), "postalCode" => $billing->getPostcode(), "city" => $billing->getCity(), "state" => "", "countryCode" => $billing->getCountry());
     try {
         $mode = Mage::getStoreConfig('payment/worldpay_mode', Mage::app()->getStore()->getStoreId());
         $settlementCurrency = Mage::getStoreConfig('payment/worldpay/settlementcurrency', Mage::app()->getStore()->getStoreId());
         $createOrderRequest = array('token' => $token, 'orderDescription' => $order_description, 'amount' => $amount * 100, 'currencyCode' => $currency_code, 'name' => $name, 'billingAddress' => $billing_address, 'customerOrderCode' => $orderId, 'settlementCurrency' => $settlementCurrency, 'successUrl' => Mage::getUrl('worldpay/apm/success', array('_secure' => true)), 'pendingUrl' => Mage::getUrl('worldpay/apm/pending', array('_secure' => true)), 'failureUrl' => Mage::getUrl('worldpay/apm/failure', array('_secure' => true)), 'cancelUrl' => Mage::getUrl('worldpay/apm/cancel', array('_secure' => true)));
         $logger->log('Order Request: ' . print_r($createOrderRequest, true));
         $response = $worldpay->createApmOrder($createOrderRequest);
         $logger->log('Order Response: ' . print_r($response, true));
         if ($response['paymentStatus'] === 'SUCCESS') {
             $this->setStore($payment->getOrder()->getStoreId());
             $logger->log('Order: ' . $response['orderCode'] . ' SUCCESS');
             $payment->setStatus(self::STATUS_APPROVED);
             $payment->setAmount($amount);
             $payment->setLastTransId($orderId);
             $payment->setTransactionId($response['orderCode']);
             $payment->setAdditionalInformation("worldpayOrderCode", $response['orderCode']);
             $payment->setShouldCloseParentTransaction(1)->setIsTransactionClosed(1)->registerCaptureNotification($amount);
         } else {
             if ($response['paymentStatus'] == 'PRE_AUTHORIZED') {
                 $logger->log('Order: ' . $response['orderCode'] . ' PRE_AUTHORIZED');
                 $payment->setAmount($amount);
                 $payment->setAdditionalInformation("worldpayOrderCode", $response['orderCode']);
                 $payment->setLastTransId($orderId);
                 $payment->setTransactionId($response['orderCode']);
                 $payment->setIsTransactionClosed(false);
                 $session->setData('wp_redirectURL', $response['redirectURL']);
                 $session->setData('wp_orderCode', $response['orderCode']);
             } else {
                 if (isset($response['paymentStatusReason'])) {
                     throw new Exception($response['paymentStatusReason']);
                 } else {
                     throw new Exception(print_r($response, true));
                 }
             }
         }
     } catch (Exception $e) {
         $payment->setStatus(self::STATUS_ERROR);
         $payment->setAmount($amount);
         $payment->setLastTransId($orderId);
         $logger->log($e->getMessage());
         Mage::throwException('Payment failed, please try again later ' . $e->getMessage());
     }
     return $this;
 }
 /**
  * Capturing method being executed via Payment Bridge
  *
  * @param Varien_Object $payment
  * @param float $amount
  * @return Enterprise_Pbridge_Model_Payment_Method_Payflow_Pro
  */
 public function capture(Varien_Object $payment, $amount)
 {
     $this->_setPbridgeAdditionalParams();
     $payment->setShouldCloseParentTransaction(!$this->_getCaptureAmount($amount));
     $payment->setFirstCaptureFlag(!$this->getInfoInstance()->hasAmountPaid());
     $response = $this->getPbridgeMethodInstance()->capture($payment, $amount);
     if (!$response) {
         $response = $this->getPbridgeMethodInstance()->authorize($payment, $amount);
     }
     $payment->addData((array) $response);
     $payment->setIsTransactionClosed(0);
     return $this;
 }
 public function capture(Varien_Object $payment, $amount)
 {
     $session = Mage::getSingleton('core/session');
     $logger = Mage::helper('worldpay/logger');
     if ($session->getData('wp_3dscompletionNeeded')) {
         $payment->setIsTransactionClosed(0);
         $payment->setSkipOrderProcessing(true);
         $payment->setStatus(self::STATUS_APPROVED);
         $payment->setAmount($amount);
         $payment->setShouldCloseParentTransaction(1);
         $payment->setAdditionalInformation("worldpayOrderCode", $session->getData('wp_orderCode'));
         $payment->setLastTransId($session->getData('wp_orderCode'));
         $payment->setTransactionId($session->getData('wp_orderCode'));
         $session->setData('wp_3dscompletionNeeded', false);
         return $this;
     }
     $worldpayOrderCode = $payment->getData('last_trans_id');
     if ($worldpayOrderCode) {
         $worldpay = $this->setupWorldpay();
         try {
             $authorizationTransaction = $payment->getAuthorizationTransaction();
             if ($authorizationTransaction) {
                 $payment->setAdditionalInformation("worldpayOrderCode", $authorizationTransaction->getTxnId());
                 $worldpayOrderCode = $authorizationTransaction->getTxnId();
             } else {
                 $worldpayOrderCode = $payment->getAdditionalInformation('worldpayOrderCode');
             }
             $worldpay->captureAuthorisedOrder($worldpayOrderCode, $amount * 100);
             $payment->setShouldCloseParentTransaction(1)->setIsTransactionClosed(1);
             $logger->log('Capture Order: ' . $session->getData('wp_orderCode') . ' success');
         } catch (Exception $e) {
             $logger->log('Capture Order: ' . $session->getData('wp_orderCode') . ' failed with ' . $e->getMessage());
             Mage::throwException('Payment failed, please try again later ' . $e->getMessage());
         }
     } else {
         $payment->setAdditionalInformation('payment_type', 'capture');
         return $this->createOrder($payment, $amount, false);
     }
 }
Example #5
0
 /**
  * Refund money
  *
  * @param   Varien_Object $payment
  * @param   float $amount
  * @return  Mage_Payment_Model_Abstract
  */
 public function refund(Varien_Object $payment, $amount)
 {
     //parent::refund($payment, $amount);
     $captureTxnId = $payment->getParentTransactionId();
     if ($captureTxnId) {
         $order = $payment->getOrder();
         $request = $this->_getApiRequest();
         $request->setData('transaction_id', $captureTxnId)->setData('amount', $amount)->setData('currency_code', $order->getBaseCurrencyCode())->setData('cc_number', $payment->getCcLast4());
         $canRefundMore = $order->canCreditmemo();
         $allRefunds = (double) $amount + (double) $order->getBaseTotalOnlineRefunded() + (double) $order->getBaseTotalOfflineRefunded();
         $isFullRefund = !$canRefundMore && 0.0001 > (double) $order->getBaseGrandTotal() - $allRefunds;
         $request->setData('is_full_refund', (int) $isFullRefund);
         // whether to close capture transaction
         $invoiceCanRefundMore = $payment->getCreditmemo()->getInvoice()->canRefund();
         $payment->setShouldCloseParentTransaction($invoiceCanRefundMore ? 0 : 1);
         $payment->setIsTransactionClosed(1);
         $api = $this->_getApi()->doRefund($request);
         $this->_importResultToPayment($payment, $api->getResponse());
         return $api->getResponse();
     } else {
         Mage::throwException(Mage::helper('enterprise_pbridge')->__('Impossible to issue a refund transaction, because capture transaction does not exist.'));
     }
 }
Example #6
0
 public function createOrder(Varien_Object $payment, $amount, $authorize)
 {
     $store_id = Mage::app()->getStore()->getStoreId();
     if ($payment->getOrder()) {
         $orderId = $payment->getOrder()->getIncrementId();
     } else {
         $orderId = $payment->getQuote()->getIncrementId();
         // $order = $payment->getOrder())
     }
     $logger = Mage::helper('worldpay/logger');
     $session = Mage::getSingleton('core/session');
     $token = $session->getData('payment_token');
     $savedCard = $session->getData('saved_card');
     $logger->log('Begin create order');
     $session->setData('wp_3dsSuccess', false);
     $session->setData('wp_orderCode', false);
     $worldpay = $this->setupWorldpay();
     if (Mage::app()->getStore()->isAdmin()) {
         $checkout = Mage::getSingleton('adminhtml/session_quote')->getQuote();
     } else {
         $checkout = Mage::getSingleton('checkout/session')->getQuote();
     }
     $billing = $checkout->getBillingAddress();
     $order_description = Mage::getStoreConfig('payment/' . $this->_code . '/description', $store_id);
     if (!$order_description) {
         $order_description = "Order";
     }
     $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
     $name = $billing->getName();
     $billing_address = array("address1" => $billing->getStreet(1), "address2" => $billing->getStreet(2), "address3" => $billing->getStreet(3), "postalCode" => $billing->getPostcode(), "city" => $billing->getCity(), "state" => "", "countryCode" => $billing->getCountry());
     try {
         $mode = Mage::getStoreConfig('payment/worldpay_mode', Mage::app()->getStore()->getStoreId());
         $orderType = 'ECOM';
         $threeDS = Mage::getStoreConfig('payment/worldpay_cc/use3ds', Mage::app()->getStore()->getStoreId());
         if (Mage::app()->getStore()->isAdmin()) {
             $orderType = 'MOTO';
             $threeDS = false;
         }
         if ($threeDS && $mode == 'Test Mode') {
             $name = '3D';
         }
         $settlementCurrency = Mage::getStoreConfig('payment/worldpay_cc/settlementcurrency', Mage::app()->getStore()->getStoreId());
         $createOrderRequest = array('token' => $token, 'orderDescription' => $order_description, 'amount' => $amount * 100, 'currencyCode' => $currency_code, 'name' => $name, 'orderType' => $orderType, 'is3DSOrder' => $threeDS, 'authoriseOnly' => $authorize, 'billingAddress' => $billing_address, 'customerOrderCode' => $orderId, 'settlementCurrency' => $settlementCurrency);
         $logger->log('Order Request: ' . print_r($createOrderRequest, true));
         $response = $worldpay->createOrder($createOrderRequest);
         $logger->log('Order Response: ' . print_r($response, true));
         if ($response['paymentStatus'] === 'SUCCESS') {
             $this->setStore($payment->getOrder()->getStoreId());
             $logger->log('Order: ' . $response['orderCode'] . ' SUCCESS');
             $payment->setStatus(self::STATUS_APPROVED);
             $payment->setAmount($amount);
             $payment->setLastTransId($orderId);
             $payment->setTransactionId($response['orderCode']);
             $payment->setAdditionalInformation("worldpayOrderCode", $response['orderCode']);
             // $formatedPrice = $order->getBaseCurrency()->formatTxt($amount);
             $payment->setShouldCloseParentTransaction(1)->setIsTransactionClosed(1)->registerCaptureNotification($amount);
         } else {
             if ($response['paymentStatus'] == 'AUTHORIZED') {
                 $this->setStore($payment->getOrder()->getStoreId());
                 $logger->log('Order: ' . $response['orderCode'] . ' AUTHORIZED');
                 $payment->setIsTransactionClosed(0);
                 $payment->setSkipOrderProcessing(true);
                 $payment->setStatus(self::STATUS_APPROVED);
                 $payment->setAmount($amount);
                 $payment->setAdditionalInformation("worldpayOrderCode", $response['orderCode']);
                 $payment->setLastTransId($orderId);
                 $payment->setTransactionId($response['orderCode']);
             } else {
                 if ($response['is3DSOrder']) {
                     $session = Mage::getSingleton('core/session');
                     $logger->log('Starting 3DS Order: ' . $response['orderCode']);
                     $session->setData('wp_3dsSuccess', false);
                     $session->setData('wp_redirectURL', $response['redirectURL']);
                     $session->setData('wp_oneTime3DsToken', $response['oneTime3DsToken']);
                     $session->setData('wp_orderCode', $response['orderCode']);
                     // IF normal checkout
                     $currentUrl = Mage::helper('core/url')->getCurrentUrl();
                     $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
                     $path = $url->getPath();
                     if (strpos($path, 'onepage') === false) {
                         Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('worldpay/threeDS'));
                         Mage::app()->getResponse()->sendResponse();
                     } else {
                         echo 'window.WorldpayMagento.loadThreeDS("' . Mage::getUrl('worldpay/threeDS') . '")';
                     }
                     exit;
                 } else {
                     if (isset($response['paymentStatusReason'])) {
                         throw new Exception($response['paymentStatusReason']);
                     } else {
                         throw new Exception(print_r($response, true));
                     }
                 }
             }
         }
     } catch (Exception $e) {
         $payment->setStatus(self::STATUS_ERROR);
         $payment->setAmount($amount);
         $payment->setLastTransId($orderId);
         $logger->log($e->getMessage());
         Mage::throwException('Payment failed, please try again later ' . $e->getMessage());
     }
     return $this;
 }