1. Go to this page and download the library: Download sveaekonomi/webpay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sveaekonomi / webpay example snippets
//Include the library
\WebPay\WebPay;
use Svea\WebPay\WebPayItem;
use Svea\WebPay\Config\ConfigurationService;
//Display all errors in case something is wrong
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Get configuration object holding the Svea service login credentials
$myConfig = ConfigurationService::getDefaultConfig();
// We assume that you've collected the following information about the order in your shop:
// The shop cart contains one item "Billy" which cost 700,99 kr excluding vat (25%).
// When selecting to pay using the invoice payment method, the customer has also provided their social security number, which is ::orderRow();
$boughtItem->setDescription("Billy");
$boughtItem->setAmountExVat(700.99);
$boughtItem->setVatPercent(25);
$boughtItem->setQuantity(1);
// Add the order rows to the order:
$myOrder->addOrderRow($boughtItem);
// Next, we create a customer identity object, for invoice orders Svea will look up the customer address et al based on the social security number
$customerInformation = WebPayItem::individualCustomer();
$customerInformation->setNationalIdNumber("194605092222");
// Add the customer to the order:
$myOrder->addCustomerDetails($customerInformation);
// If a stronger authentication is needed (invoice to SE for example) we need to tell where to redirect after successful/rejected authentication
$myOrder->setIdentificationConfirmationUrl('https://mydomain.com/successful-authentication');
$myOrder->setIdentificationRejectionUrl('https://mydomain.com/rejected-authentication');
// We have now completed specifying the order, and wish to send the payment request to Svea. To do so, we first select the invoice payment method:
$myInvoiceOrderRequest = $myOrder->useInvoicePayment();
// Then send the request to Svea using the doRequest method, and immediately receive a service response object back
$myResponse = $myInvoiceOrderRequest->doRequest();
// If the response attribute accepted is true, the payment succeeded.
if($myResponse->accepted == true)
{
echo "Invoice payment succeeded!";
};
// The response also contains a customerIdentity object containing the invoice address of the customer, which should match the order shipping address.
print_r($myResponse->customerIdentity);
...
$myOrderRow = WebPayItem::orderRow(); // create the order row object
$myOrderRow->setQuantity(1); // nt(25); // recommended to specify price using AmountExVat & VatPercent
$myOrder->addOrderRow($myOrderRow); // add order row to the order
...
/* the same code expressed in a more compact, fluent style:
$myOrder->addOrderRow( WebPayItem::orderRow()->setQuantity(1)->setAmountExVat(10.00)->setVatPercent(25) );
*/
...
$order
...
->setCountryCode("SE") // t, direct bank & PayPage payments. Ignored for invoice and payment plan.
->setClientOrderNumber("A123456") // Required for card payment, direct payment, Unique String(65). Optional for Invoice and Payment plan String(32).
->setCustomerReference("att: kgm") // Optional for invoice and payment plan String(32), ignored for card & direct bank orders.
->setOrderDate("2012-12-12") //
...
// fetch all available campaigns from Svea
$campaignsRequest = WebPay::getPaymentPlanParams($config);
$campaignsRequest->setCountryCode("SE");
$campaignsResponse = $campaignsRequest->doRequest();
// we pick the first available campaign from the response
$campaign = $campaignsResponse->campaignCodes[0]->campaignCode;
// create the order
$order = WebPay::createOrder($config);
$order
->addOrderRow( ...
->addCustomerDetails( ...
->setCountryCode("SE")
->setOrderDate("2012-12-12")
;
// send the request, using the first available campaign with the payment plan payment method
$request = $order->usePaymentPlanPayment($campaign)
$response = $request->doRequest();
...
...
$form = $order
->usePaymentMethod(PaymentMethod::SVEACARDPAY) // Card payment, get available providers using WebPay::listPaymentMethods()
->setReturnUrl("http://myurl.se") // Required
->setCancelUrl("http://myurl.se") // Optional
->setCardPageLanguage("sv") // Optional, languageCode As ISO639, eg. "en", default english
->getPaymentForm();
...
...
$form = $order
->addCustomerDetails(
...
->setIpAddress() // Required
...
->usePaymentMethod(PaymentMethod::SVEACARDPAY) // Card payment, get available providers using WebPay::listPaymentMethods()
->setReturnUrl("http://myurl.se") // Required
->setCancelUrl("http://myurl.se") // Optional
->setCardPageLanguage("sv") // Required, languageCode As ISO639, eg. "en", default english
->getPaymentUrl();
...
...
$form = $order
->usePaymentMethod(PaymentMethod::NORDEA_SE) // Direct bank payment, get available banks using WebPay::listPaymentMethods()
->setReturnUrl("http://myurl.se") // Required
->setCancelUrl("http://myurl.se") // Optional
->setCardPageLanguage("sv") // Optional, languageCode As ISO639, eg. "en", default english
->getPaymentForm();
...
...
$form = $order
->usePaymentMethod(PaymentMethod::SVEACARDPAY) // Use WebPay::listPaymentMethods() to get available payment methods
->setReturnUrl("http://myurl.se") // Required
->setCancelUrl("http://myurl.se") // Optional
->setCardPageLanguage("sv") // Optional, languageCode As ISO639, eg. "en", default english
->getPaymentForm();
...
$orderBuilder->setCountryCode('SE')// customer country, we recommend basing this on the customer billing address
->setCurrency('SEK')
->setClientOrderNumber('123-das-555-32')// 123&svea_order=123') //ags::HIDENOTYOU) // optional, hides certain elements in the iframe
->setMerchantData("\"Newsletter\":\"true\"") // optional, sets data on order which is not displayed to end-user but is available in backend
->setLocale('sv-Se'); //
WebPayItem::fixedDiscount()->setAmountIncVat(10); // this is 10 kr for example, not percent
// create and add items to order
$firstBoughtItem = WebPayItem::orderRow()
->setAmountIncVat(100.00)// - l. Checkout orders only. Will not be applicable for other order types.
->setMerchantData('gray') // optional. Checkout orders only. Set data which is not displayed to end-users but is available in the backend
->setName('Fork');
$secondBoughtItem = WebPayItem::orderRow()
->setAmountIncVat(10.00)
->setVatPercent(12)
->setQuantity(2)
->setDescription('Korv med bröd')
->setArticleNumber('321')
->setTemporaryReference('231') // optional. Checkout orders only. Will not be applicable for other order types.
->setMerchantData('tasty') // optional. Checkout orders only. Set data which is not displayed to end-users but is available in the backend
->setName('Fork');
$discountItem = WebPayItem::fixedDiscount()
->setName('Promo coupon')
->setVatPercent(10)
->setAmountIncVat(20);
$shippingItem = WebPayItem::shippingFee()
->setAmountIncVat(17.60)
->setVatPercent(10)
->setName('incvatShippingFee');
$orderBuilder->addOrderRow($firstBoughtItem);
$orderBuilder->addOrderRow($secondBoughtItem);
$orderBuilder->addDiscount($discountItem);
$orderBuilder->addFee($shippingItem);
$orderBuilder = WebPay::checkout($myConfig);
$orderBuilder->setCheckoutOrderId(5)
->setCountryCode('SE');
// create and add items to order
$firstBoughtItem = WebPayItem::orderRow()
->setAmountExVat(10.99)
->setVatPercent(25)
->setQuantity(1)
->setDescription("Billy")
->setArticleNumber("123456789A")
->setTemporaryReference('230') // optional. Checkout orders only. Will not be applicable for other order types.
->setMerchantData('hello') // optional. Checkout orders only. Set data which is not displayed to end-users but is available in the backend
->setName('Fork');
$secondBoughtItem = WebPayItem::orderRow()
->setAmountIncVat(5.00)
->setVatPercent(12)
->setQuantity(2)
->setDescription("Korv med bröd")
->setArticleNumber("123456789B")
->setTemporaryReference('231') // optional. Checkout orders only. Will not be applicable for other order types.
->setMerchantData('tasty') // optional. Checkout orders only. Set data which is not displayed to end-users but is available in the backend
->setName('Fork');
$orderBuilder->addOrderRow($firstBoughtItem);
$orderBuilder->addOrderRow($secondBoughtItem);
...
$config = ConfigurationService::getDefaultConfig();
$form = WebPay::createOrder($config)
->addOrderRow(/*add any orderrow*/)
->setCountryCode("SE") //Required, must be set to "SE" for Swish Payments
->setClientOrderNumber("33") //Required
->setCurrency("SEK") //Required, must be set to "SEK" for Swish Payments
->setPayerAlias("46701234567") //Required, set consumers mobile number
->usePaymentMethod(PaymentMethod::SWISH) //Set payment method to SWISH
->setReturnUrl("http://myurl.se") //Required, this is where the user will be redirected after a successful payment
->setCallbackUrl("http://myurl.se/callback") //Optional, however VERY recommended as redirects from Swish to returnUrl might fail
->getPaymentForm();
...
...
$orderrow = WebPayItem::orderRow()
->setAmountExVat(100.00) // optional, Float, recommended, use precisely two of the price specification methods
->setVatPercent(25) // optional, Integer, recommended, use precisely two of the price specification methods
->setAmountIncVat(125.00) // optional, Float, use precisely two of the price specification methods
->setQuantity(2) //
->setArticleNumber("1") // optional, String(10) for invoice and payment plan, String (256) for card and direct
->setDiscountPercent(0) // optional
;
...
...
$shippingFee = WebPayItem::shippingFee()
->setAmountExVat(100.00) // optional, Float, recommended, use precisely two of the price specification methods
->setVatPercent(25) // optional, Integer, recommended, use precisely two of the price specification methods
->setAmountIncVat(125.00) // optional, Float, use precisely two of the price specification methods
->setUnit("pcs") // optional, String(3) for invoice and payment plan, String(64) for card and direct bank
->setName('name') // optional, invoice & payment plan orders will merge "name" with "description", String(256) for card and direct
->setDescription("description") // optional, String(40) for invoice & payment plan orders will merge "name" with "description" , String(512) for card and direct
->setShippingId('33') // optional
->setDiscountPercent(0)
; // optional
...
...
$invoiceFee = WebPayItem::invoiceFee()
->setAmountExVat(100.00) // optional, Float, recommended, use precisely two of the price specification methods
->setVatPercent(25) // optional, Integer, recommended, use precisely two of the price specification methods
->setAmountIncVat(125.00) // optional, Float, use precisely two of the price specification methods
->setUnit("pcs") // optional, String(3)
->setName('name') // optional, will merge "name" with "description"
->setDescription("description") // optional, String(40) will merge "name" with "description"
->setDiscountPercent(0)
; // optional
...
...
$fixedDiscount = WebPayItem::fixedDiscount()
->setAmountIncVat(100.00) // optional, Float, use precisely two of the price specification methods
->setAmountExVat(1.0) // optional, Float, recommended, use precisely two of the price specification methods
->setVatPercent(25) // optional, Integer, recommended, use precisely two of the price specification methods
->setDiscountId("1") // optional
->setUnit("st") // optional
->setName("Fixed") // optional, invoice & payment plan orders will merge "name" with "description", String(256) for card and direct
->setDescription("FixedDiscount"); // optional, String(40) for invoice & payment plan orders will merge "name" with "description" , String(512) for card and direct
...
...
$relativeDiscount = WebPayItem::relativeDiscount()
->setDiscountPercent(10.0) // ->setName("DiscountName") // optional, invoice & payment plan orders will merge "name" with "description", String(256) for card and direct
->setDescription("DiscountDesc.") // optional, String(40) for invoice & payment plan orders will merge "name" with "description" , String(512) for card and direct
;
...
...
$myNumberedOrderRow = WebPayItem::numberedOrderRow() //inherited from OrderRow
->setAmountExVat(100.00) // recommended to specify price using AmountExVat & VatPercent
->setVatPercent(25) // recommended to specify price using AmountExVat & VatPercent
->setAmountIncVat(125.00) // optional, need to use two out of three of the price specification methods
->setQuantity(2) // eric) //
...
$order = WebPay::createOrder($config)
->addOrderRow($orderrow) // fee
->addDiscount($discount) // optional, see WebPayItem for fixed, relative discount
->addCustomerDetails($customer) // yments
->setCurrency("SEK") // bank orders.
->setPeppolId("1234:abcd1234") // Optional for invoices where customer type is company, if set the invoice will be sent via PEPPOL
;
...
...
$request = WebPay::deliverOrder($config)
->setOrderId() // invoice, payment plan and accountCredit only - InvoiceDistributionType() // invoice only and accountCredit, :deliverOrderRows instead
->setCreditInvoice() // deprecated, optional -- use WebPayAdmin::creditOrderRows instead
;
// then select the corresponding request class and send request
$response = $request->deliverInvoiceOrder()->doRequest(); // returns DeliverOrdersResponse (no rows) or DeliverOrderResult (with rows)
$response = $request->deliverPaymentPlanOrder()->doRequest(); // returns DeliverOrdersResponse (no rows) or DeliverOrderResult (with rows)
$response = $request->deliverCardOrder()->doRequest(); // returns ConfirmTransactionResponse
$response = $request->deliverAccountCreditOrder()->doRequest(); // returns DeliverOrderResponse
...
dd namespaces
use Svea\WebPay\WebPay;
use Svea\WebPay\Config\ConfigurationService;
use Svea\WebPay\Constant\DistributionType;
//Display all errors in case something is wrong
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Get configuration object holding the Svea service login credentials
$myConfig = ConfigurationService::getDefaultConfig();
// We assume that you've previously run the firstorder.php file and successfully made a createOrder request to Svea using the invoice payment method.
$mySveaOrderId = "123456";
// Begin the order creation process by creating an order builder object using the WebPay::createOrder() method:
$myOrder = WebPay::deliverOrder($myConfig);
// We then add information to the order object by using the various methods in the Svea\DeliverOrderBuilder class.
// We begin by adding any additional information
...
$request = WebPay::getAddresses($config)
->setCountryCode() // mber etc. used to identify customer
->setOrderTypeInvoice() // deprecated -- method that corresponds to the ConfigurationProvider account credentials used
->setOrderTypePaymentPlan() // deprecated -- method that corresponds to the ConfigurationProvider account credentials used
;
// then select the corresponding request class and send request
$response = $request->getIndividualAddresses()->doRequest(); // returns GetAddressesResponse
$response = $request->getCompanyAddresses()->doRequest(); // returns GetAddressesResponse
...
$myGetAddressResponse = WebPay::getAddresses($myConfig);
// GetAddressResponse attributes:
$myGetAddressResponse->accepted; // Boolean // true iff request was accepted
$myGetAddressResponse->resultcode; // String // set iff accepted false
$myGetAddressResponse->errormessage; // String // set iff accepted false
$myGetAddressResponse->customerIdentity; // Array of GetAddressIdentity
$firstCustomerAddress = $myGetAddressesResponse->customerIdentity[0];
// GetAddressIdentity attributes:
$firstCustomerAddress->customerType; // String // "Person" or "Business" for individual and company customers, respectively
$firstCustomerAddress->nationalIdNumber; // Numeric // national id number of individual or company
$firstCustomerAddress->fullName; // String // amalgamated firstName and surname for individual, or company name for company customers
$firstCustomerAddress->coAddress; // String // optional
$firstCustomerAddress->street; // String //
$request = WebPay::getAddresses($config)
->setCountryCode() // t number etc. used to identify customer
->setOrderTypeInvoice() // deprecated -- method that corresponds to the ConfigurationProvider account credentials used
->setOrderTypePaymentPlan() // deprecated -- method that corresponds to the ConfigurationProvider account credentials used
;
// then select the corresponding request class and send request
$response = $request->getIndividualAddresses()->doRequest(); // returns GetAddressesResponse
$response = $request->getCompanyAddresses()->doRequest(); // returns GetAddressesResponse
$response = WebPay::getAddresses( $config )
->setCountryCode("SE") // Required -- supply the country code that corresponds to the account credentials used
->setOrderTypeInvoice() // Required -- use invoice account credentials for getAddresses lookup
->setCustomerIdentifier("194605092222") // Required -- lookup the address
->getIndividualAddresses() // get private individual address
->doRequest();
;
...
$response->accepted // true if request was accepted by the service
$response->errormessage // may be set if accepted above is false
$response->resultcode // 27xxx, reason
$response->campaignCodes[] // array of all available campaign payment plans in an array
->campaignCode // numeric campaign code identifier
->description // localised description string
->paymentPlanType // human readable identifier (not guaranteed unique)
->contractLengthInMonths
->monthlyAnnuityFactor // pricePerMonth = price * monthlyAnnuityFactor + notificationFee
->initialFee
->notificationFee
->interestRatePercent
->numberOfInterestFreeMonths
->numberOfPaymentFreeMonths
->fromAmount // amount lower limit for plan availability
->toAmount // amount upper limit for plan availability
...
...
$request = WebPayAdmin::cancelOrderRows($config)
->setOrderId() // // , index of original order rows you wish to cancel
->addNumberedOrderRow() // request->cancelPaymentPlanOrderRows()->doRequest(); // returns CancelOrderRowsResponse
$response = $request->cancelCardOrderRows()->doRequest(); // returns LowerTransactionResponse
$response = $request->cancelCheckoutOrderRows()->doRequest(); // returns no content if response is success
...
...
$request = WebPayAdmin::creditOrderRows($config)
->setCheckoutOrderId() // delivery action
->setInvoiceId() // // the original order
->addCreditOrderRows() // optional
->setRowToCredit() // optional, index of one of the original order row you wish to credit
->setRowsToCredit() // optional, list of row indexes you wish to credit
->addNumberedOrderRow() // card and direct bank only, nkOrderRows()->doRequest(); // returns CreditTransactionResponse
$response = $request->creditCheckoutOrderRows()->doRequest();
$response = $request->creditAccountCreditOrderRows()->doRequest(); // returns CreditIOrderRowsResponse
...
...
$request = WebPayAdmin::addOrderRows($testConfig)
->setCheckoutOrderId() // Required for checkout orders
->setOrderId() // Required for invoice or payment plan
->setCountryCode()
->addOrderRow() // One or more, for checkout is limit only one row
->addOrderRows() // optional
//Finish by selecting the correct orderType and perform the request:
$response = $request->addInvoiceOrderRows()->doRequest();
$response = $request->addPaymentPlanOrderRows()->doRequest();
$response = $request->addCheckoutOrderRows()->doRequest();
...
...
$request = WebPayAdmin::updateOrderRows($config)
->setCheckoutOrderId() // // ex of original order row
// then select the corresponding request class and send request
$response = $request->updateInvoiceOrderRows()->doRequest(); // returns UpdateOrderRowsResponse
$response = $request->updatePaymentPlanOrderRows()->doRequest(); // returns UpdateOrderRowsResponse
$response = $request->updateCheckoutOrderRows()->doRequest();
...
...
$creditOrderRowsBuilder = WebPayAdmin::creditOrderRows($testConfig)
->setCheckoutOrderId($sveaCheckoutOrderId) // Set the checkout order id
->setDeliveryId(1) // t();
...
...
$creditOrderRowsBuilder = WebPayAdmin::creditOrderRows($testConfig)
->setCheckoutOrderId($sveaCheckoutOrderId)
->setDeliveryId(1) // edited order with new Order row");
$creditOrderRowsBuilder->addCreditOrderRow($myCreditRow);
$response = $creditOrderRowsBuilder->creditCheckoutOrderWithNewOrderRow()->doRequest();
...
$order->addDiscount( WebPayItem::fixedDiscount()
->setAmountIncVat(-0.25) // a negative discount shows up as a positive adjustment
->setVatPercent(0)
)
...
$order->addDiscount( WebPayItem::fixedDiscount()
->setAmountIncVat(-0.25) // a negative discount shows up as a positive adjustment
->setVatPercent(0)
)
...