Skip to content

vedatunlu/laravel-payment

Repository files navigation

Laravel Payment

Laravel Payment is a package used for managing payment gateway integrations. This package provides an easy interface for integrating popular payment gateways such as Sipay (and soon Iyzico, Sipay, PayTr, iPara, Paynet etc.) into your Laravel application.

Features

  • Easy integration with various payment gateways
  • Easy usage with Payment client class
  • Wallet usage with available payment gateways
  • Easy validation for hash keys within incoming error or success callback request, webhooks etc.
  • Multiple usage with available gateways and its methods.

Installation

  1. Use Composer to add the Laravel Payment package to your project:
    composer require vedatunlu/payment
  1. Package service providers will be discovered by your laravel project automatically. So you don't need to update your app/config.php file to add package service provider.

  2. Publish the config files to your project.

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

The command above will create a file named payment.php on config directory.

Create environment variables on your .env file before usage

Add your credentials provided by your payment gateway to the related payment gateway scope of the payment config file which is published to the config directory of your laravel file. You can provide the credentials from your service provider if you don't have yet. You don't need to define any other credentials if you don't plan to use another one. If yes. You can use each of payment gateway supported by the package by defining credentials on the related scope of the payment config file.

Payment class usage reference table

Please check out the table given below to get basic knowledge of the Payment class behavior before using it.

Method Name Description Available gateways
getCards Returns saved cards with given customer Sipay
saveCard Stores card information on the payment gateway host Sipay
updateCard Updates card information on the payment gateway host Sipay
deleteCard Deletes card information on the payment gateway host Sipay
payWith2D Start a 2D payment with given credit card for given customer info Sipay
payWith3D Start a 3D payment with given credit card for given customer info Sipay
payWithSavedCard Start a payment with stored credit card on the payment gateway host Sipay
verifyPayment Make a verification request for given payment Sipay
refund Start a refund process for given invoice Sipay
installmentInquiry Make an installment inquiry request for given credit card Sipay
transactionStatus Check all processed transaction's status Sipay

Hint: All methods accepts an array as parameter. You are not required to attach any credentials such as merchant_id, app_key, secret_key thanks to the package service provider can attach the credentials automatically. Please check your payment gateway documentation to get further information about the required parameters of the endpoints.

Basic Usage Examples

  1. getCards:
    // will return credit card resources
    Payment::gateway('sipay')
        ->getCards([
            'customer_number' => 123123
        ])->toArray();
  1. saveCard:
    // will return credit card token
    Payment::gateway('sipay')
        ->saveCard([
            'card_number' => 4508034508034509,
            'customer_number' => 123123,
            'expiry_month' => 12,
            'expiry_year' => 2026,
            'card_holder_name' => 'Vedat Ünlü'
        ])->toArray();
  1. payWith3D:
    // will return a html form body and this form will redirect to the 3D verification
    $response = Payment::gateway('sipay')
                    ->payWith3D([
                        'cc_holder_name' => 'Vedat Ünlü',
                        'cc_no' => '4508034508034509',
                        'expiry_month' => '12',
                        'expiry_year' => '2026',
                        'cvv' => '000',
                        'currency_code' => 'TRY',
                        'installments_number' => 1,
                        'invoice_id' => rand(100000, 999999),
                        'invoice_description' => 'invoice_description',
                        'name' => 'Vedat',
                        'surname' => 'Ünlü',
                        'total' => 101.10,
                        'items' => json_encode([
                            [
                                'name' => 'Item 2',
                                'price' => 101.10,
                                'quantity' => 1,
                                'description' => "item description"
                            ]
                        ]),
                        'cancel_url' => 'payment-success-callback-url', // route('payment.callback.success)
                        'return_url' => 'payment-error-callback-url', // route('payment.callback.error)
                        'response_method' => 'POST'
                    ]);
                    
    if ($response->isSuccess() == true) {
        return $response->get3DSForm(); // get response as html form
    }
    
    return $response->toArray(); // get response as array
  1. Validate Incoming Sipay Hash key:

You can easily validate hash keys returned from sipay gateway using Payment::validate($hashKey).

    $hashKey = $request->input('hash_key');
    
    if (!Payment::validate('sipay', $hashKey)) {
        return back()->with('error', 'Invalid hash key');
    }

This method returns array including status, total amount, invoice id, order id, currency code if key is valid. If not method will be returned false.

Contributing to the package

We welcome and appreciate your contributions to the package! The contribution guide can be found here.

License

This package is open-sourced software licensed under the MIT license.