PHP code example of gridonic / swisscom-easypay

1. Go to this page and download the library: Download gridonic/swisscom-easypay 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/ */

    

gridonic / swisscom-easypay example snippets


use Gridonic\EasyPay\Environment\Environment;

$prodEnvironment = new Environment(Environment::ENV_PROD, 'my-merchant-id', 'my-secret-key')
$stagingEnvironment = new Environment(Environment::ENV_STAGING, 'my-merchant-id', 'my-secret-key')

use Gridonic\EasyPay\CheckoutPage\CheckoutPageItem;
use Gridonic\EasyPay\CheckoutPage\CheckoutPageService;

// Map the user's shopping cart to a CheckoutPageItem
$checkoutPageItem = new CheckoutPageItem();
$checkoutPageItem
    ->setTitle('A mandatory title displayed on the checkout page')
    ->setDescription('A mandatory description displayed on the checkout page')
    ->setPaymentInfo('Mandatory payment information, visible on the invoice of the customer')
    ->setAmount('99.90')
    ->setSuccessUrl('https://myshop.com/return')
    ->setErrorUrl('https://myshop.com/return')
    ->setCancelUrl('https://myshop.com/cancel');

// Get the checkout page redirect URL
$checkoutPageService = CheckoutPageService::create($environment);
$redirectUrl = $checkoutPageService->getCheckoutPageUrl($checkoutPageItem);

use Gridonic\EasyPay\CheckoutPage\CheckoutPageResponse;

// Create an instance from the available GET parameters
$checkoutPageResponse = CheckoutPageResponse::createFromGet();

if ($checkoutPageResponse->isSuccess()) {
    $paymentId = $checkoutPageResponse->getPaymentId();
    
    // or if the submitted CheckoutPageItem is a subscription (recurrent service)
    $authSubscriptionId = $checkoutPageResponse->getAuthSubscriptionId();
} else {
    print_r($checkoutPageResponse->getErrorCode());
}

use Gridonic\EasyPay\REST\RESTApiService;

$restApiService = RESTApiService::create($environment);

// Commit a direct payment
$directPaymentResponse = $restApiService->directPayment('paymentId');

if ($directPaymentResponse->isSuccess()) {
    // Payment commited successfully
} else {
    // A more detailed error is available as error message:
    $errorMessages = $directPaymentResponse->getErrorMessages();
    $errorMessage = array_pop($errorMessages);
    $errorMessage->getMessage();
    $errorMessage->getCode();
    $errorMessage->getField();
    $errorMessage->getRequestId();
}

// Authorize a subscription
$authSubscriptionResponse = $restApiService->authorizeSubscription('authSubscriptionId');

if ($authSubscriptionResponse->isSuccess()) {
    // Subscription authorized successfully
} else {
    $errorMessages = $authSubscriptionResponse->getErrorMessages();
    $errorMessage = array_pop($errorMessages);
    // ...accessing the error details is identical to the direct payment example above
}