wychoong/lunarphp-mpgs

MPGS payment adapter for Lunar

v0.1.2 2023-04-14 14:13 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

MPGS Hosted checkout integration for Lunar

Supported action

  • purchase

Not supported (PR welcome)

  • refund
  • authorize/capture

Installation

You can install the package via composer:

composer require wychoong/lunarphp-mpgs

You can publish the config file with:

php artisan vendor:publish --tag="lunarphp-mpgs-config"

Enable the driver

Set the driver in config/lunar/payments.php

<?php

return [
    // ...
    'types' => [
        'card' => [
            'driver' => 'stripe',
            'authorized' => 'payment-received',  # or any status key configured in lunar.orders.statuses
        ],
    ],
];

###Add your MPGS credentials Set your MPGS_ variable in .env

MPGS_MERCHANT_ID=
MPGS_API_PASSWORD=
MPGS_VERSION=

Setup

We use closure to return the data you want to pass to the api

use \WyChoong\Mpgs\Facades\Mpgs;

// in service provider `boot` method
Mpgs::initiateCheckoutUsing(function ($cart, $amount, $currency): array {
    if (!$order = $cart->order) {
        $order = $cart->createOrder();
    }

    $reference = $order->reference . date('Ymdhis');

    return  [
        // refer to the api spec for Initiate Checkout params
        'order' => [
            'id' => $reference,
            'currency' => $currency,
            'amount' => $amount,
            'description' => "Payment for #" . $order->reference,
            'reference' => $reference,
        ],
        'transaction' => [
            'reference' => $reference,
        ],
        'interaction' => [
            'merchant' => [
                'name' => 'Lunar store',
            ],
            'displayControl' => [
                'billingAddress' => 'HIDE',
            ]
        ]
    ];
});

Backend Usage

Creating a PaymentIntent

use \WyChoong\Mpgs\Facades\Mpgs;

Mpgs::createIntent(\Lunar\Models\Cart $cart);

This method will initiate a checkout session to be used by checkout.js Latest session and order.id are stored in cart's meta

'meta' => [
    'payment_intent' => `session`,
    'order_id' => `order.id`,
],

Storefront Usage

This package only provide basic blade components to interact with MPGS,, publish the views to fit your storefront design

php artisan vendor:publish --tag="lunarphp-mpgs-views"

Set up the scripts and payment component

In the your checkout page

@mpgsScripts

@if ($paymentType == 'card')
    <livewire:mpgs.payment :cart="$cart" />
@endif

The component will handle the success payment for you. To redirect or add handling after payment verified, set your route or listen to livewire event

// config/lunar-mpgs.php
'route' => [
    'payment-success' => null,
    'payment-failed' => null,
]

// livewire events
'mpgsPaymentSuccess'
'mpgsPaymentFailed'

Roadmap

  • support capture/refund if applicable
  • implement omnipay-mpgs adapter

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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