sextanet/laravel-webpay

Laravel Webpay

1.0.0 2024-09-21 04:56 UTC

This package is auto-updated.

Last update: 2024-10-23 17:54:31 UTC


README

Laravel Webpay

Laravel, Transbank, Webpay and SextaNet products and logos are property of their respective companies.

The easiest way to use Webpay in your projects

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require sextanet/laravel-webpay

Publish and run migrations

php artisan vendor:publish --tag="webpay-migrations"
php artisan migrate

Copy these keys in your .env file

WEBPAY_IN_PRODUCTION=false
WEBPAY_COMMERCE_CODE=
WEBPAY_SECRET_KEY=
WEBPAY_DEBUG=true

Usage

With attached model (recommended)

// app/Models/YourModel.php

use SextaNet\LaravelWebpay\Traits\PayWithWebpay; // 👈 Import it

class YourModel
{
    // ...

    use HasFactory;
    
    use PayWithWebpay; // 👈 Use it!
}

By default, it uses these 3 fields which are required by Transbank:

If you use other names for your fields, no problem: you can make each model recognize them very easily using the following magic methods:

// app/Models/YourModel.php

public function getBuyOrderAttribute(): string
{
    return $this->id; // Give it your custom logic
}

public function getAmountAttribute(): string
{
    return $this->price; // Give it your custom logic: Don't need to use decimals
}

public function getSessionIdAttribute(): string
{
    return md5($this->id); // Give it your custom logic
}
// In your controller or equivalent

$order = YourOrder::where('id', 1)->first();

return $order->payWithWebpay(); // 👈 Done!

Easy peasy!

Setting custom URLs

For convenience, you can set custom pages for each status before calling payWithOrder() method

Cancelled

LaravelWebpay::setCancelledUrl('/cancelled-page');

Rejected

LaravelWebpay::setRejectedUrl('/rejected-page');

Testing cards

  • Expire date: (Any valid date)
  • RUT: 11111111-1
  • Password: 123

Source: Official Transbank Developers website

Production mode

When you are ready to be in production, you need to set WEBPAY_IN_PRODUCTION to true, and specify WEBPAY_COMMERCE_CODE and WEBPAY_SECRET_KEY.

Alternative usage

If you don't want to import Trait, you can create or instanciate a order, and then, calling LaravelWebpay::create($order) method, for example:

// In your controller or equivalent
$order = YourOrder::where('id', 1)->first();

// ❗️ Your order model needs to have: buy_order, session_id and amount fields
return LaravelWebpay::create($order);

License

The MIT License (MIT). Please see License File for more information.