sextanet / laravel-oneclick
Laravel Oneclick
Fund package maintenance!
Requires
- php: ^8.2|^8.3
- illuminate/contracts: ^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
- transbank/transbank-sdk: ^5.1
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel, Transbank, Webpay and SextaNet products and logos are property of their respective companies.
The easiest way to use Oneclick in your projects
Installation
You can install the package via composer:
composer require sextanet/laravel-oneclick
Publish and run migrations
php artisan vendor:publish --tag="oneclick-migrations"
php artisan migrate
Copy these keys in your .env file
ONECLICK_IN_PRODUCTION=false ONECLICK_API_KEY= ONECLICK_COMMERCE_CODE= ONECLICK_MALL_CODE= ONECLICK_DEBUG=true
Usage
Registering a card
Import the trait in your User or equivalent model
use SextaNet\LaravelOneclick\Traits\HasOneclick; class User // extends ... { use HasOneclick; }
Now, you can do that:
$user = User::find(2); return $user->registerCardOneclick();
Pay with registered card
Import the trait in your Order or equivalent model
use SextaNet\LaravelOneclick\Traits\PayWithOneclick; class Order extends Model { use PayWithOneclick; }
Now, you can do that:
$oneclick_card = User::find(2)->oneclick_cards()->first(); $order = Order::findById(1); $order->payWithOneclick($oneclick_card);
Also, you can add installments by passing a second parameter:
$order->payWithOneclick($oneclick_card, 3);
For convenience, you can set custom pages for each status before calling payWithOneclick() method
Cancelled
LaravelOneclick::setCancelledUrl('/cancelled-page');
Rejected
LaravelOneclick::setRejectedUrl('/rejected-page');
Testing cards
| Type | Numbers | Result |
|---|---|---|
| VISA | 4051 8856 0044 6623 | Approved |
| Mastercard | 5186 0595 5959 0568 | Rejected |
| Redcompra | 4051 8842 3993 7763 | Approved |
| VISA Prepaid | 4051 8860 0005 6590 | Approved |
- Expire date: (Any valid date)
- RUT: 11111111-1
- Password: 123
Source: Official Transbank Developers website
Testing
To test Oneclick flows without hitting the real Transbank API, call LaravelOneclick::enableTests(). It returns a fluent builder to queue stub responses, and always reset the fake in afterEach.
use SextaNet\LaravelOneclick\LaravelOneclick; afterEach(fn () => LaravelOneclick::disableTests());
Registering a card (approved)
LaravelOneclick::enableTests() ->withInscriptionStart() ->withInscriptionFinishApproved(); $start = LaravelOneclick::instance() ->start($username, $email, $responseUrl); $finish = LaravelOneclick::instance() ->finish($start->getToken()); $finish->isApproved(); // true $finish->getTbkUser(); // 'b6bd6ba3-e718-4107-9386-d2b099a8dd42' $finish->getCardType(); // 'Visa' $finish->getCardNumber(); // 'XXXXXXXXXXXX6623'
You can also stub a rejected or cancelled registration:
LaravelOneclick::enableTests()->withInscriptionFinishRejected(); // response_code -1 LaravelOneclick::enableTests()->withInscriptionFinishCancelled(); // response_code -96
Authorizing a payment (approved)
LaravelOneclick::enableTests()->withTransactionAuthorizeApproved(); $response = LaravelOneclick::pay($username, $tbkUser, $parentBuyOrder, $details); $response->isApproved(); // true $response->getDetails()[0]->status; // 'AUTHORIZED'
Stub a rejected payment:
LaravelOneclick::enableTests()->withTransactionAuthorizeRejected();
Overriding stub data
Any stub method accepts an array of overrides merged into the response:
LaravelOneclick::enableTests()->withTransactionAuthorizeApproved([ 'details' => [['amount' => 9999]], ]);
Asserting all queued stubs were consumed
$fake = LaravelOneclick::enableTests() ->withInscriptionStart() ->withInscriptionFinishApproved(); // ... run code ... $fake->assertAllResponsesConsumed();
Integration
Official Laravel integration project
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_API_KEY.
License
The MIT License (MIT). Please see License File for more information.