zfhassaan/easypaisa

Unlock the power of EasyPaisa Payment Gateway in Laravel 9 and Laravel 10. Accept payments seamlessly and securely, empowering your business to thrive in the digital era. Experience hassle-free integration and provide a seamless checkout experience for your customers.

v1.0.5 2023-05-31 09:29 UTC

This package is auto-updated.

Last update: 2024-05-29 17:22:41 UTC


README

EASYPAISA Payment Gateway

Latest Version on Packagist MIT Licensed Total Downloads Hits

Disclaimer

This is unofficial Easypaisa API Payment Gateway. This repository is only created to help developers in streamlining the integration process.

Installation

The Package provides two way of using.

  • Direct Checkout
  • Hosted Checkout.

To use the Direct checkout ( Rest API ) you can call the already defined function sendRequest() and to use the Hosted Checkout (Redirection Method) you can call the sendHostedRequest() method to redirect user to easypaisa page for payment.

To install the EasyPaisa package, follow these steps:

Install the package via Composer by running the following command:

composer require zfhassaan/easypaisa

Publish the package configuration file by running the following command:

php artisan vendor:publish

Update the .env file with the required configuration values:

EASYPAISA_MODE=sandbox
EASYPAISA_SANDBOX_URL=
EASYPAISA_PRODUCTION_URL=
EASYPAISA_TYPE=direct
EASYPAISA_SANDBOX_USERNAME=
EASYPAISA_SANDBOX_PASSWORD=
EASYPAISA_SANDBOX_STOREID=
EASYPAISA_PRODUCTION_USERNAME=
EASYPAISA_PRODUCTION_PASSWORD=
EASYPAISA_PRODUCTION_STOREID=
EASYPAISA_PAYMENT_TYPE=MA
EASYPAISA_SANDBOX_HASHKEY=
EASYPAISA_PRODUCTION_HASHKEY=
EASYPAISA_CALLBACK_URL=
EASYPAISA_HOSTED_CHECKOUT=

Usage

Direct Checkout

To perform a direct checkout using the EasyPaisa payment gateway, use the following code:

try {
    $easypaisa = new Easypaisa;
    $response = $easypaisa->sendRequest($request->all());
    $responseCode = $response['responseCode'];
    $responseDesc = $response['responseDesc'];

    if ($responseCode != '0000') {
        return response()->json(['status' => false, 'message' => $responseDesc], Response::HTTP_NOT_ACCEPTABLE);
    }

    $result = [
        'status_code' => '00',
        'status_msg' => $response['responseDesc'],
        'transaction_id' => $response['transactionId'],
        'code' => $response['responseCode'],
        'message' => $response['responseDesc'],
        'basket_id' => strip_tags($request['orderId'])
    ];
    return response()->json($result);
} catch (\Exception $e) {
    return response()->json(['status' => false, 'message' => 'Error Processing Your Request.'], 500);
}

The sendRequest() method of the Easypaisa class processes the payment request. It takes an array of request data as input and returns the response from the EasyPaisa payment gateway.

Package File: Easypaisa.php The sendRequest() method is defined in the Easypaisa package file. It processes the payment request by sending the data to the EasyPaisa API.

$credentials = $this->getCredentials();

$data = [
    'orderId' => strip_tags($request['orderId']),
    'storeId' => $this->getStoreId(),
    'transactionAmount' => strip_tags($request['amount']),
    'transactionType' => 'MA',
    'mobileAccountNo' => strip_tags($request['mobileAccountNo']),
    'emailAddress' => strip_tags($request['emailAddress'])
];
$response = Http::timeout(60)->withHeaders([
    'credentials' => $credentials,
    'Content-Type' => 'application/json'
])->post($this->getApiUrl(), $data);

$result = $response->json();

return $result;

The getCredentials() method retrieves the credentials based on the configuration settings

Hosted Checkout

To perform a hosted checkout using the EasyPaisa payment gateway, use the following code:

$data['amount'] = strip_tags($request['amount']);  //Last two digits will be considered as Decimal
        $data['orderRefNum'] = strip_tags($request['orderRefNum']); // You can customize it (only Max 20 Alpha-Numeric characters)
        $data['paymentMethod'] = 'InitialRequest';
        $data['postBackURL'] = $this->getCallbackUrl();
        $data['storeId'] = $this->getStoreId();
        $data['timeStamp'] = $this->getTimestamp();
        $hashk = $this->gethashRequest($data);
        $data['encryptedHashRequest'] = $hashk;
        $data['mobileAccountNo'] = '';
        return $this->getCheckoutUrl($data);

The gethashRequest() method retrieves the HashKey after encryption. The getCheckoutUrl() method retrieves the checkouturl which will redirect the customer to the Easypaisa portal.

Hosted.blade.php

     <form action="" method="POST" target="_blank">
@csrf
        <input name="transactionAmount" value=""/>
        <input name="orderId" value="" hidden = "true"/>
        <input name="storeId" value="" hidden = "true"/>
        <input name="mobileAccountNo" value=""/>
        <input name="emailAddress" value=""/>
        <input name="postBackURL" value="" hidden = "true"/>
        <input type="submit" src=”checkout-button-with-logo.png border=”0” name= “pay”>
    </form>

Controller

public function index(Request $request)
    {

        $easypaisa = new Easypaisa();
        $response = $easypaisa->sendHostedRequest($request->all());
        return redirect()->away($response,302);

    }