PHP code example of sirumobile / siru-php-sdk

1. Go to this page and download the library: Download sirumobile/siru-php-sdk 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/ */

    

sirumobile / siru-php-sdk example snippets


# web/checkout.php

ure is used to sign outgoing messages and verify
 * responses from API. Replace $merchantId and $secret with your own credentials.
 */
$signature = new \Siru\Signature($merchantId, $secret);

/**
 * Siru\API is used to retrieve API specific classes and setting default values
 * for outgoing requests. It   ->set('basePrice', '5.00')
    ->set('redirectAfterSuccess', 'https://my-shop.com/checkout/success')
    ->set('redirectAfterFailure', 'https://my-shop.com/checkout/failure')
    ->set('redirectAfterCancel', 'https://my-shop.com/checkout/cancel')
    ->set('customerNumber', '0401234567')
    ->set('title', 'Concert ticket')
    ->set('description', 'Concert ticket to see an awesome band live')
    ->createPayment();
  
  header('location: ' . $transaction['redirect']);
  exit();

} catch(\Siru\Exception\TransportException $e) {
  echo "Unable to contact Payment API. Error was: " . $e->getMessage();

} catch(\Siru\Exception\ApiException $e) {
  echo "API request failed with error code " . $e->getCode() . ": " . $e->getMessage();
  foreach($e->getErrorStack() as $error) {
    echo $error . "<br />";
  }
}

# /web/checkout/success.php, failure.php or cancel.php
$signature = new \Siru\Signature($merchantId, $secret);

if(isset($_GET['siru_event']) == true) {
  if($signature->isNotificationAuthentic($_GET)) {
    // User was redirected from Siru payment page and query parameters are authentic
  }
}

// /web/checkout/callback.php
$signature = new \Siru\Signature($merchantId, $secret);

$entityBody = file_get_contents('php://input');
$entityBodyAsJson = json_decode($entityBody, true);

if($signature->isNotificationAuthentic($entityBodyAsJson)) {
  // Notification was sent by Siru Mobile and is authentic
}

/**
 * Imaginary example on calculating Signature without using \Siru\API.
 */
$paymentRequestFields = [
  // ... RT_FIELDS);
$paymentRequestFields['signature'] = $hash;
$paymentRequestJson = json_encode($paymentRequestFields);

// Send request using what ever HTTP
$response = $myHttpClient->send('https://staging.sirumobile.com', 'POST', $paymentRequestJson);

// Then you would check API response status, parse the JSON string in response body
// and redirect user to the payment page.