vp-trading / chapa-laravel
Requires
- php: ^8.2
- illuminate/http: ^11.0 || ^12.0
- moneyphp/money: ^4.7
Requires (Dev)
- larastan/larastan: ^3.6
- laravel/pint: ^1.24
- orchestra/testbench: ^9.12.0 || ^10.1
- pestphp/pest: ^2.0 || ^3.0 || ^4.0
- phpunit/phpunit: ^10.5.35 || ^11.5.15
- rector/rector: ^2.1
README
Installation
To install the chapa-laravel
package, follow these steps:
-
Require the package via Composer:
composer require vp-trading/chapa-laravel
-
Publish the configuration, route, and migration files:
php artisan vendor:publish --provider="Vptrading\ChapaLaravel\ChapaServiceProvider"
The package comes with config, route, and migration files to help you get started quickly.
-
Run the migration:
php artisan migrate
-
Configure your
.env
file: Add your Chapa API keys:CHAPA_SECRET_KEY=your_chapa_secret_key_here CHAPA_CALLBACK_URL=/vp/chapa/webhook CHAPA_WEBHOOK_SECRET=54LZzhZbPs7yreuS6bAw74zS0KE7P4Mt1fiqRx7wOL8OdjUQHjBqsIpkpT2rm43S CHAPA_REF_PREFIX=vp_chapa_
-
Ready to use! You can now use the package in your Laravel application.
Usage
Before you start using this package you should know all amount must be put in cent values. The development of this package took standards from stripe. So for example if you want the amount be 100 Birr you will put 10000.
Here are some basic usage examples:
Initialize Payment
use Vptrading\ChapaLaravel\Facades\Chapa; use VpTrading\ChapaLaravel\ValueObjects\User; use VpTrading\ChapaLaravel\ValueObjects\Customization; use Money\Money; $response = Chapa::acceptPayment([ Money::ETB(10000), new User( firstName: 'John', lastName: 'Doe', email: 'johndoe@example.com', phoneNumber: '0912345678' ), route('return-url'), new Customization( title: 'VP Solutions', description: 'This package is working like a charm.', logo: 'https://vptrading.et/wp-content/uploads/2023/08/cropped-VP-Logo-Symbol-White.png' ) ]); // Redirect user to payment page return redirect($response->checkout_url);
The Customization in the acceptPayment() method is optional and can be left out.
Verify Payment
use Vptrading\ChapaLaravel\Facades\Chapa; $tx_ref = 'unique-tx-ref-123'; $verification = Chapa::verify($tx_ref); if ($verification->data['status'] === 'success') { // Payment was successful }
Refund
use Vptrading\ChapaLaravel\Facades\Chapa; use Money\Money; $chapaRef = "APfxLoHHsl1eD"; Chapa::refund($chapaRef, Money::ETB(10000), 'Client Requested Refund');
The amount and reason in the refund method are optional and can be left null.
Webhook
Chapa sends webhooks for all payment-related events to your application. This package provides a default webhook controller to handle these events. You can extend or override the default controller Vptrading\ChapaLaravel\Http\Controllers\WebhookController::class
to implement custom logic for different webhook events.
To customize webhook handling, create your own controller and update the route in your application as needed.
Default route for receiving webhook is:
/vp/chapa/webhook
You can change the route by changing the CHAPA_CALLBACK_URL
env variable.
class WebhookController extends ChapaWebhookController { public function __invoke(Request $request) { parent::__invoke($request); // Handle your custom logic here. } }
🚀 And that's it. Do your thing and Give us a star if this helped you.🚀