public function test_paymentPlanPricePerMonth_returns_PaymentPlanPricePerMonth() { $campaigns = WebPay::getPaymentPlanParams(\Svea\SveaConfig::getDefaultConfig())->setCountryCode("SE")->doRequest(); $this->assertTrue($campaigns->accepted); $pricesPerMonth = \Svea\Helper::paymentPlanPricePerMonth(2000, $campaigns, true); $this->assertInstanceOf("Svea\\WebService\\PaymentPlanPricePerMonth", $pricesPerMonth); // $this->assertEquals(213060, $pricesPerMonth->values[0]['campaignCode']);//don't test to be flexible $this->assertEquals(2029, $pricesPerMonth->values[0]['pricePerMonth']); }
public function test_convertExVatToIncVat() { $this->assertEquals(10.0, WebServiceRowFormatter::convertExVatToIncVat(8.0, 25)); $this->assertEquals(69.98999999999999 * 1.25, WebServiceRowFormatter::convertExVatToIncVat(69.98999999999999, 25)); $this->assertEquals(0, WebServiceRowFormatter::convertExVatToIncVat(0, 0)); $this->assertEquals(1, WebServiceRowFormatter::convertExVatToIncVat(1, 0)); $this->assertEquals(0, WebServiceRowFormatter::convertExVatToIncVat(0, 25)); $this->assertEquals(100.0 * 1.06, WebServiceRowFormatter::convertExVatToIncVat(100.0, 6)); $this->assertEquals(\Svea\Helper::bround(100.0 * 1.0825, 2), WebServiceRowFormatter::convertExVatToIncVat(100.0, 8.25)); }
private function SetSoapClient($config) { $libraryProperties = \Svea\Helper::getSveaLibraryProperties(); $libraryName = $libraryProperties['library_name']; $libraryVersion = $libraryProperties['library_version']; $integrationProperties = \Svea\Helper::getSveaIntegrationProperties($config); $integrationPlatform = $integrationProperties['integration_platform']; $integrationCompany = $integrationProperties['integration_company']; $integrationVersion = $integrationProperties['integration_version']; $client = new \SoapClient($this->svea_server, array("trace" => 1, 'stream_context' => stream_context_create(array('http' => array('header' => 'X-Svea-Library-Name: ' . $libraryName . "\n" . 'X-Svea-Library-Version: ' . $libraryVersion . "\n" . 'X-Svea-Integration-Platform: ' . $integrationPlatform . "\n" . 'X-Svea-Integration-Company: ' . $integrationCompany . "\n" . 'X-Svea-Integration-Version: ' . $integrationVersion))))); return $client; }
protected function createRequestXml() { $XMLWriter = new \XMLWriter(); $XMLWriter->openMemory(); $XMLWriter->setIndent(true); $XMLWriter->startDocument("1.0", "UTF-8"); $XMLWriter->writeComment(\Svea\Helper::getLibraryAndPlatformPropertiesAsJson($this->config)); $XMLWriter->startElement($this->method); $XMLWriter->writeElement("merchantid", $this->config->getMerchantId(\ConfigurationProvider::HOSTED_TYPE, $this->countryCode)); $XMLWriter->endElement(); $XMLWriter->endDocument(); return $XMLWriter->flush(); }
/** returns xml for hosted webservice "annul" request */ protected function createRequestXml() { $XMLWriter = new \XMLWriter(); $XMLWriter->openMemory(); $XMLWriter->setIndent(true); $XMLWriter->startDocument("1.0", "UTF-8"); $XMLWriter->writeComment(\Svea\Helper::getLibraryAndPlatformPropertiesAsJson($this->config)); $XMLWriter->startElement($this->method); $XMLWriter->writeElement("transactionid", $this->transactionId); $XMLWriter->endElement(); $XMLWriter->endDocument(); return $XMLWriter->flush(); }
/** * When used from PHP, the SveaWebPay Administration Service requires some configuration. * getSoapClient() takes the config and eturns a SoapClient with a working set * of options, bypassing the server wsdl. * * @param ConfigurationProvider $config * @param string $orderType * @return SoapClient */ public function setSoapClient($config, $orderType) { $libraryProperties = \Svea\Helper::getSveaLibraryProperties(); $libraryName = $libraryProperties['library_name']; $libraryVersion = $libraryProperties['library_version']; $integrationProperties = \Svea\Helper::getSveaIntegrationProperties($config); $integrationPlatform = $integrationProperties['integration_platform']; $integrationCompany = $integrationProperties['integration_company']; $integrationVersion = $integrationProperties['integration_version']; $endpoint = $config->getEndPoint($orderType); $client = new \SoapClient(null, array('location' => $endpoint, 'uri' => "http://tempuri.org/", 'use' => SOAP_LITERAL, 'exceptions' => 1, 'connection_timeout' => 60, 'trace' => 1, 'soap_version' => SOAP_1_1, 'stream_context' => stream_context_create(array('http' => array('header' => 'X-Svea-Library-Name: ' . $libraryName . "\n" . 'X-Svea-Library-Version: ' . $libraryVersion . "\n" . 'X-Svea-Integration-Platform: ' . $integrationPlatform . "\n" . 'X-Svea-Integration-Company: ' . $integrationCompany . "\n" . 'X-Svea-Integration-Version: ' . $integrationVersion))))); return $client; }
protected function createRequestXml() { $XMLWriter = new \XMLWriter(); $XMLWriter->openMemory(); $XMLWriter->setIndent(true); $XMLWriter->startDocument("1.0", "UTF-8"); $XMLWriter->writeComment(\Svea\Helper::getLibraryAndPlatformPropertiesAsJson($this->config)); $XMLWriter->startElement($this->method); $XMLWriter->writeElement("amount", $this->amount); $XMLWriter->writeElement("customerrefno", $this->customerRefNo); $XMLWriter->writeElement("subscriptionid", $this->subscriptionId); if (isset($this->currency)) { $XMLWriter->writeElement("currency", $this->currency); } $XMLWriter->endElement(); $XMLWriter->endDocument(); return $XMLWriter->flush(); }
/** * Add Customer details to Svea CreateOrder object * * @param type $order * @param type $additionalInfo * @return type Svea CreateOrder object */ public function getSveaPaymentObject($order, $additionalInfo = null) { $svea = parent::getSveaPaymentObject($order, $additionalInfo); //Add more customer info $countryCode = $order->getBillingAddress()->getCountryId(); $company = $additionalInfo['svea_customerType']; $address = $order->getBillingAddress()->getStreetFull(); $addressArray = \Svea\Helper::splitStreetAddress($address); if ($company == "1") { $item = WebPayItem::companyCustomer(); $item = $item->setEmail($order->getBillingAddress()->getEmail())->setCompanyName($order->getBillingAddress()->getCompany())->setStreetAddress($addressArray[1], $addressArray[2])->setZipCode($order->getBillingAddress()->getPostcode())->setLocality($order->getBillingAddress()->getCity())->setIpAddress(Mage::helper('core/http')->getRemoteAddr(false))->setPhoneNumber($order->getBillingAddress()->getTelephone()); if ($countryCode == "DE" || $countryCode == "NL") { $item = $item->setVatNumber($additionalInfo['svea_vatNo']); } else { $item = $item->setNationalIdNumber($additionalInfo['svea_ssn']); $item = $item->setAddressSelector($additionalInfo['svea_addressSelector']); } } else { $item = WebPayItem::individualCustomer(); // Not all countries has svea_ssn input if (array_key_exists('svea_ssn', $additionalInfo)) { $item = $item->setNationalIdNumber($additionalInfo['svea_ssn']); } $item = $item->setEmail($order->getBillingAddress()->getEmail())->setName($order->getBillingAddress()->getFirstname(), $order->getBillingAddress()->getLastname())->setStreetAddress($addressArray[1], $addressArray[2])->setZipCode($order->getBillingAddress()->getPostcode())->setLocality($order->getBillingAddress()->getCity())->setIpAddress(Mage::helper('core/http')->getRemoteAddr(false))->setPhoneNumber($order->getBillingAddress()->getTelephone()); if ($countryCode == "DE" || $countryCode == "NL") { $validBirthday = true; foreach (array('svea_birthYear', 'svea_birthMonth', 'svea_birthDay') as $key) { if (!array_key_exists($key, $additionalInfo) || trim($additionalInfo[$key]) === "") { $validBirthday = false; } } if ($validBirthday) { $item = $item->setBirthDate($additionalInfo['svea_birthYear'], $additionalInfo['svea_birthMonth'], $additionalInfo['svea_birthDay']); } } if ($countryCode == "NL") { if (array_key_exists('svea_initials', $additionalInfo)) { $item = $item->setInitials($additionalInfo['svea_initials']); } } } // Set public key on the object if publicKey is set in additionalInfo if (array_key_exists('svea_publicKey', $additionalInfo)) { $publicKey = $additionalInfo['svea_publicKey']; if ($publicKey !== '') { $item = $item->setPublicKey($publicKey); // $item = $item->setAddressSelector(''); } } $svea = $svea->addCustomerDetails($item); return $svea; }
public function formatRelativeDiscountRows($row) { $tempRow = new HostedOrderRowBuilder(); if (isset($row->name)) { $tempRow->setName($row->name); } if (isset($row->description)) { $tempRow->setDescription($row->description); } if (isset($row->discountId)) { $tempRow->setSku($row->discountId); } if (isset($row->unit)) { $tempRow->setUnit($row->unit); } $rawAmount = $this->rawAmount / 100 * $row->discountPercent / 100; $rawVat = $this->rawVat / 100 * $row->discountPercent / 100; $tempRow->setAmount(-\Svea\Helper::bround($rawAmount, 2) * 100); $tempRow->setVat(-\Svea\Helper::bround($rawVat, 2) * 100); $tempRow->setQuantity(1); $this->totalAmount += $tempRow->amount; $this->totalVat += $tempRow->vat; $this->newRows[] = $tempRow; $this->discountAmount += $tempRow->amount; $this->discountVat += $tempRow->vat; }
public function test_incvat_order_with_fixedDiscount_as_incvat_only_has_priceIncludingVat_true() { $order = FixedDiscountRowsIntegrationTest::create_only_incvat_order_and_fee_rows_order(); $order->addDiscount(WebPayItem::fixedDiscount()->setAmountIncVat(10.0)->setDiscountId("fixedDiscount")->setName("fixedDiscount: 10i")); $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 incvat => split across 10i *(72/72+33) @20% + 10i *(33/72+33) @10% => 6.8571i @20% + 3.1428i @10% =>5.71 + 2.86 $this->assertEquals(-6.86, \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.14, \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*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-6.86-3.14 => 121.40 $this->assertEquals("121.40", $response->amount); }
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 ); }
private function formatFixedDiscountRows($row) { // only amountIncVat (i.e. amount) was specified: if (isset($row->amount) && !isset($row->vatPercent) && !isset($row->amountExVat)) { $this->newRows = array_merge($this->newRows, $this->formatFixedDiscountSpecifiedAsAmountIncVatOnly($row)); } // only amountExVat was specified: if (!isset($row->amount) && !isset($row->vatPercent) && isset($row->amountExVat)) { $this->newRows = array_merge($this->newRows, $this->formatFixedDiscountSpecifiedAsAmountExVatOnly($row)); } // amountIncVat (i.e. amount) and vatPercent is set, so we use that vatPercent: if (isset($row->amount) && isset($row->vatPercent) && !isset($row->amountExVat)) { $orderRow = new WebServiceSoap\SveaOrderRow(); if (isset($row->discountId)) { $orderRow->ArticleNumber = $row->discountId; } $orderRow->Description = $this->formatRowNameAndDescription($row); if (isset($row->unit)) { $orderRow->Unit = $row->unit; } $orderRow->DiscountPercent = 0; //no discount on discount $orderRow->NumberOfUnits = 1; //only one discount per row //calculate discount $vatRate = $row->vatPercent; $discountAtThisVatRateIncVat = $row->amount; // $discountAtThisVatRateExVat = // WebServiceRowFormatter::convertIncVatToExVat( $discountAtThisVatRateIncVat, $vatRate ); $orderRow->PricePerUnit = -1 * ($this->priceIncludingVat ? $discountAtThisVatRateIncVat : WebServiceRowFormatter::convertIncVatToExVat($row->amount, \Svea\Helper::bround($row->vatPercent))); $orderRow->VatPercent = $vatRate; $orderRow->PriceIncludingVat = $this->priceIncludingVat ? TRUE : FALSE; $this->newRows[] = $orderRow; } // amountExVat (i.e. amount) and vatPercent is set, so we use that vatPercent: if (!isset($row->amount) && isset($row->vatPercent) && isset($row->amountExVat)) { $orderRow = new WebServiceSoap\SveaOrderRow(); if (isset($row->discountId)) { $orderRow->ArticleNumber = $row->discountId; } $orderRow->Description = $this->formatRowNameAndDescription($row); if (isset($row->unit)) { $orderRow->Unit = $row->unit; } $orderRow->DiscountPercent = 0; //no discount on discount $orderRow->NumberOfUnits = 1; //only one discount per row //calculate discount $vatRate = $row->vatPercent; $discountAtThisVatRate = $this->priceIncludingVat ? WebServiceRowFormatter::convertExVatToIncVat($row->amountExVat, \Svea\Helper::bround($row->vatPercent)) : $row->amountExVat; $orderRow->PricePerUnit = -1 * $discountAtThisVatRate; $orderRow->VatPercent = $vatRate; $orderRow->PriceIncludingVat = $this->priceIncludingVat ? TRUE : FALSE; $this->newRows[] = $orderRow; } }
/** * Given a ConfigurationProvider, return a json string containing the Svea integration package (library) * and integration (from config) name, version et al. Used by HostedService requests. * @param ConfigurationProvider $config * @return string in json format */ static function getLibraryAndPlatformPropertiesAsJson($config) { $libraryProperties = \Svea\Helper::getSveaLibraryProperties(); $libraryName = $libraryProperties['library_name']; $libraryVersion = $libraryProperties['library_version']; $integrationProperties = \Svea\Helper::getSveaIntegrationProperties($config); $integrationPlatform = $integrationProperties['integration_platform']; $integrationCompany = $integrationProperties['integration_company']; $integrationVersion = $integrationProperties['integration_version']; $properties_json = '{' . '"X-Svea-Library-Name": "' . $libraryName . '", ' . '"X-Svea-Library-Version": "' . $libraryVersion . '", ' . '"X-Svea-Integration-Platform": "' . $integrationPlatform . '", ' . '"X-Svea-Integration-Company": "' . $integrationCompany . '", ' . '"X-Svea-Integration-Version": "' . $integrationVersion . '"' . '}'; return $properties_json; }
private function serializeOrderRow($orderRow) { $this->XMLWriter->startElement("row"); if (!empty($orderRow->sku) && $orderRow->sku != null) { $this->XMLWriter->writeElement("sku", $orderRow->sku); } else { $this->XMLWriter->writeElement("sku", ""); } if (!empty($orderRow->name) && $orderRow->name != null) { $this->XMLWriter->writeElement("name", $orderRow->name); } else { $this->XMLWriter->writeElement("name", ""); } if (!empty($orderRow->description) && $orderRow->description != null) { $this->XMLWriter->writeElement("description", $orderRow->description); } else { $this->XMLWriter->writeElement("description", ""); } if (!empty($orderRow->amount) && $orderRow->amount != null) { $this->XMLWriter->writeElement("amount", \Svea\Helper::bround($orderRow->amount)); } else { $this->XMLWriter->writeElement("amount", "0"); } if (!empty($orderRow->vat) && $orderRow->vat != null) { $this->XMLWriter->writeElement("vat", \Svea\Helper::bround($orderRow->vat)); } else { $this->XMLWriter->writeElement("vat", "0"); } if (!empty($orderRow->quantity) && $orderRow->quantity != null) { $this->XMLWriter->writeElement("quantity", $orderRow->quantity); } if (!empty($orderRow->unit) && $orderRow->unit != null) { $this->XMLWriter->writeElement("unit", $orderRow->unit); } $this->XMLWriter->endElement(); }
function calculateVatPercentFromVatAndAmount($vat, $amount) { $amountExVat = $amount - $vat; $unroundedVatPercent = $amountExVat > 0 ? $vat / $amountExVat : 0.0; // catch potential divide by zero $vatPercent = \Svea\Helper::bround($unroundedVatPercent, 2) * 100; // OrderRow has vatpercent as int. return $vatPercent; }
private function calculateTotalVatSumOfRows($row) { //if amount inc vat $sum = 0; //calculate the exvat sum if ($row->PriceIncludingVat == true) { $rowsum_incvat = $this->getRowAmount($row); $exvat = $this->convertIncVatToExVat($row, $rowsum_incvat); $vat = \Svea\Helper::bround($rowsum_incvat, 2) - \Svea\Helper::bround($exvat, 2); $sum += $vat; } else { $exvat = $this->getRowAmount($row); $vat = \Svea\Helper::bround($exvat, 2) * ($row->VatPercent / 100); $sum += \Svea\Helper::bround($vat, 2); } // $vat = \Svea\Helper::bround($exvat,2) * ($row->VatPercent / 100 ); // $sum += intval(100.00 * $vat) / 100.00; //php for .NET Math.Truncate -- round to nearest integer towards zero return $sum; }