function testTransactionCurrencyConverting() { $eur = Currency::getNewInstance('EUR'); $eur->rate->set('3.4528'); $eur->save(); $this->products[0]->setPrice($this->usd, '9.99'); $this->order->addProduct($this->products[0], 1); $this->order->save(); $this->order->changeCurrency($this->usd); //$this->order->finalize(); ActiveRecord::clearPool(); $order = CustomerOrder::getInstanceByID($this->order->getID(), true); $order->loadAll(); $details = new LiveCartTransaction($order, $eur); ActiveRecord::clearPool(); $order = CustomerOrder::getInstanceByID($this->order->getID(), true); $order->loadAll(); $this->assertEquals($details->amount->get(), '2.89'); $result = new TransactionResult(); $result->amount->set($details->amount->get()); $result->currency->set($details->currency->get()); $transaction = Transaction::getNewInstance($order, $result); $transaction->type->set(Transaction::TYPE_SALE); $this->assertEquals($transaction->amount->get(), '9.99'); $this->assertEquals($transaction->realAmount->get(), '2.89'); $transaction->save(); $this->assertFalse((bool) $order->isFinalized->get()); $order->finalize(); $this->assertTrue((bool) $order->isFinalized->get()); $this->assertEquals($order->getPaidAmount(), '9.99'); $this->assertEquals($order->totalAmount->get(), '9.99'); $this->assertTrue((bool) $order->isPaid->get()); }
public function testPaymentMethod() { ActiveRecordModel::getApplication()->getConfig()->setRuntime('INVENTORY_TRACKING', 'DISABLE'); $this->product1->isEnabled->set(true); $this->product1->save(); $this->order->addProduct($this->product1, 1); $this->order->save(); ActiveRecord::clearPool(); $this->order = CustomerOrder::getInstanceById($this->order->getID(), true); $this->order->loadAll(); $this->assertEquals(1, $this->order->getShoppingCartItemCount()); $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->conditionClass->set('RuleConditionPaymentMethodIs'); $condition->addValue('TESTING'); $condition->save(); $this->assertEquals(0, count($this->order->getDiscountConditions(true))); $this->order->setPaymentMethod('TESTING'); $this->assertEquals(1, count($this->order->getDiscountConditions(true))); $this->order->setPaymentMethod('AnotherOne'); $this->assertEquals(0, count($this->order->getDiscountConditions(true))); // test finalized order ClassLoader::import('library.payment.TransactionResult'); $transResult = new TransactionResult(); $transResult->setTransactionType(TransactionResult::TYPE_SALE); $transResult->amount->set(10000); $transResult->currency->set('USD'); $transaction = Transaction::getNewInstance($this->order, $transResult); $transaction->method->set('TESTING'); $transaction->save(); $this->order->finalize(); ActiveRecord::clearPool(); $reloaded = CustomerOrder::getInstanceById($this->order->getID(), true); $reloaded->loadAll(); $this->assertTrue($reloaded->isExistingRecord()); $this->assertEquals(1, $reloaded->getShoppingCartItemCount()); $this->assertEquals(1, count($reloaded->getDiscountConditions(true))); $condition->removeValue('TESTING'); $condition->addValue('Whatever'); $condition->save(); $this->assertEquals(0, count($reloaded->getDiscountConditions(true))); }
protected function registerPayment(TransactionResult $result, TransactionPayment $handler) { // transaction already registered? if (Transaction::getInstance($this->order, $result->gatewayTransactionID->get())) { $this->session->set('completedOrderID', $this->order->getID()); return new ActionRedirectResponse('checkout', 'completed'); } $transaction = Transaction::getNewInstance($this->order, $result); $transaction->setHandler($handler); $transaction->save(); $this->order->setPaidStatus(); if ($this->order->isPaid->get()) { $this->order->save(); } return $this->finalizeOrder(); }
function testPayment() { $this->order->addProduct($this->products[0], 1); $this->order->addProduct($this->products[1], 1); $this->order->save(); $this->order->finalize(); $result = new TransactionResult(); $result->amount->set($this->order->totalAmount->get()); $result->currency->set($this->order->currency->get()->getID()); $result->gatewayTransactionID->set('TESTTRANSACTION'); $result->setTransactionType(TransactionResult::TYPE_SALE); $transaction = Transaction::getNewInstance($this->order, $result); $transaction->save(); $this->assertEqual($this->order->totalAmount->get(), $this->order->capturedAmount->get()); }
public function create() { $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>'); $request = $this->application->getRequest(); $orderID = $request->get('orderID'); $currencyID = $request->get('currencyID'); $amount = $request->get('amount'); $method = $request->get('method'); $gatewayTransactionID = $request->get('gatewayTransactionID'); if (intval($orderID) <= 0) { throw new Exception('Order ID is required'); } if (!isset($currencyID) || !isset($method) || !isset($gatewayTransactionID)) { throw new Exception('Complete required fields : currencyID, method, gatewayTransactionID'); } $transaction_result = new TransactionResult(); $transaction_result->currency->set($currencyID); // = $currencyID; $transaction_result->amount->set($amount); //= $amount; $transaction_result->gatewayTransactionID->set($gatewayTransactionID); // = $gatewayTransactionID; $transaction_result->setTransactionType(TransactionResult::TYPE_SALE); $order = CustomerOrder::getInstanceById($orderID); $order->loadAll(); $order_array = $order->toArray(); $user = User::getInstanceByID($order_array['userID']); $user->load(); $order->user->set($user); if ($current_transaction = Transaction::getInstance($order, $gatewayTransactionID)) { $current_transaction->method->set($method); $current_transaction->save(); } else { $new_transaction = Transaction::getNewInstance($order, $transaction_result); $new_transaction->method->set($method); $new_transaction->save(); if (!$this->finalizeOrder($order, $user)) { // Create new order for current user $new_order = CustomerOrder::getNewInstance($user); $new_order->beginTransaction(); $new_order->save(); $new_order->commit(); } } $parser = $this->getParser(); $apiFieldNames = $parser->getApiFieldNames(); $f = new ARSelectFilter(); $f->mergeCondition(new EqualsCond(new ARFieldHandle('Transaction', 'orderID'), $orderID)); $transactions = ActiveRecordModel::getRecordSetArray('Transaction', $f); $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>'); if (false && count($transactions) == 0) { throw new Exception('Transactions not found'); } while ($transaction = array_shift($transactions)) { $transaction_response = $response->addChild('transaction'); foreach ($transaction as $k => $v) { if (in_array($k, $apiFieldNames)) { $transaction_response->addChild($k, htmlentities($v)); } } } return new SimpleXMLResponse($response); }
/** * @role update */ public function processCreditCard() { $order = CustomerOrder::getInstanceById($this->request->get('id')); if (!$this->buildCreditCardValidator()->isValid()) { return new ActionRedirectResponse('backend.payment', 'ccForm', array('id' => $order->getID())); } // set up transaction details $transaction = new LiveCartTransaction($order, $order->currency->get()); $transaction->amount->set($this->request->get('amount')); // process payment $handler = $this->application->getCreditCardHandler($transaction); if ($this->request->isValueSet('ccType')) { $handler->setCardType($this->request->get('ccType')); } $handler->setCardData($this->request->get('ccNum'), $this->request->get('ccExpiryMonth'), $this->request->get('ccExpiryYear'), $this->request->get('ccCVV')); if ($this->config->get('CC_AUTHONLY')) { $result = $handler->authorize(); } else { $result = $handler->authorizeAndCapture(); } if ($result instanceof TransactionResult) { $order->isPaid->set(true); $transaction = Transaction::getNewInstance($order, $result); $transaction->setHandler($handler); $transaction->comment->set($this->request->get('comment')); $transaction->save(); if ($order->totalAmount->get() <= $order->capturedAmount->get()) { $order->isPaid->set(true); $order->save(); } $this->request->set('id', $transaction->getID()); return $this->getTransactionUpdateResponse(); } elseif ($result instanceof TransactionError) { return new JSONResponse(false, 'failure', $this->translate('_err_processing_cc')); } else { throw new Exception('Unknown transaction result type: ' . get_class($result)); } }