hypnodev/larapal

An easy way to integrate Paypal into Laravel

v1.0.7 2023-08-10 10:38 UTC

This package is auto-updated.

Last update: 2024-05-10 12:08:51 UTC


README

logo.png

Latest Version on Packagist Total Downloads

A modern, easy and fluent way to allow your clients to pay with PayPal. banner

Installation

Via Composer

$ composer require hypnodev/larapal

Publish the configuration with command:

$ php artisan vendor:publish --provider="hypnodev\Larapal\LarapalServiceProvider" 

Add these keys in your .env:

PAYPAL_MODE=sandbox

PAYPAL_SANDBOX_ID=
PAYPAL_SANDBOX_SECRET=

PAYPAL_PRODUCTION_ID=
PAYPAL_PRODUCTION_SECRET=

If you don't have yet credentials for PayPal API, please refer to Get Started - PayPal Developer

Usage

Add BillableWithPaypal trait to your User model

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
}

This will add chargeWithPaypal, subscribeWithPaypal, getPaypalCurrency, getShippingFields to your user.

Then you can charge your user with method:

<?php
auth()->user()->chargeWithPaypal('Charge description', [ // Array of items
    ['name' => 'pkg base', 'description' => 'base package', 'price' => 10.00, 'tax' => 2]
]);

// If your charge has shipping, you need to add an extra param with name and amount
auth()->user()->chargeWithPaypal('Charge description', [ // Array of items
    ['name' => 'pkg base', 'description' => 'base package', 'price' => 10.00, 'tax' => 2]
], [ // Shipping
    'name' => 'Courier name',
    'amount' => 100.50,
    'address' => [ // Optional, you can skip this key
        'address' => '4178 Libby Street',
        'city' => 'Hermosa Beach',
        'state' => 'CA',
        'postal_code' => '90254',
        'country' => 'USA'
    ]
]);

Or for subscription:

auth()->user()->subscribeWithPaypal('Plan id');

You can create a plan under "App Center" in your PayPal Merchant Dashboard

If you need to charge user with another currency different from the configuration, you can override getPaypalCurrency method:

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
    
    /**
     * @inheritDoc
     */
    protected function getPaypalCurrency(): string
    {
        return 'USD';
    }
}

You can set default shipping info with getShippingFields method

<?php

namespace App;

use hypnodev\Larapal\Traits\BillableWithPaypal;
// ...

class User extends Authenticatable
{
    use Notifiable, BillableWithPaypal;
    
    // ...
    
    /**
     * @inheritDoc
     */
    protected function getShippingFields(): array
    {
        return [
            'address' => $this->shipping_address,
            'city' => $this->shipping_city,
            'state' => $this->shipping_state,
            'postal_code' => $this->shipping_postal_code,
            'country' => $this->shipping_country
        ];
    }
}

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email me@cristiancosenza.com instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.