Coder Social home page Coder Social logo

devinweb / laravel-hyperpay Goto Github PK

View Code? Open in Web Editor NEW
24.0 3.0 11.0 161 KB

Laravel package for Hyperpay payment gateway in MENA.

Home Page: https://devinweb.com

License: MIT License

PHP 100.00%
laravel hyperpay mena gateway laravel-package payment devinweb

laravel-hyperpay's Introduction

Laravel Hyperpay

Laravel HyperPay

tests StyleCI Shield Latest Version on Packagist Total Downloads

Laravel HyperPay provides an easy way to handle all the transactions with different states.

Installation

You can install the package via composer:

composer require devinweb/laravel-hyperpay

Database migration

Laravel-hyperpay provides a migration to handle its own transaction, don't forget to publish the migration after installation

php artisan vendor:publish --tag="hyperpay-migrations"

Then migrate

php artisan migrate

This migration has a model named Transaction, if your app use multi-tenancy, you can create a new transaction model based on the hyperpay transaction model.

use Hyn\Tenancy\Traits\UsesTenantConnection;
use Devinweb\LaravelHyperpay\Models\Transaction as ModelsTransaction;

class Transaction extends ModelsTransaction
{
    use UsesTenantConnection;
}

then don't forget the update the transaction_model path in the config file app/hyperpay.php

<?php

return [
    //
    "transaction_model" => 'YOUR_NEW_TRANSACTION_MODEL_NAMESPACE',
    //
];

Setup and configuration

You can also publish the config file using

php artisan vendor:publish --tag="hyperpay-config"

After that you can see the file in app/hyperpay.php

Before start using Laravel-hyperpay, add the ManageUserTransaction trait to your User model, this trait provides mutliple tasks to allow you to perform the transaction process from the given user.

use Devinweb\LaravelHyperpay\Traits\ManageUserTransactions;

class User extends Authenticatable
{
    use ManageUserTransactions;
}

This package use User model that will be App\User or App\Models\User, if else you can define your user model using the .env

PAYMENT_MODEL=App\Models\User

HyperPay Keys

Next, you should configure your hyperpay environment in your application's .env

SANDBOX_MODE=true
ACCESS_TOKEN=
ENTITY_ID_MADA=
ENTITY_ID=
# default SAR
CURRENCY=

Creating a transaction

To create a transaction in hyperpay using this package, we need to to prepare the checkout then generate the form.

Prepare the checkout

use Devinweb\LaravelHyperpay\Facades\LaravelHyperpay;

class PaymentController extends  Controller
{
    public function prepareCheckout(Request $request)
    {
        $trackable = [
            'product_id'=> 'bc842310-371f-49d1-b479-ad4b387f6630',
            'product_type' => 't-shirt'
        ];
        $user = User::first();
        $amount = 10;
        $brand = 'VISA' // MASTER OR MADA

        return LaravelHyperpay::checkout($trackable_data, $user, $amount, $brand, $request);
    }
}

you can also attach the billing data to the checkout by creating the billing class using this command, all billing files you can find them in app/Billing folder.

php artisan make:billing HyperPayBilling

then use

use Devinweb\LaravelHyperpay\Facades\LaravelHyperpay;
use App\Billing\HyperPayBilling

LaravelHyperpay::addBilling(new HyperPayBilling())->checkout($trackable_data, $user, $amount, $brand, $request);

You can also generate your own merchantTransactionId and tell the package to use it, by using addMerchantTransactionId($id)

use Illuminate\Support\Str;

$id = Str::random('64');

LaravelHyperpay::addMerchantTransactionId($id)->addBilling(new HyperPayBilling())->checkout($trackable_data, $user, $amount, $brand, $request);

Next the response returned by the prepareCheckout actions

{
    "buildNumber": "",
    "id": "RANDOME_ID.uat01-vm-tx04",
    "message": "successfully created checkout",
    "ndc": "RANDOME_ID.uat01-vm-tx04",
    "result": {
        "code": "000.200.100",
        "description": "successfully created checkout"
    },
    "script_url": "https://test.oppwa.com/v1/paymentWidgets.js?checkoutId=RANDOME_ID.uat01-vm-tx04",
    "shopperResultUrl": "shopperResultUrl",
    "status": "200",
    "timestamp": "2021-04-05 11:16:50+0000"
}

To create the payment form you just need to add the following lines of HTML/JavaScript to your page.

<script src="{{ script_url }}"></script>
<form
    action="{{shopperResultUrl}}"
    class="paymentWidgets"
    data-brands="VISA MASTER"
></form>

Payment status

After the transaction process hyperpay redirect the user to the merchant page using the shopperResultUrl that you can configure it in the config file app/hyperpay.php, by updating the redirect_url value.

// app/hyperpay.php

return [
    //
    "redirect_url" => "/hyperpay/finalize",
    //
]

You can also add redirect_url dynamically via addRedirectUrl($url) that can override the default config redirect_url if you want to add a dynamic url like if you use multi-tenant or add some parameters to your redirection url.

$redirect_url = $req->headers->get('origin'). '/finalize';

LaravelHyperpay::addRedirectUrl($redirect_url)->addBilling(new HyperPayBilling())->checkout($trackable_data, $user, $amount, $brand, $request);

After redirection you can use an action the handle the finalize step

use Devinweb\LaravelHyperpay\Facades\LaravelHyperpay;

class PaymentController extends Controller
{
    public function paymentStatus(Request $request)
    {
        $resourcePath = $request->get('resourcePath');
        $checkout_id = $request->get('id');
        return LaravelHyperpay::paymentStatus($resourcePath, $checkout_id);
    }
}

Events handlers

Laravel-hyperpay providers two events during the transaction process, after finalize this package fire for successfull transaction

Event Description
Devinweb\LaravelHyperpay\Events\SuccessTransaction success transaction
Devinweb\LaravelHyperpay\Events\FailTransaction fail transaction

Each event of them contains the trackable_data and hyperpay_data that used to prepare the checkout

Listener exemple

First we start by creating a new listener using

php artisan make:listener TransactionSuccessListener

After that go to app/Providers/EventServiceProvider and register the event with your listener

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
         'Devinweb\LaravelHyperpay\Events\SuccessTransaction' => [
            'App\Listeners\TransactionSuccessListener',
        ],
    ];

    //
}

In each success transaction laravel-hyperpay package fire an event with the necessary data take a look at our TransactionSuccessListener class.

<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class TransactionSuccessListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
       $hyperpay_data =  $event->transaction['hyperpay_data'];

       $trackable_data =  $event->transaction['trackable_data'];
    }
}

The same you can do with Devinweb\LaravelHyperpay\Events\FailTransaction event.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel hyperpay Boilerplate

You can use this repository to check the integration of the package laravel-hyperpay-boilerplate.

laravel-hyperpay's People

Contributors

darbaoui avatar stylecibot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-hyperpay's Issues

Undefined index: id

When i call this function

$trackable_data = [
                        'product_id'=> 'bc842310-371f-49d1-b479-ad4b387f6630',
                        'product_type' => 't-shirt'
                    ];
                    $user = \App\Models\User::first();
                    $amount = 10;
                    $brand = 'MADA'; // MASTER OR MADA            
                    return   LaravelHyperpay::checkout($trackable_data, $user, $amount, $brand, $request);

I am getting 'Undefined index: id'
Error shows here

public function addScriptUrl($base_url)
    {
        $script_url = $base_url."/v1/paymentWidgets.js?checkoutId={$this->body()['id']}"; // here shows the error
        $this->script_url = $script_url;
        return $this;

    }

[200.300.404] Invalid or missing parameter in production

Hello,

First, thank you for this great package, it helps a lot.

Lately we're getting this error when submitting Card details for Visa and Mada only in production. We're not sure what the problem is. Any ideas?

{
    "ndc": "###.prod02-vm-tx07",
    "result": {
        "code": "200.300.404",
        "description": "invalid or missing parameter",
        "parameterErrors": [
            {
                "name": "customer.browser.language",
                "value": "en-US",
                "message": "was already set and cannot be overwritten"
            },
            {
                "name": "customer.browser.userAgent",
                "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
                "message": "was already set and cannot be overwritten"
            }
        ]
    },
    "status": 422,
    "message": "invalid or missing parameter",
    "timestamp": "2022-09-07 08:02:26+0000",
    "buildNumber": "2621780cf1399bc67d0207fab6d754153a4fc274@2022-08-11 07:04:50 +0000",
    "transaction_status": "cancel"
}

We've added these parameters:

class HyperPayBilling implements BillingInterface
{
    /**
     * Get the billing data.
     *
     * @return array
     */
    public function getBillingData(): array
    {
        ...
        ...

        $parameters = [
            'billing.street1'   => $address1,
            'billing.city'      => $city,
            'billing.state'     => $state,
            'billing.country'   => $countryIso,
            'billing.postcode'  => $postcode,
        ];

        return $parameters;
    }
}

This is the full parameters that we're seeing:

[
  "entityId" => "###"
  "amount" => "109.99"
  "currency" => "SAR"
  "paymentType" => "DB"
  "merchantTransactionId" => "xXGXCw4nY3ZvdRyZJNsZ3a88iDNHpV3T8mSGYkyq"
  "notificationUrl" => "http://localhost:8021/hyperpay/webhook"
  "customer.email" => "[email protected]"
  "customer.givenName" => "Test"
  "customer.surname" => "Test"
  "customer.browser.userAgent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
  "customer.browser.language" => "ar"
  "billing.street1" => "3651"
  "billing.city" => "Riyadh"
  "billing.state" => "Riyadh Province"
  "billing.country" => "SA"
  "billing.postcode" => "12333"
]

composer.json

{
    "php": "^7.3|^8.0",
    "barryvdh/laravel-dompdf": "^0.9.0",
    "devinweb/laravel-hyperpay": "1.2",
    "doctrine/dbal": "^3.1",
    "elfsundae/laravel-hashid": "^1.7",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "jorenvanhocht/laravel-share": "^4.1",
    "laravel/framework": "^8.40",
    "laravel/passport": "^10.1",
    "laravel/socialite": "^5.2",
    "laravel/tinker": "^2.5",
    "league/flysystem-aws-s3-v3": "~1.0",
    "league/flysystem-cached-adapter": "~1.0",
    "maatwebsite/excel": "^3.1",
    "maxbanton/cwh": "^2.0",
    "mckenziearts/laravel-notify": "^2.2",
    "mesavolt/mdi-php": "^1.1",
    "mohannadnaj/smsa-sdk": "^1.0",
    "propaganistas/laravel-phone": "^4.3",
    "protonemedia/laravel-cross-eloquent-search": "^3.0",
    "spatie/laravel-responsecache": "^7.4"
}

Thank you in advance.

Array to string conversion

Hello @darbaoui

When I tried to prepare a checkout it's give an error:

Array to string conversion

Before two weeks maybe or less, I make it and every thing is working perfectly. So, I continue working on other tasks and features; but now I tried to test it and it not working because of that error ?!!

Can you help.?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.