Manage recurring payments with Redsys and Laravel. Laravel Redsys v2.0.
After months of development and testing, today we are releasing version 2.0 of the Laravel package, laravel-redsys
.
This new version has been completely rewritten to become the most comprehensive Laravel package for Redsys. It comes with the following features:
- Integration with redirection and REST methods
- Associating operations to Eloquent models
- Securely storing and associating cards with Eloquent models
- Automatic management of Redsys notifications (payment confirmation)
- Ability to create fully customized Redsys requests to implement any Redsys functionality
- Local payment page simulator for local testing
But most importantly, unlike v1, now it's ready to incorporate more features or updates to the Redsys API.
Another significant improvement compared to v1 is that it allows creating payments not associated to Eloquent models, covering all the scenarios that developers may encounter.
Creating a payment with Redsys is as simple as this:
// Prepare your model
use Creagia\LaravelRedsys\Concerns\CanCreateRedsysRequests;
use Creagia\LaravelRedsys\Contracts\RedsysPayable;
class YourModel extends Model implements RedsysPayable
{
use CanCreateRedsysRequests;
public function getTotalAmount(): int
{
return 199_99;
}
public function paidWithRedsys(): void
{
// Notify user, change status to paid, ...
}
}
// Create the request
use Creagia\Redsys\Enums\PayMethod;
$redsysRequest = $yourModel->createRedsysRequest(
productDescription: 'Product description',
payMethod: PayMethod::Card,
);
Remember that if you want to make a fully customized implementation, you can directly use our library redsys-php
, which has also been recently updated to a new version.
Next, let's go over a couple of important enhancements in this new version.
Operations not related to Eloquent models
Although the package documentation provides detailed information on how to associate operations with Eloquent models, this new version allows creating payments or operations not related to models too.
use Creagia\LaravelRedsys\RequestBuilder;
$redsysRequestBuilder = RequestBuilder::newRequest(
new \Creagia\Redsys\Support\RequestParameters(
transactionType: \Creagia\Redsys\Enums\TransactionType::Autorizacion,
productDescription: 'Description',
amountInCents: 123_12,
currency: Currency::EUR,
payMethods: \Creagia\Redsys\Enums\PayMethod::Card,
)
);
This way, you can create fully customized operations by adding the desired parameters to the RequestParameters
object.
Credential-On-File (CoF) Operations or Reference Payments
A CoF operation is one where, once authorized, Redsys provides us with a reference token that we can use for further operations without customer interaction.
To create a CoF operation, you need to prepare the model that you want to associate with the card. This is usually a User
, Team
, or Subscription
model, for example.
use Creagia\LaravelRedsys\Concerns\InteractsWithRedsysCards;
class Team extends Model
{
use InteractsWithRedsysCards;
...
}
With this, we can make the initial request:
use Creagia\Redsys\Enums\CofType;
use Creagia\Redsys\Enums\PayMethod;
public function initialRequest()
{
$redsysRequest = $yourProductModel->createRedsysRequest(
productDescription: 'Product description',
payMethod: PayMethod::Card,
)->requestingCardToken(
CofType::Recurring
)->storeCardOnModel(
$yourPayingModel // User, Team, ...
);
return $redsysRequest->redirect();
}
Once Redsys confirms the operation, we can create new payments using the token:
use Creagia\Redsys\Enums\Currency;
use Creagia\Redsys\Enums\TransactionType;
use Creagia\LaravelRedsys\RequestBuilder;
use Illuminate\Database\Eloquent\Model;
use Creagia\Redsys\Enums\CofType;
public function renewSubscription(Model $yourProductModel, Model $yourPayingModel)
{
$redsysCard = $yourPayingModel->redsysCards->last(); // User, Team, ...
$redsysRequest = RequestBuilder::newRequest(new RequestParameters(
amountInCents: $yourProductModel->getTotalAmount(),
currency: Currency::EUR,
transactionType: TransactionType::Autorizacion,
))
->associateWithModel(
$yourProductModel
)->usingCard(
CofType::Recurring,
$redsysCard,
);
return $redsysRequest->post();
}