定制 abdallhsamy/laravel-hyperpay 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

abdallhsamy/laravel-hyperpay

Composer 安装命令:

composer require abdallhsamy/laravel-hyperpay

包简介

Laravel package for Hyperpay payment gateway in MENA.

README 文档

README

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 abdallhsamy/laravel-hyperpay

Database migration

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

php artisan migrate

If you want to make an update or change the path of the migration, you can publish it using vendor:publish

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

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 Abdallhsamy\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 Abdallhsamy\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 Abdallhsamy\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 Abdallhsamy\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 Abdallhsamy\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
Abdallhsamy\LaravelHyperpay\Events\SuccessTransaction success transaction
Abdallhsamy\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,
        ],
         'Abdallhsamy\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 Abdallhsamy\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 abdallhsamy2011[at]gmail.com 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.

abdallhsamy/laravel-hyperpay 适用场景与选型建议

abdallhsamy/laravel-hyperpay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 567 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「abdallhsamy」 「laravel-hyperpay」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 abdallhsamy/laravel-hyperpay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 abdallhsamy/laravel-hyperpay 我们能提供哪些服务?
定制开发 / 二次开发

基于 abdallhsamy/laravel-hyperpay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 abdallhsamy/laravel-hyperpay 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 567
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-26