PHP code example of creagia / redsys-php

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

    

creagia / redsys-php example snippets


use Creagia\Redsys\Enums\Currency;
use Creagia\Redsys\Enums\TransactionType;
use Creagia\Redsys\RedsysClient;
use Creagia\Redsys\RedsysRequest;
use Creagia\Redsys\Support\RequestParameters;

$redsysClient = new RedsysClient(
    merchantCode: env('redsys.merchantCode'),
    secretKey: env('redsys.key'),
    terminal: env('redsys.terminal'),
    environment: \Creagia\Redsys\Enums\Environment::Test,
);

$redsysRequest = RedsysRequest::create(
    $redsysClient,
    new RequestParameters(
        amountInCents: 123_45,
        orderNumber: '22013100005',
        currency: Currency::EUR,
        transactionType: TransactionType::Autorizacion,
        merchantUrl: 'https://example.com/redsysNotification',
        urlOk: 'https://example.com/paymentOk',
        urlKo: 'https://example.com/paymentKo',
    )
);

echo $redsysRequest->getRedirectFormHtml()

use Creagia\Redsys\RedsysRequest;
use Creagia\Redsys\Support\RequestParameters;
use Creagia\Redsys\Enums\CofType;

$redsysRequest = RedsysRequest::create(
    redsysClient: $redsysClient,
    requestParameters: new RequestParameters(...)
)->requestingCardToken(
    cofType: CofType::Recurring
);

echo $redsysRequest->getRedirectFormHtml()

use Creagia\Redsys\RedsysRequest;
use Creagia\Redsys\Support\RequestParameters;

$redsysRequest = RedsysRequest::create(
    redsysClient: $redsysClient,
    requestParameters: new RequestParameters(...)
)->usingCardToken(
    cofType: CofType::Recurring,
    cofTransactionId: $cofTransactionId,
    merchantIdentifier: $merchantIdentifier,
);

$response = $redsysRequest->sendPostRequest();

use Creagia\Redsys\Exceptions\DeniedRedsysPaymentResponseException;
use Creagia\Redsys\RedsysClient;
use Creagia\Redsys\RedsysResponse;

$redsysClient = new RedsysClient(
    merchantCode: env('redsys.merchantCode'),
    secretKey: env('redsys.key'),
    terminal: env('redsys.terminal'),
    environment: \Creagia\Redsys\Enums\Environment::Test,
);

$redsysNotification = new RedsysResponse($redsysClient);
$redsysNotification->setParametersFromResponse($_POST);

// If you need it, prior to checking response, you can use the decoded data from
// Redsys accessing the `NotificationParameters` object on `$redsysNotification->parameters`. 

try {
    $notificationData = $redsysNotification->checkResponse();
    // Authorised payment
} catch (DeniedRedsysPaymentResponseException $e) {
    $errorMessage = $e->getMessage();
    // Denied payment with $errorMessage
}

$redsysClient = new RedsysClient(
    merchantCode: env('redsys.merchantCode'),
    secretKey: env('redsys.key'),
    terminal: env('redsys.terminal'),
    environment: \Creagia\Redsys\Enums\Environment::Custom,
    customBaseUrl: 'https://localGateway.test',
);
bash
composer