bensondevs/laravel-midtrans

Laravel wrapper for Midtrans Payments — simple, clean integration for Snap, Core API, and subscription billing.

0.1.0-alpha 2025-06-15 09:05 UTC

This package is auto-updated.

Last update: 2025-06-15 10:22:15 UTC


README

Latest Version on Packagist Total Downloads

A modern, testable, and fluent Laravel wrapper for the Midtrans API.

Supports:

  • Snap payments
  • Subscription billing
  • Card registration
  • GoPay partner account creation
  • Laravel auto-discovery and configuration publishing

🚧 Project Status

This package is currently in alpha testing phase.

The development process has encountered several challenges, particularly with GoPay-related integration, due to inconsistencies and gaps in the official Midtrans documentation. Please expect some rough edges, especially around GoPay functionality.

Your feedback and contributions are highly appreciated as the package stabilizes.

🔧 Installation

You can install the package via Composer:

composer require bensondevs/laravel-midtrans:^0.1@alpha

If you want to publish the config file:

php artisan vendor:publish --tag=midtrans-config

⚙️ Configuration

After publishing, configure your .env:

MIDTRANS_SANDBOX=true
MIDTRANS_SANDBOX_CLIENT_KEY=your_sandbox_client_key
MIDTRANS_SANDBOX_SERVER_KEY=your_sandbox_server_key

MIDTRANS_PRODUCTION_CLIENT_KEY=your_production_client_key
MIDTRANS_PRODUCTION_SERVER_KEY=your_production_server_key

💳 Billable Trait

To make a model (like User) billable, use the Billable trait:

use Bensondevs\Midtrans\Models\Concerns\Billable;

class User extends Authenticatable
{
    use Billable;
}

This trait provides:

  • snapCharge(array $order)
  • refund(TransactionDetails $order, ?int $amount = null, ?string $reason = '')
  • subscribe(Subscribable $subscribable, PaymentType|string $paymentType, string $token)
  • gopaySubscribe(Subscribable $subscribable, GopayPaymentOption|string|null $paymentOption)
  • registerCard(...) via HasRegisteredCards
  • createGopayAccount() and getGopayToken(...) via HasGopayAccounts

Ensure your model implements Customer, and optionally Subscribable or TransactionDetails.

🚀 Usage Examples

Snap Payment

$response = $user->snapCharge([
    'order_id' => now()->timestamp,
    'gross_amount' => 100000,
]);

$redirectUrl = $response->getRedirectUrl();

Refund

$user->refund($order, 50000, 'Customer requested refund');

Create Subscription (Credit Card)

$user->subscribe($plan, PaymentType::CreditCard, $cardToken);

Create Subscription (GoPay)

$user->gopaySubscribe($plan);

Register Credit Card

$user->registerCard('4811111111111114', '12', '2025');

Create GoPay Partner Account

$activationUrl = $user->getGopayActivationUrl('https://yourapp.com/redirect');

✅ Testing

composer test

Make sure to set valid sandbox credentials in your .env.

📦 Roadmap

  • Snap charge
  • Subscription API
  • Card registration
  • GoPay partner creation
  • Refunds & Transaction status
  • Event handling (webhooks)

🤝 Contributing

Contributions, issues, and feature requests are welcome!
Feel free to open an issue or submit a pull request.

📄 License

MIT © Simeon Benson