定制 ziming/laravel-myinfo-business-sg 二次开发

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

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

ziming/laravel-myinfo-business-sg

Composer 安装命令:

composer require ziming/laravel-myinfo-business-sg

包简介

Laravel Package for Singapore MyInfo Business

README 文档

README

Latest Version on Packagist Total Downloads Buy us a tree

PHP Laravel Package for MyInfo Business Singapore.

Official MyInfo Business Docs

Installation

You can install the package via composer:

composer require ziming/laravel-myinfo-business-sg

Followed by adding the following variables to your .env file.

The values provided below are the ones provided in the official MyInfo nodejs tutorial.

Change them to the values you are given for your app.

MYINFOBIZ_APP_CLIENT_ID=STG2-MYINFOBIZ-SELF-TEST
MYINFOBIZ_APP_CLIENT_SECRET=44d953c796cccebcec9bdc826852857ab412fbe2
MYINFOBIZ_APP_REDIRECT_URL=http://localhost:3001/callback
MYINFOBIZ_APP_REALM=http://localhost:3001
MYINFOBIZ_APP_PURPOSE="demonstrating MyInfo Business APIs"
MYINFOBIZ_APP_ATTRIBUTES=name,sex,race,nationality,dob,regadd,housingtype,email,mobileno,marital,edulevel,basic-profile,addresses,appointments

MYINFOBIZ_APP_SIGNATURE_CERT_PRIVATE_KEY=file:///Users/your-username/your-laravel-app/storage/myinfo-business-ssl/demoapp-client-privatekey-2018.pem
MYINFOBIZ_SIGNATURE_CERT_PUBLIC_CERT=file:///Users/your-username/your-laravel-app/storage/myinfo-business-ssl/staging_myinfo_public_cert.cer

MYINFOBIZ_DEBUG_MODE=false

# SANDBOX ENVIRONMENT (no PKI digital signature)
MYINFOBIZ_AUTH_LEVEL=L0
MYINFOBIZ_API_AUTHORISE=https://sandbox.api.myinfo.gov.sg/biz/v2/authorise
MYINFOBIZ_API_TOKEN=https://sandbox.api.myinfo.gov.sg/biz/v2/token
MYINFOBIZ_API_ENTITYPERSON=https://sandbox.api.myinfo.gov.sg/biz/v2/entity-person-sample

# TEST ENVIRONMENT (with PKI digital signature)
MYINFOBIZ_AUTH_LEVEL=L2
MYINFOBIZ_API_AUTHORISE=https://test.api.myinfo.gov.sg/biz/v2/authorise
MYINFOBIZ_API_TOKEN=https://test.api.myinfo.gov.sg/biz/v2/token
MYINFOBIZ_API_ENTITYPERSON=https://test.api.myinfo.gov.sg/biz/v2/entity-person

# Controller URI Paths. IMPORTANT
MYINFOBIZ_CALL_AUTHORISE_API_URL=/redirect-to-singpass
MYINFOBIZ_GET_ENTITY_PERSON_DATA_URL=/myinfo-entity-person

Lastly, publish the config file

php artisan vendor:publish --provider="Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgServiceProvider" --tag="config"

You may also wish to publish the MyInfo official nodejs demo app ssl files as well to storage/myinfo-business-ssl. You should replace these in your production environment.

php artisan vendor:publish --provider="Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgServiceProvider" --tag="myinfo-business-ssl"

Usage and Customisations

When building your button to redirect to SingPass. It should link to route('myinfo-business.singpass')

After SingPass redirects back to your Callback URI, you should make a post request to route('myinfo.person')

If you prefer to not use the default routes provided you may set enable_default_myinfo_business_routes to false in config/laravel-myinfo-business-sg.php and map your own routes. This package controllers will still be accessible as shown in the example below:

<?php
use Ziming\LaravelMyinfoBusinessSg\Http\Controllers\CallMyinfoBusinessAuthoriseApiController;
use Ziming\LaravelMyinfoBusinessSg\Http\Controllers\GetMyinfoBusinessEntityPersonDataController;
use Illuminate\Support\Facades\Route;

Route::post(config('/go-myinfo-business-singpass'), CallMyinfoBusinessAuthoriseApiController::class)
->name('myinfo-business.singpass')
->middleware('web');

Route::post('/fetch-myinfo-business-entity-person-data', GetMyinfoBusinessEntityPersonDataController::class)
->name('myinfo-business.entity-person');

During the entire execution, some exceptions may be thrown. If you do not like the format of the json responses. You can customise it by intercepting them in your laravel application app/Exceptions/Handler.php

An example is shown below:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\AccessTokenNotFoundException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        // You may wish to add all the Exceptions thrown by this package. See src/Exceptions folder
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function report(\Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, \Throwable $exception)
    {
        // Example of an override. You may override it via Service Container binding too
        if ($exception instanceof AccessTokenNotFoundException && $request->wantsJson()) {
            return response()->json([
                'message' => 'Access Token is missing'
            ], 404);
        }
        
        return parent::render($request, $exception);
    }
}

The list of exceptions are as follows

<?php
use Ziming\LaravelMyinfoBusinessSg\Exceptions\AccessTokenNotFoundException;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidAccessTokenException;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidDataOrSignatureForEntityPersonDataException;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\InvalidStateException;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\MyinfoEntityPersonDataNotFoundException;
use Ziming\LaravelMyinfoBusinessSg\Exceptions\SubNotFoundException;

Lastly, if you prefer to write your own controllers, you may make use of LaravelMyinfoBusinessSgFacade or LaravelMyinfoBusinessSg to generate the authorisation api uri (The redirect to Singpass link) and to fetch MyInfo Person Data. Examples are shown below

<?php

use Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgFacade as LaravelMyinfoBusinessSg;

// Get the Singpass URI and redirect to there
return redirect(LaravelMyinfoBusinessSg::generateAuthoriseApiUrl($state));
<?php
use Ziming\LaravelMyinfoBusinessSg\LaravelMyinfoBusinessSgFacade as LaravelMyinfoBusinessSg;

// Get the Myinfo Business data in an array with 'data' key
$entityPersonData = LaravelMyinfoBusinessSg::getMyinfoEntityPersonData($code);

// If you didn't want to return a json response with the person information in the 'data' key. You can do this
return response()->json($entityPersonData['data']);

You may also choose to subclass GetMyinfoEntityPersonDataController and override its preResponseHook() template method to do logging or other stuffs before returning the person data.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Other ways of Contributing

Please see CONTRIBUTING for details.

ziming/laravel-myinfo-business-sg 适用场景与选型建议

ziming/laravel-myinfo-business-sg 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.64k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 ziming/laravel-myinfo-business-sg 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-01-28