/home/kueuepay/www/app/Http/Controllers/Admin/PushNotificationController.php
<?php

namespace App\Http\Controllers\Admin;

use Exception;
use App\Models\Admin\Admin;
use Illuminate\Http\Request;
use App\Models\Admin\BasicSettings;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Models\Admin\PushNotificationRecord;
use App\Models\User;
use App\Providers\Admin\BasicSettingsProvider;
use Pusher\PushNotifications\PushNotifications;


class PushNotificationController extends Controller
{

    /**
     * Display The configuration page of push notification
     *
     * @return view
     */
    public function configuration()
    {
        $page_title = "Setup Notification";
        $push_notification = BasicSettingsProvider::get()->push_notification_config ?? null;
        $broadcast_config   = BasicSettingsProvider::get()->broadcast_config ?? null;
        return view('admin.sections.push-notification.config', compact(
            'page_title',
            'push_notification',
            'broadcast_config'
        ));
    }

    /**
     * Display The Push Notification Send Page
     *
     * @return view
     *
     */
    public function index()
    {
        $page_title = "Send Notification";
        $notifications = PushNotificationRecord::orderByDesc("id")->paginate(10);
        return view('admin.sections.push-notification.send', compact(
            'page_title',
            'notifications',
        ));
    }

    /**
     * Function for send push notification
     * @param  \Illuminate\Http\Request  $request
     * @return method
     */
    public function send(Request $request) {
        $validator = Validator::make($request->all(),[
            'title'     => 'required|string|max:40',
            'body'      => 'required|string|max:80',
        ]);

        $validated = $validator->validate();

        $basic_settings = BasicSettingsProvider::get();
        if(!$basic_settings) {
            return back()->with(['error' => ['Oops! Basic settings not found!']]);
        }

        if(!$basic_settings->push_notification_config) {
            return back()->with(['error' => ['Sorry! You have to configure first to send push notification.']]);
        }

        $saved_method = $basic_settings->push_notification_config->method ?? null;

        if($saved_method == null) {
            return back()->with(['error' => ['Please configure your push notification with valid credentials.']]);
        }

        $methodDistribute = [
            'pusher'        => "sendNotificationWithPusher",
        ];

        if(!array_key_exists($saved_method,$methodDistribute)) {
            abort(404);
        }

        $distribute_method_name =$methodDistribute[$saved_method];

        return $this->$distribute_method_name($validated);


    }


    /**
     * Function for send push notification via Pusher(Message Bird)
     * @param array $data
     * @return back URL
     */
    public function sendNotificationWithPusher($data) {
        $basic_settings = BasicSettingsProvider::get();

        if(!$basic_settings) {
            return back()->with(['error' => ['Oops! Basic settings not found!']]);
        }

        $notification_config = $basic_settings->push_notification_config;

        if(!$notification_config) {
            return back()->with(['error' => ['Sorry! You have to configure first to send push notification.']]);
        }

        $instance_id    = $notification_config->instance_id ?? null;
        $primary_key    = $notification_config->primary_key ?? null;

        if($instance_id == null || $primary_key == null) {
            return back()->with(['error' => ['Sorry! You have to configure first to send push notification.']]);
        }

        $notification = new PushNotifications(
            array(
                "instanceId" => $notification_config->instance_id,
                "secretKey" => $notification_config->primary_key,
            )
        );

        $notification_data = [
            'title'     => $data['title'] ?? "",
            'body'      => $data['body'] ?? "",
            'icon'      => get_fav($basic_settings),
        ];

        // Get all users/admins ids
        $admins_id = Admin::all()->map(function($data) {
            return make_user_id_for_pusher("admin", $data->id);
        })->toArray();

        $users_id = User::all()->map(function($data){
            return make_user_id_for_pusher("user", $data->id);
        })->toArray();

        $publisher_ids = array_merge($users_id,$admins_id);
        $ids_plot = array_chunk($publisher_ids,900);

        try{
            foreach($ids_plot as $item) {
                $response = $notification->publishToUsers(
                    $item,
                    [
                        "web"   => [
                            "notification"      => $notification_data,
                        ],
                    ],
                );
                sleep(2);
            }
        }catch(Exception $e){
            return back()->with(['error' => ['Something went wrong! Please try again.']]);
        }

        // Insert notification record to database
        try{
            $push_notification_record = [
                'method'        => "pusher",
                'response'      => $response,
                'message'       => $notification_data,
                'send_by'       => Auth::user()->id,
            ];
            PushNotificationRecord::create($push_notification_record);
        }catch(Exception $e) {
            return back()->with(['error' => ['Oops! Failed to store information.']]);
        }

        return back()->with(['success' => ['Notification sended successfully!']]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $vlaidator = Validator::make($request->all(),[
            'method'            => 'required|string|in:pusher',
            'instance_id'       => 'required_if:method,pusher',
            'primary_key'        => 'required_if:method,pusher',
        ]);

        $validated = $vlaidator->validate();

        $accept_fields = [
            'pusher'        => ['instance_id','primary_key'],
            'firebase'      => ['server_key'],
            'one-signal'    => ['server_key'],
        ];

        $data = [];
        foreach($validated as $form_input => $item) {
            foreach($accept_fields as $method_name => $values) {
                if($validated['method'] == $method_name) {
                    $data['method'] = $validated['method'];
                    foreach($values as $value) {
                        $data[$value]  = $validated[$value] ?? null;
                    }
                    break;
                }
            }
        }

        $basic_settings = BasicSettings::first();
        if(!$basic_settings) {
            return back()->with(['error' => ['Oops! Basic setting not found!']]);
        }

        try{
            // Update Push notification config
            $basic_settings->update([
                'push_notification_config' => $data,
            ]);
        }catch(Exception $e) {
            return back()->with(['error' => ['Something went wrong! Please try again.']]);
        }

        return back()->with(['success' => ['Push notification configuration updated successfully!']]);
    }

}
Access Token

Get Access Token

Get access token to initiates payment transaction.

Endpoint: POST generate-token
Parameter Type Comments
client_id string Enter merchant API client/primary key
secret_id string Enter merchant API secret key
env string Enter merchant API environment
merchant_id string Enter merchant API merchant id
Just request to that endpoint with all parameter listed below:
                    
                        Request Example (guzzle)
                        

<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
  'accept' => 'application/json',
  'content-type' => 'application/json',
 ],
'form_params' => [
  'client_id' => '$client_id',
  'secret_id' => 'secret_id',
  'env' => 'env',
  'merchant_id' => 'merchant_id',
 ],
]);
echo $response->getBody();
                    
                        
**Response: SUCCESS (200 OK)**
{
 "message": {
 "success": [
  "Successfully token is generated"
 ]
},
"data": {
 "token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9P
SIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtn
VkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2x
oU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0
VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtc
G9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRi
Q0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U
4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3Jzbn
IyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYz
kzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
                    
                        
**Response: ERROR (400 FAILED)**
{
 "message": {
 "error": [
  "Invalid credentials."
 ]
},
"data": null,
"type": "error"
}