PHP code example of webmasterlv / swedbank-spp

1. Go to this page and download the library: Download webmasterlv/swedbank-spp 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/ */

    

webmasterlv / swedbank-spp example snippets


use Swedbank\SPP\Accreditation;

$accreditation = new Accreditation();
$accreditation -> setDev('devuser', 'devpass', 'http://dev.example.com/payments/');
$accreditation -> setProd('produser', 'prodpass', 'http://www.example.com/payments/');

use Swedbank\SPP\Merchant;

$merchant = new Merchant();
$merchant -> setRegion(Gateway::REGION_LAT);
$merchant -> setLanguage(Gateway::LANG_LAT);

use Swedbank\SPP\Gateway;

$gateway = new Gateway($accreditation, $merchant, Gateway::ENV_DEV);

$gateway -> setLogger($logger);

use Swedbank\SPP\Order;

$orderId = 12345;
$orderDescr = 'Payment for book';
$orderAmount = 10;

$order = new Order($orderId, $orderDescr, $orderAmount);

use Swedbank\SPP\Customer;

$customer = Customer::fromArray([
	'email' => '[email protected]',
	'first_name' => 'Jānis',
	'last_name' => 'Bērziņš',
	'city' => 'Rīga',
	'phone' => '+37120000000',
	'zip_code' => 'LV-1000',
	'address' => 'Pilsētas iela 10-21',
	'country' => 'LV'
]);

$customer -> setAnonymous(true)

use Swedbank\SPP\Payment\CreditCard;
use Swedbank\SPP\Payment\InternetBank;
use Swedbank\SPP\Payment\PayPal;

// Internet banking
$method	 = new InternetBank(InternetBank::SWEDBANK);

// PayPal
$method = new PayPal();

// Credit Card
$method = new CreditCard();

$result	 = $gateway -> createTransaction($order, $customer, $method);
if ($result -> isSuccess()) {
    // Save gateway order ID and payment method for later retrieval
    $referenceId = $result -> getReference();
    // Redirect customer to this URL
    $redirectUrl = $result -> getRedirectUrl(); // User redirect URL
    header("Location: ".$redirectUrl);
    exit;
} else {
    $errorCode = $result -> getStatus(); // Error code (see 5.2)
    $errorMessage = $result -> getMessage(); // Error message
    $internalCode = $result -> getRemoteStatus(); // Internal error code
}

$isExtended = false;

// Your internal order ID, this should always be present
$orderID = $_GET['_merchantRef'];

// The status returned from payment system. You should not rely on this! This is just to prematurely 
// indicate whether payment was successful or not. If you cannot detect transaction status at this moment,
// just redirect customer to hinted page. Available values are 'success' or 'fail'.
$status = $_GET['_banklinkStatus'];

// Reference ID. May not be present! Use $orderID to determine reference ID.
$referenceId = $_GET['dts_reference'];

// Assuming you now have $referenceId
// Assuming you have your Gateway instance ready

$method = new CreditCard(); // or PayPal() or InternetBank() depending on original payment method.

$status = $gateway -> getStatus($order, $customer, $method, $referenceId, $isExtended);

if ($status -> isSuccess()) {
    // Payment is successful!
    // Update data about your order accordingly.
}

$result = $gateway -> getPaymentInfo($reference, $paymethod, $order);
if ($result -> isSuccess()) {
    $data = $result -> getExtendedData();
    var_dump($data);
}

$result = $gateway -> setPassword('newpassword');

if ($result -> isSuccess()) {
    echo "Password changed to " . $result -> getNewPassword();
} else {
    echo $result -> getMessage();
}

$respond = true;
$result = $gateway -> validatePayment($respond);
if ($result->isSuccess()) {
    $order = $result -> getOrder();
    $orderId = $order -> getId(); // Your Order ID
}


use Swedbank\SPP\Payment\Helper;

$method = Helper::getMethodByCode('IB', 'SW');
// $method is instance of InternetBank class