/** * This method is a sad reality in order for the total amount to work in * all the different magento versions we support. Calculations differ * between systems and we can't at this point send only the grand total * value (that would fix things). * * @param $request * @param $order * @return mixed */ protected function _equalizeRows($request, $order) { // Find out what the amount is, and if it differs from the Magento // grand total, add it as an equalisation row $paymentForm = $request->getPaymentForm(); $message = @simplexml_load_string($paymentForm->xmlMessage); $amount = (int) $message->amount; $grandTotal = (int) ($order->getGrandTotal() * 100); if ($amount !== $grandTotal) { // The difference can be negative or positive $diff = $grandTotal - $amount; $diff /= 100; $svea = $request->order; if ($diff < 0) { // Negative, use a discount row $row = WebPayItem::fixedDiscount()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setAmountIncVat(abs($diff)); $svea->addDiscount($row); } else { // Positive, use a normal order row $rate = $this->_getOrderMainTaxRate($order); $row = WebPayItem::orderRow()->setArticleNumber('eq')->setQuantity(1)->setName('Magento Equalisation')->setUnit(Mage::helper('svea_webpay')->__('unit'))->setVatPercent($rate)->setAmountIncVat($diff); $svea->addOrderRow($row); } } return $request; }
public function testBuildOrderWithFixedDiscount() { $config = SveaConfig::getDefaultConfig(); $sveaRequest = \WebPay::createOrder($config)->addDiscount(\WebPayItem::fixedDiscount()->setDiscountId("1")->setAmountIncVat(100.0)->setUnit("st")->setDescription("FixedDiscount")->setName("Fixed")); $this->assertEquals("1", $sveaRequest->fixedDiscountRows[0]->discountId); $this->assertEquals(100.0, $sveaRequest->fixedDiscountRows[0]->amount); $this->assertEquals("FixedDiscount", $sveaRequest->fixedDiscountRows[0]->description); //test type $this->assertInternalType("float", $sveaRequest->fixedDiscountRows[0]->amount); }
public function testBuildCardPaymentWithAmountExVatWithAmountIncVat() { $config = SveaConfig::getDefaultConfig(); $form = \WebPay::createOrder($config)->addOrderRow(\WebPayItem::orderRow()->setArticleNumber("1")->setQuantity(1)->setAmountExVat(240.0)->setAmountIncVat(300.0)->setDescription("CD"))->addOrderRow(\WebPayItem::orderRow()->setArticleNumber("1")->setQuantity(1)->setAmountExVat(188.68)->setAmountIncVat(200.0)->setDescription("Bok"))->addDiscount(\WebPayItem::fixedDiscount()->setDiscountId("1")->setAmountIncVat(100.0)->setUnit("st")->setDescription("testBuildCardPaymentWithAmountExVatWithAmountIncVat")->setName("Fixed"))->setCountryCode("SE")->setClientOrderNumber("33")->setOrderDate("2012-12-12")->setCurrency("SEK")->usePayPageCardOnly()->setReturnUrl("http://myurl.se")->getPaymentForm(); $xmlMessage = new \SimpleXMLElement($form->xmlMessage); $this->assertEquals('40000', $xmlMessage->amount); $this->assertEquals('5706', $xmlMessage->vat); $this->assertEquals('30000', $xmlMessage->orderrows->row[0]->amount); $this->assertEquals('20000', $xmlMessage->orderrows->row[1]->amount); $this->assertEquals('-10000', $xmlMessage->orderrows->row[2]->amount); }
public function test_bv_order_sent_incvat_two_decimals_with_both_discounts_with_amount_only() { // print_r("\n\n-----test_bv_order_sent_incvat_two_decimals_with_both_discounts_with_amount_only()\n"); $order = WebPay::createOrder(Svea\SveaConfig::getDefaultConfig())->addCustomerDetails(WebPayItem::individualCustomer()->setNationalIdNumber(194605092222))->setCountryCode("SE")->setCustomerReference("1337")->setOrderDate("2015-05-20")->setCurrency("SEK")->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(1.0)->setVatPercent(6)->setQuantity(800)->setName("3.00i@6%*800"))->setClientOrderNumber(date('c')); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(240)->setDiscountId("fixedDiscount")->setName("-240i*1")); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(20)->setDiscountId("fixedDiscount2")->setName("-20i*1")); $request = $order->usePaymentMethod(PaymentMethod::KORTCERT)->setReturnUrl("https://test.sveaekonomi.se/webpay-admin/admin/merchantresponsetest.xhtml"); $request = $request->getPaymentForm(); // print_r( $request->xmlMessage ); // 240i@6% => 240 (13,58491) => 24000 (1358) $expectedDiscountRow = " <row>\n" . " <sku>fixedDiscount</sku>\n" . " <name>-240i*1</name>\n" . " <description></description>\n" . " <amount>-24000</amount>\n" . " <vat>-1358</vat>\n"; " <quantity>1</quantity>\n" . " </row>\n"; $this->assertEquals(1, substr_count($request->xmlMessage, $expectedDiscountRow)); // 20i@6% => 2000 (1,132076) => 2000 (113) $expectedDiscountRow2 = " <row>\n" . " <sku>fixedDiscount2</sku>\n" . " <name>-20i*1</name>\n" . " <description></description>\n" . " <amount>-2000</amount>\n" . " <vat>-113</vat>\n"; " <quantity>1</quantity>\n" . " </row>\n"; $this->assertEquals(1, substr_count($request->xmlMessage, $expectedDiscountRow2)); // lagt ordern med den dumpade xml:en från utskriften i tools/payment, ger detta response: //<response> // <transaction id="600123"> // <paymentmethod>KORTCERT</paymentmethod> // <merchantid>1130</merchantid> // <customerrefno>2015-05-22T13:00:54 02:00</customerrefno> // <amount>54000</amount> // <currency>SEK</currency> // <cardtype>VISA</cardtype> // <maskedcardno>444433xxxxxx1100</maskedcardno> // <expirymonth>02</expirymonth> // <expiryyear>17</expiryyear> // <authcode>594378</authcode> // <customer> // <firstname/> // <lastname/> // <initials/> // <email/> // <ssn>194605092222</ssn> // <address/> // <address2/> // <city/> // <country>SE</country> // <zip/> // <phone/> // <vatnumber/> // <housenumber/> // <companyname/> // <fullname/> // </customer> // </transaction> // <statuscode>0</statuscode> //</response> }
public function test_getOrderTotals_has_same_amounts_as_service_when_order_sent_priceIncludingVat_true() { $order = GetRequestTotalsTest::create_only_incvat_order_and_fee_rows_order(); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(8.0)->setVatPercent(20)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 8.00i@20%"))->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(3.67)->setVatPercent(10)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 3.67ie@10%")); $request = $order->useInvoicePayment()->prepareRequest(); // all order rows $this->assertEquals(72.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(33.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat); // all invoice fee rows $this->assertEquals(8.800000000000001, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat); // all shipping fee rows $this->assertEquals(17.6, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat); // all discount rows // expected: fixedDiscount: 10 exvat => split across 10e *(60/60+30) @20% + 10e *(30/60+30) @10% => 6.6666e @20% + 3.3333e @10% => 8.00i + 3.67i $this->assertEquals(-8.0, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit), 2, PHP_ROUND_HALF_UP); //=WS $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat); $this->assertEquals(-3.67, \Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PricePerUnit), 2, PHP_ROUND_HALF_UP); //=WS $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PriceIncludingVat); // check that service accepts order //$response = $order->useInvoicePayment()->doRequest(); //$this->assertEquals( true, $response->accepted ); //// r() is round($val, 2, PHP_ROUND_HALF_EVEN), i.e. bankers rounding //// r(72.00*1) + r(33.00*1) + r(17.60*1) + r(8.80*1) + r(-8.00*1) + r(-3.66*1) => 72.00+33.00+17.60+8.80-8.00-3.67 => 119.73 ////$this->assertEquals( "119.73", $response->amount ); // TODO check that this is the amount in S1 invoice, vs 119.74 w/PriceIncludingVat = false //$this->assertEquals( "119.73", $response->amount ); // jfr vs 119.73 w/PriceIncludingVat = true // verify that getRequestTotals() got the same amount as the service $preview = $order->useInvoicePayment()->getRequestTotals(); $this->assertEquals("119.73", $preview['total_incvat']); //$this->assertEquals( $preview['total_incvat'], $response->amount ); }
public function testDeliverOrderAndFixedDiscountSetWithMixedVat3() { $config = Svea\SveaConfig::getDefaultConfig(); $request = WebPay::deliverOrder($config); $request = $request->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(123.9876)->setAmountExVat(99.98999999999999)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setAmountExVat(9.999000000000001)->setVatPercent(24))->setOrderId("id")->setInvoiceDistributionType(DistributionType::POST)->setCreditInvoice("id")->setCountryCode("SE")->deliverInvoiceOrder()->prepareRequest(); $this->assertEquals(99.98999999999999, $request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(24, $request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][0]->VatPercent); $this->assertFalse($request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(-9.999000000000001, $request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(24, $request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][1]->VatPercent); $this->assertFalse($request->request->DeliverOrderInformation->DeliverInvoiceDetails->OrderRows['OrderRow'][1]->PriceIncludingVat); }
public function test_incvat_only_order_with_fixedDiscount_amount_specified_as_incvat_and_calculated_vat_rate_order_sent_with_PriceIncludingVat_true() { $order = $this->order; $order->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(720.0)->setVatPercent(20)->setQuantity(1))->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(330.0)->setVatPercent(10)->setQuantity(1))->addFee(WebPayItem::invoiceFee()->setAmountIncVat(88.0)->setVatPercent(10))->addFee(WebPayItem::shippingFee()->setAmountIncVat(172.0)->setVatPercent(10)); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(10.0)->setDiscountId("TenCrownsOff")->setName("fixedDiscount: 10 off incvat")); $request = $order->useInvoicePayment()->prepareRequest(); // all order rows $this->assertEquals(720.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(330.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat); // all invoice fee rows $this->assertEquals(88.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat); // all shipping fee rows $this->assertEquals(172.0, Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit, 2)); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat); // all discount rows // expected: fixedDiscount: 10 off incvat, order row amount are 66% at 20% vat, 33% at 10% vat // 1.2*0.66x + 1.1*0.33x = 10 => x = 8.6580 => 5.7143 @20% and 2.8571 @10% = 10kr $this->assertEquals(-6.86, Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit, 2)); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat); $this->assertEquals(-3.14, Svea\Helper::bround($request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PricePerUnit, 2)); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PriceIncludingVat); }
/** * Builds request for DeliverInvoice as Credit for Invoice. Calls from * Magento Refund * * @param type $payment * @param type $auth * @param type $sveaOrderId * @return type */ public function getRefundRequest($payment, $auth, $sveaOrderId) { $conf = new SveaMageConfigProvider($auth); $sveaObject = WebPay::deliverOrder($conf); $order = $payment->getOrder(); $creditMemo = $payment->getCreditmemo(); $storeId = $order->getStoreId(); $countryCode = $order->getBillingAddress()->getCountryId(); $taxCalculationModel = Mage::getSingleton('tax/calculation'); $taxConfig = Mage::getSingleton('tax/config'); $store = Mage::app()->getStore($storeId); foreach ($creditMemo->getAllItems() as $item) { $orderItem = $item->getOrderItem(); if ($orderItem->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { continue; } // Default to the item price $name = $item->getName(); $price = $orderItem->getPrice(); $priceInclTax = $orderItem->getPriceInclTax(); $taxPercent = $orderItem->getTaxPercent(); if (!(int) $taxPercent) { $taxPercent = false; } $qty = $item->getQty(); $parentItem = $orderItem->getParentItem(); if ($parentItem) { switch ($parentItem->getProductType()) { case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE: $price = $parentItem->getPrice(); $priceInclTax = $parentItem->getPriceInclTax(); $taxPercent = $parentItem->getTaxPercent(); $qty = $parentItem->getQtyRefunded(); break; case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE: $taxPercent = $priceInclTax = $price = 0; $name = '- ' . $name; break; } } if ($taxPercent === false) { // If it's a bundle item we have to calculate the tax from // the including/excluding tax values $taxPercent = round(100 * ($priceInclTax / $price - 1)); } $orderRow = WebPayItem::orderRow()->setArticleNumber($item->getSku())->setQuantity((int) $qty)->setName($name)->setUnit(Mage::helper('svea_webpay')->__('unit'))->setVatPercent((int) $taxPercent)->setAmountIncVat((double) $priceInclTax); $sveaObject->addOrderRow($orderRow); } $request = $taxCalculationModel->getRateRequest($order->getShippingAddress(), $order->getBillingAddress(), null, $store); // Shipping if ($creditMemo->getShippingAmount() > 0) { $shippingFee = WebPayItem::shippingFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName($order->getShippingDescription()); // We require shipping tax to be set $shippingTaxClass = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $storeId); $rate = $taxCalculationModel->getRate($request->setProductClassId($shippingTaxClass)); $shippingFee->setVatPercent((int) $rate); if ($taxConfig->shippingPriceIncludesTax($storeId)) { $shippingFee->setAmountIncVat($creditMemo->getShippingInclTax()); } else { $shippingFee->setAmountExVat($creditMemo->getShippingAmount()); } $sveaObject->addFee($shippingFee); } // Discount $discount = abs($creditMemo->getDiscountAmount()); if ($discount > 0) { $discountRow = WebPayItem::fixedDiscount()->setAmountIncVat($discount)->setName(Mage::helper('svea_webpay')->__('discount'))->setUnit(Mage::helper('svea_webpay')->__('unit')); $sveaObject->addDiscount($discountRow); } // Gift cards if (abs($creditMemo->getGiftCardsAmount()) > 0) { $giftCardRow = WebPayItem::fixedDiscount()->setAmountIncVat(abs($creditMemo->getGiftCardsAmount()))->setUnit(Mage::helper('svea_webpay')->__('unit')); $sveaObject->addDiscount($giftCardRow); } // Invoice fee $paymentFee = $creditMemo->getSveaPaymentFeeAmount(); $paymentFeeInclTax = $creditMemo->getSveaPaymentFeeInclTax(); $refunded = $creditMemo->getOrder()->getSveaPaymentFeeRefunded(); if ($paymentFee > 0 && $refunded == 0) { $invoiceFee = WebPayItem::invoiceFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName(Mage::helper('svea_webpay')->__('invoice_fee'))->setAmountExVat($paymentFee)->setAmountIncVat($paymentFeeInclTax); $sveaObject = $sveaObject->addFee($invoiceFee); $creditMemo->getOrder()->setSveaPaymentFeeRefunded($paymentFeeInclTax); } $adjustmentFee = $creditMemo->getAdjustmentPositive(); if ($adjustmentFee > 0) { $invoiceAdjustment = WebPayItem::invoiceFee()->setVatPercent(0)->setAmountIncVat($adjustmentFee); $sveaObject->addFee($invoiceAdjustment); } $response = $sveaObject->setCountryCode($countryCode)->setOrderId($sveaOrderId)->setInvoiceDistributionType(Mage::getStoreConfig("payment/svea_invoice/deliver_method")); $order->setData('svea_refund_request', $response); return $order->getData('svea_refund_request'); }
public static function fixedDiscount() { return WebPayItem::fixedDiscount(); }
public function test_FixedDiscount_specified_using_amountExVat_and_vatPercent() { $config = Svea\SveaConfig::getDefaultConfig(); $order = WebPay::createOrder($config); $order->addOrderRow(WebPayItem::orderRow()->setAmountExVat(100.0)->setVatPercent(25)->setQuantity(2))->addOrderRow(WebPayItem::orderRow()->setAmountExVat(100.0)->setVatPercent(6)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setName("->setAmountExVat(100)")->setDescription("testFormatFixedDiscountRowsWithDifferentVatRatesPresent2 (25%)")->setAmountExVat(66.67)->setVatPercent(25))->addDiscount(WebPayItem::fixedDiscount()->setName("->setAmountExVat(100)")->setDescription("testFormatFixedDiscountRowsWithDifferentVatRatesPresent2 (6%)")->setAmountExVat(33.33)->setVatPercent(6)); $formatter = new WebServiceRowFormatter($order); $resultRows = $formatter->formatRows(); $testedRow = $resultRows[2]; $this->assertEquals("->setAmountExVat(100): testFormatFixedDiscountRowsWithDifferentVatRatesPresent2 (25%)", $testedRow->Description); $this->assertEquals(-66.67, $testedRow->PricePerUnit); $this->assertEquals(25, $testedRow->VatPercent); $testedRow = $resultRows[3]; $this->assertEquals("->setAmountExVat(100): testFormatFixedDiscountRowsWithDifferentVatRatesPresent2 (6%)", $testedRow->Description); $this->assertEquals(-33.33, $testedRow->PricePerUnit); $this->assertEquals(6, $testedRow->VatPercent); $this->assertEquals(false, isset($resultRows[4])); // no more rows }
public function testCreateOrderWithDiscountAsAmountExAndDeliverWithAmountIncvat() { $config = Svea\SveaConfig::getDefaultConfig(); $campaigncode = TestUtil::getGetPaymentPlanParamsForTesting(); $order = WebPay::createOrder($config)->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(1239.876)->setVatPercent(24)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(8)->setVatPercent(24))->addDiscount(WebPayItem::relativeDiscount()->setDiscountPercent(10))->addCustomerDetails(TestUtil::createIndividualCustomer("SE"))->setCountryCode("SE")->setOrderDate("2012-12-12")->usePaymentPlanPayment($campaigncode)->doRequest(); $request = WebPay::deliverOrder($config); $request = $request->addOrderRow(WebPayItem::orderRow()->setAmountExVat(999.9)->setVatPercent(24)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setAmountExVat(8)->setVatPercent(24))->addDiscount(WebPayItem::relativeDiscount()->setDiscountPercent(10))->setOrderId($order->sveaOrderId)->setInvoiceDistributionType(DistributionType::POST)->setCountryCode("SE")->deliverPaymentPlanOrder()->doRequest(); $this->assertEquals(1, $request->accepted); }
public function test_FixedDiscount_specified_using_amountExVat_is_calculated_from_order_item_rows_only_php_original_version() { $order = \WebPay::createOrder(Svea\SveaConfig::getDefaultConfig())->addOrderRow(\WebPayItem::orderRow()->setName("product with price 100 @25% = 125")->setAmountExVat(100.0)->setVatPercent(25)->setQuantity(2.0))->addOrderRow(\WebPayItem::orderRow()->setName("product with price 100 @6% = 106")->setAmountExVat(100.0)->setVatPercent(6)->setQuantity(1.0))->addFee(\WebPayItem::shippingFee()->setName("shipping with price 50 @6% = 53")->setAmountExVat(50.0)->setVatPercent(6))->addFee(\WebPayItem::invoiceFee()->setAmountExVat(23.2)->setVatPercent(25))->addDiscount(\WebPayItem::fixedDiscount()->setDiscountId("f100e")->setName("couponName")->setDescription("couponDesc")->setAmountExVat(100.0)->setUnit("st")); $formatter = new HostedRowFormatter(); $newRows = $formatter->formatRows($order); // of type HostedOrderRowBuilder // 100*200/300 = 66.67 ex. 25% vat => discount 83.34 (incl. 16.67 vat @25%) // 100*100/300 = 33.33 ex. 6% vat => discount 35.33 (incl 2.00 vat @6%) // => total discount is 118.67 (incl 18.67 vat @18.67%) // validate HostedOrderRowBuilder row contents $newRow = $newRows[4]; $this->assertEquals("f100e", $newRow->sku); $this->assertEquals("couponName", $newRow->name); $this->assertEquals("couponDesc", $newRow->description); $this->assertEquals(-11867, $newRow->amount); $this->assertEquals(-1867, $newRow->vat); $this->assertEquals(1, $newRow->quantity); $this->assertEquals("st", $newRow->unit); $paymentForm = $order->setCountryCode("SE")->setOrderDate("2015-02-23")->setClientOrderNumber("unique")->setCurrency("SEK")->usePaymentMethod(PaymentMethod::KORTCERT)->setReturnUrl("http://myurl.com")->getPaymentForm(); $paymentXml = $paymentForm->xmlMessage; // // 250.00 (50.00) // // 106.00 (6.00) // // 53.00 (3.00) // // 29.00 (5.80) // // -118.67 (-18.67) // // => 319.33 (46.13) $this->assertRegexp('/<amount>31933<\\/amount>/', $paymentXml); $this->assertRegexp('/<vat>4613<\\/vat>/', $paymentXml); }
public function test_getRequestTotals_reference_1400_00_inc_cant_be_done_with_compensation_row() { $config = Svea\SveaConfig::getDefaultConfig(); $order = WebPay::createOrder($config)->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(1400.0)->setVatPercent(6)->setQuantity(1))->addCustomerDetails(TestUtil::createIndividualCustomer("SE"))->setCountryCode("SE")->setOrderDate("2016-04-14"); $preview_total = $order->useInvoicePayment()->getRequestTotals(); $this->assertEquals(1400.0, $preview_total['total_incvat']); $this->assertEquals(1320.75, $preview_total['total_exvat']); $this->assertEquals(79.25, $preview_total['total_vat']); $target_total = 1400.0; $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(-0.25)->setVatPercent(0)); $compensated_preview_total = $order->useInvoicePayment()->getRequestTotals(); $this->assertEquals(1400.25, $compensated_preview_total['total_incvat']); // should be 1400.00! $this->assertEquals(1321.0, $compensated_preview_total['total_exvat']); $this->assertEquals(79.25, $compensated_preview_total['total_vat']); $response = $order->useInvoicePayment()->doRequest(); $this->assertEquals(1, $response->accepted); $this->assertEquals($compensated_preview_total['total_incvat'], $response->amount); }
/** * Add values and rows to Svea CreateOrder object * * Configurable products: * Calculate prices using the parent price, to take price variations into concern * * Simple products: * Just use their prices as is * * Bundle products: * The main parent product has the price, but the associated products * need to be transferred on separate 0 amount lines so the invoice is * verbose enough * * Grouped products: * These are treated the same way as simple products * * @param type $order * @param type $additionalInfo * @return type Svea CreateOrder object */ public function getSveaPaymentObject($order, $additionalInfo = null) { //Get Request and billing addres $svea = $order->getData('svea_payment_request'); $billingAddress = $order->getBillingAddress(); $storeId = $order->getStoreId(); $store = Mage::app()->getStore($storeId); $taxCalculationModel = Mage::getSingleton('tax/calculation'); $taxConfig = Mage::getSingleton('tax/config'); // Build the rows for request foreach ($order->getAllItems() as $item) { if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { continue; } // Default to the item price $name = $item->getName(); $price = $item->getPrice(); $priceInclTax = $item->getPriceInclTax(); $taxPercent = $item->getTaxPercent(); if (!(int) $taxPercent) { $taxPercent = false; } $parentItem = $item->getParentItem(); if ($parentItem) { switch ($parentItem->getProductType()) { case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE: $priceInclTax = $parentItem->getPriceInclTax(); $taxPercent = $parentItem->getTaxPercent(); break; case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE: $taxPercent = $priceInclTax = $price = 0; $name = '- ' . $name; break; } } if ($taxPercent === false) { if ((double) $price === 0.0) { if ((double) $priceInclTax === 0.0) { $taxPercent = 0; } else { throw new Mage_Exception("Price is 0 but priceIncTax is not"); } } else { // If it's a bundle item we have to calculate the tax from // the including/excluding tax values $taxPercent = round(100 * ($priceInclTax / $price - 1)); } } $qty = get_class($item) == 'Mage_Sales_Model_Quote_Item' ? $item->getQty() : $item->getQtyOrdered(); $orderRow = WebPayItem::orderRow()->setArticleNumber($item->getSku())->setQuantity((int) $qty)->setName($name)->setUnit(Mage::helper('svea_webpay')->__('unit'))->setVatPercent((int) $taxPercent)->setAmountIncVat((double) $priceInclTax); $svea->addOrderRow($orderRow); } $request = $taxCalculationModel->getRateRequest($order->getShippingAddress(), $order->getBillingAddress(), null, $store); // Shipping if ($order->getShippingAmount() > 0) { $shippingFee = WebPayItem::shippingFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName($order->getShippingDescription()); // We require shipping tax to be set $shippingTaxClass = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_TAX_CLASS, $storeId); $rate = $taxCalculationModel->getRate($request->setProductClassId($shippingTaxClass)); $shippingFee->setVatPercent((int) $rate); $shippingFee->setAmountIncVat($order->getShippingInclTax()); $svea->addFee($shippingFee); } // Discount $discount = abs($order->getDiscountAmount()); if ($discount > 0) { $discountRow = WebPayItem::fixedDiscount()->setName(Mage::helper('svea_webpay')->__('discount'))->setUnit(Mage::helper('svea_webpay')->__('unit'))->setAmountIncVat($discount); $svea->addDiscount($discountRow); } // Gift cards if (abs($order->getGiftCardsAmount()) > 0) { $giftCardRow = WebPayItem::fixedDiscount()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setAmountIncVat(abs($order->getGiftCardsAmount())); $svea->addDiscount($giftCardRow); } // Invoice fee $paymentFeeInclTax = $order->getSveaPaymentFeeInclTax(); if ($paymentFeeInclTax > 0) { $paymentFeeTaxClass = $this->getConfigData('handling_fee_tax_class'); $rate = $taxCalculationModel->getRate($request->setProductClassId($paymentFeeTaxClass)); $invoiceFeeRow = WebPayItem::invoiceFee()->setUnit(Mage::helper('svea_webpay')->__('unit'))->setName(Mage::helper('svea_webpay')->__('invoice_fee'))->setVatPercent((int) $rate); $invoiceFeeRow->setAmountIncVat((double) $paymentFeeInclTax); $svea->addFee($invoiceFeeRow); } $svea->setCountryCode($billingAddress->getCountryId())->setClientOrderNumber($order->getIncrementId())->setOrderDate(date("Y-m-d"))->setCurrency($order->getOrderCurrencyCode()); return $svea; }
public function testResponseOrderAndFixedDiscountSetWithMixedVat3() { $config = Svea\SveaConfig::getDefaultConfig(); $request = WebPay::createOrder($config)->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(123.9876)->setAmountExVat(99.98999999999999)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setAmountExVat(9.999000000000001))->addCustomerDetails(TestUtil::createIndividualCustomer("SE"))->setCountryCode("SE")->setOrderDate("2012-12-12")->useInvoicePayment()->doRequest(); $this->assertEquals(1, $request->accepted); }
public function testOrderRowsInOrderAsAddedOnHosted() { $config = SveaConfig::getDefaultConfig(); $request = \WebPay::createOrder($config); $form = $request->addOrderRow(\WebPayItem::orderRow()->setDescription("1")->setQuantity(1)->setAmountIncVat(10.0)->setVatPercent(25))->addFee(\WebPayItem::invoiceFee()->setDescription("2")->setVatPercent(25)->setAmountIncVat(30))->addOrderRow(\WebPayItem::orderRow()->setDescription("3")->setQuantity(1)->setAmountIncVat(20.0)->setVatPercent(25))->addDiscount(\WebPayItem::fixedDiscount()->setDescription("4")->setAmountIncVat(10))->setCountryCode("SE")->setClientOrderNumber("33")->setOrderDate("2012-12-12")->setCurrency("SEK")->usePaymentMethod(\PaymentMethod::SVEACARDPAY)->setReturnUrl("http://myurl.se")->getPaymentForm(); $xmlMessage = new \SimpleXMLElement($form->xmlMessage); $this->assertEquals('1', $xmlMessage->orderrows->row[0]->description); $this->assertEquals('2', $xmlMessage->orderrows->row[1]->description); $this->assertEquals('3', $xmlMessage->orderrows->row[2]->description); $this->assertEquals('4', $xmlMessage->orderrows->row[3]->description); }
public function test_fixedDiscount_amount_with_calculated_vat_rate_incvat_creates_discount_rows_using_incvat_and_vatpercent() { $order = WebPay::createOrder(Svea\SveaConfig::getDefaultConfig())->addCustomerDetails(WebPayItem::individualCustomer()->setNationalIdNumber(194605092222))->setCountryCode("SE")->setCustomerReference("33")->setOrderDate("2012-12-12")->setCurrency("SEK")->addOrderRow(WebPayItem::orderRow()->setAmountExVat(60.0)->setVatPercent(20)->setQuantity(1)->setName("exvatRow"))->addOrderRow(WebPayItem::orderRow()->setAmountExVat(30.0)->setVatPercent(10)->setQuantity(1)->setName("exvatRow2"))->addFee(WebPayItem::invoiceFee()->setAmountExVat(8.0)->setVatPercent(10)->setName("exvatInvoiceFee"))->addFee(WebPayItem::shippingFee()->setAmountExVat(16.0)->setVatPercent(10)->setName("exvatShippingFee"))->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(10.0)->setDiscountId("TenCrownsOff")->setName("fixedDiscount: 10 off incvat")); $request = $order->useInvoicePayment()->prepareRequest(); // all order rows $this->assertEquals(60.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertEquals(30.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); // all invoice fee rows $this->assertEquals(8.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent); // all shipping fee rows $this->assertEquals(16.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent); // all discount rows // expected: fixedDiscount: 10 off incvat, order row amount are 66% at 20% vat, 33% at 10% vat // 1.2*0.66x + 1.1*0.33x = 10 => x = 8.6580 => 5.7143 @20% and 2.8571 @10% = $this->assertEquals(-5.7142857142857, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent); $this->assertEquals(-2.8571428571429, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][5]->VatPercent); }
public function testCalculateRequestValues_CorrectTotalAmount_WithFixedDiscountExVatAndIncVat_WithDifferentVatRatesPresent() { $config = SveaConfig::getDefaultConfig(); $order = \WebPay::createOrder($config); $order->addOrderRow(\WebPayItem::orderRow()->setAmountExVat(100.0)->setVatPercent(25)->setQuantity(2))->addOrderRow(\WebPayItem::orderRow()->setAmountExVat(100.0)->setVatPercent(6)->setQuantity(1))->addDiscount(\WebPayItem::fixedDiscount()->setAmountExVat(80.0)->setAmountIncVat(100.0)); // follows HostedPayment calculateRequestValues() outline: $formatter = new HostedRowFormatter(); $request = array(); $request['rows'] = $formatter->formatRows($order); $request['amount'] = $formatter->formatTotalAmount($request['rows']); $request['totalVat'] = $formatter->formatTotalVat($request['rows']); $this->assertEquals(25600, $request['amount']); // 35600 - 10000 discount $this->assertEquals(3600, $request['totalVat']); // 5600 - 2000 discount }
public function test_incvat_order_with_fixedDiscount_as_incvat_and_vatpercent_has_priceIncludingVat_true() { $order = FixedDiscountRowsIntegrationTest::create_only_incvat_order_and_fee_rows_order(); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(11.0)->setVatPercent(10)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 11i@10%")); $request = $order->useInvoicePayment()->prepareRequest(); // all order rows $this->assertEquals(72.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(33.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat); // all invoice fee rows $this->assertEquals(8.800000000000001, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat); // all shipping fee rows $this->assertEquals(17.6, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat); // all discount rows // expected: fixedDiscount: 11incvat @10% = -11.00 $this->assertEquals(-11.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat); $this->assertFalse(isset($request->request->CreateOrderInformation->OrderRows['OrderRow'][5])); // check that service accepts order $response = $order->useInvoicePayment()->doRequest(); $this->assertEquals(true, $response->accepted); // r() is round($val, 2, PHP_ROUND_HALF_EVEN), i.e. bankers rounding // r(72*1) + r(33*1) + r(17.60*1) + r(8.80*1) + r(-5.72*1.20*1) + r(-2.85*1.10*1) => 72.00+33.00+17.60+8.80-11.00 => 120.40 $this->assertEquals("120.40", $response->amount); }
/** * From a given total discount value, mean tax rate & an array of tax rates, * this functions returns an array of FixedDiscount objects representing the * discount split across the given tax rates. The FixedDiscount rows are set * using setAmountIncVat & setVatPercent. * * Note: this function is limited to one or two given tax rates at most. For * a mean tax rate of zero, a single discount row is returned. */ static function splitMeanAcrossTaxRates($discountAmount, $discountMeanVat, $discountName, $discountDescription, $allowedTaxRates, $amountExVatFlag = true) { $fixedDiscounts = array(); if ($discountMeanVat > 0) { if (sizeof($allowedTaxRates) == 2) { // m = $discountMeanVat // r0 = allowedTaxRates[0]; r1 = allowedTaxRates[1] // m = a r0 + b r1 => m = a r0 + (1-a) r1 => m = (r0-r1) a + r1 => a = (m-r1)/(r0-r1) // d = $discountAmountExVat; // d = d (a+b) => 1 = a+b => b = 1-a $a = ($discountMeanVat - $allowedTaxRates[1]) / ($allowedTaxRates[0] - $allowedTaxRates[1]); $b = 1 - $a; $discountAAmount = $discountAmount * $a * ($amountExVatFlag ? 1 + $allowedTaxRates[0] / 100.0 : (1 + $allowedTaxRates[0] / 100.0) / (1 + $discountMeanVat / 100.0)); $discountA = \WebPayItem::fixedDiscount()->setAmountIncVat(Helper::bround($discountAAmount, 2))->setVatPercent($allowedTaxRates[0])->setName(isset($discountName) ? $discountName : "")->setDescription((isset($discountDescription) ? $discountDescription : "") . ' (' . $allowedTaxRates[0] . '%)'); $discountBAmount = $discountAmount * $b * ($amountExVatFlag ? 1 + $allowedTaxRates[1] / 100.0 : (1 + $allowedTaxRates[1] / 100.0) / (1 + $discountMeanVat / 100.0)); $discountB = \WebPayItem::fixedDiscount()->setAmountIncVat(Helper::bround($discountBAmount, 2))->setVatPercent($allowedTaxRates[1])->setName(isset($discountName) ? $discountName : "")->setDescription((isset($discountDescription) ? $discountDescription : "") . ' (' . $allowedTaxRates[1] . '%)'); $fixedDiscounts[] = $discountA; $fixedDiscounts[] = $discountB; } elseif (sizeof($allowedTaxRates) == 1) { $discountIncVat = $discountAmount * ($amountExVatFlag ? 1 + $discountMeanVat / 100.0 : 1.0); // get amount inc vat if needed $discountA = \WebPayItem::fixedDiscount()->setAmountIncVat(Helper::bround($discountIncVat, 2))->setVatPercent($allowedTaxRates[0])->setName(isset($discountName) ? $discountName : "")->setDescription(isset($discountDescription) ? $discountDescription : ""); $fixedDiscounts[] = $discountA; } } else { $discount = \WebPayItem::fixedDiscount()->setAmountIncVat(Helper::bround($discountAmount, 2))->setVatPercent(0.0)->setName(isset($discountName) ? $discountName : "")->setDescription(isset($discountDescription) ? $discountDescription : ""); $fixedDiscounts[] = $discount; } return $fixedDiscounts; }
public function test_incvat_order_with_fixedDiscount_as_incvat_and_vatpercent_has_priceIncludingVat_true() { $order = FixedDiscountRowsTest::create_only_incvat_order_and_fee_rows_order(); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(11.0)->setVatPercent(10)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 11i@10%")); $request = $order->useInvoicePayment()->prepareRequest(); // all order rows $this->assertEquals(72.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(20, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(33.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PriceIncludingVat); // all invoice fee rows $this->assertEquals(8.800000000000001, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][2]->PriceIncludingVat); // all shipping fee rows $this->assertEquals(17.6, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][3]->PriceIncludingVat); // all discount rows // expected: fixedDiscount: 11incvat @10% = -11.00 $this->assertEquals(-11.0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PricePerUnit); $this->assertEquals(10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->VatPercent); $this->assertEquals(true, $request->request->CreateOrderInformation->OrderRows['OrderRow'][4]->PriceIncludingVat); $this->assertFalse(isset($request->request->CreateOrderInformation->OrderRows['OrderRow'][5])); // See file IntegrationTest/WebService/Payment/FixedDiscountRowsIntegrationTest for service response tests. }
public function testDiscountSetAsExVatAndVatPercentWhenPriceSetAsIncVatAndVatPercent() { $config = Svea\SveaConfig::getDefaultConfig(); $campaign = $this->getGetPaymentPlanParamsForTesting(); $request = WebPay::createOrder($config)->addOrderRow(WebPayItem::orderRow()->setAmountIncVat(123.9876)->setVatPercent(24)->setQuantity(1))->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(10)->setVatPercent(0))->addCustomerDetails(TestUtil::createIndividualCustomer("SE"))->setCountryCode("SE")->setOrderDate("2012-12-12")->usePaymentPlanPayment($campaign)->prepareRequest(); $this->assertEquals(123.9876, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PricePerUnit); $this->assertEquals(24, $request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->VatPercent); $this->assertTrue($request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); $this->assertEquals(-10, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->PricePerUnit); $this->assertEquals(0, $request->request->CreateOrderInformation->OrderRows['OrderRow'][1]->VatPercent); $this->assertTrue($request->request->CreateOrderInformation->OrderRows['OrderRow'][0]->PriceIncludingVat); }