Skip to content

Commit

Permalink
Merge pull request #25 from remotemerge/migration
Browse files Browse the repository at this point in the history
Migration
  • Loading branch information
remotemerge authored Aug 24, 2024
2 parents 1bbc1c2 + b42f818 commit d71f696
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 50 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@ Bugs and feature requests are tracked using GitHub issues, and prioritization is

```php
// Init composer autoloader.
require 'vendor/autoload.php';
require dirname(__DIR__) . '/vendor/autoload.php';

use RemoteMerge\Esewa\Client;
use RemoteMerge\Esewa\Config;

// Set success and failure callback URLs.
$successUrl = 'https://example.com/success.php';
$failureUrl = 'https://example.com/failed.php';

// Config for development.
$config = new Config($successUrl, $failureUrl);

// Config for production.
$config = new Config($successUrl, $failureUrl, 'b4e...e8c753...2c6e8b');

// Initialize eSewa client.
$esewa = new Client($config);
// Initialize eSewa client for development.
$esewa = new Client([
'merchant_code' => 'EPAYTEST',
'success_url' => $successUrl,
'failure_url' => $failureUrl,
]);

// Initialize eSewa client for production.
$esewa = new Client([
'merchant_code' => 'b4e...e8c753...2c6e8b',
'success_url' => $successUrl,
'failure_url' => $failureUrl,
]);
```

Here `b4e...e8c753...2c6e8b` is merchant code retrieved from eSewa.
Expand All @@ -64,13 +68,13 @@ eSewa system will redirect the user to your specified success URL if the payment
the payment fails.

```php
$esewa->process('P101W201', 100, 15, 80, 50);
$esewa->payment('P101W201', 100, 15, 80, 50);
```

The method accepts five parameters.

```text
process(string $pid, float $amt, float $txAmt = 0, float $psc = 0, float $pdc = 0)
payment(string $pid, float $amt, float $txAmt = 0, float $psc = 0, float $pdc = 0)
```

1. `pid` A unique ID of product or item or ticket etc.
Expand All @@ -89,7 +93,7 @@ The verification process identifies potentially fraudulent transactions and chec
amount and other parameters.

```php
$status = $esewa->verify('R101', 'P101W201', 245);
$status = $esewa->verifyPayment('R101', 'P101W201', 245);
if ($status) {
// Verification successful.
}
Expand All @@ -98,7 +102,7 @@ if ($status) {
The method accepts three parameters.

```text
verify(string $refId, string $oid, float $tAmt)
verifyPayment(string $refId, string $oid, float $tAmt)
```

1. `refId` A unique payment reference code generated by eSewa.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remotemerge/esewa-php-sdk",
"version": "3.0.0",
"version": "3.1.0",
"type": "library",
"description": "eSewa payment gateway integration in PHP.",
"homepage": "https://github.com/remotemerge/esewa-php-sdk",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions demo/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
declare(strict_types=1);

use RemoteMerge\Esewa\Client;
use RemoteMerge\Esewa\Config;

// Init the autoloader
require dirname(__DIR__) . '/vendor/autoload.php';

// Set up the configuration object
$successUrl = 'http://localhost:8090/demo/success.php';
$failureUrl = 'http://localhost:8090/demo/failed.php';
$config = new Config($successUrl, $failureUrl);
$successUrl = 'http://localhost:8080/demo/success.php';
$failureUrl = 'http://localhost:8080/demo/failed.php';

// Initialize the client
$esewa = new Client($config);
// Initialize the client with the configuration
$esewa = new Client([
'merchant_code' => 'EPAYTEST',
'success_url' => $successUrl,
'failure_url' => $failureUrl,
]);

// Generate random 16 characters product ID
$productId = substr(bin2hex(random_bytes(8)), 0, 16);

// Process the payment
$esewa->process($productId, 100, 10);
$esewa->payment($productId, 100, 10);
14 changes: 8 additions & 6 deletions demo/success.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
declare(strict_types=1);

use RemoteMerge\Esewa\Client;
use RemoteMerge\Esewa\Config;

// Require the autoloader
require dirname(__DIR__) . '/vendor/autoload.php';

// Set up the configuration object
$successUrl = 'http://localhost:8090/demo/success.php';
$failureUrl = 'http://localhost:8090/demo/failed.php';
$config = new Config($successUrl, $failureUrl);
$successUrl = 'http://localhost:8080/demo/success.php';
$failureUrl = 'http://localhost:8080/demo/failed.php';

// Initialize the client
$esewa = new Client($config);
$esewa = new Client([
'merchant_code' => 'EPAYTEST',
'success_url' => $successUrl,
'failure_url' => $failureUrl,
]);

// Get the query parameters
$productId = $_GET['oid'] ?? null;
Expand All @@ -23,7 +25,7 @@

try {
// Verify the payment and output the result
$status = $esewa->verify($referenceId, $productId, (float) $amount);
$status = $esewa->verifyPayment($referenceId, $productId, (float) $amount);
exit($status ? 'The payment is verified.' : 'The payment is not verified.');
} catch (Exception $exception) {
exit($exception->getMessage());
Expand Down
22 changes: 11 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

use Exception;

class Client
class Client extends Config implements ClientInterface
{
public function __construct(private readonly Config $config)
public function __construct(private readonly array $configs = [])
{
//
parent::__construct($this->configs);
}

/**
* This method creates the form in runtime and post the data to eSewa server.
*/
public function process(string $productId, float $amount, float $taxAmount, float $serviceAmount = 0.0, float $deliveryAmount = 0.0): void
public function payment(string $productId, float $amount, float $taxAmount, float $serviceAmount = 0.0, float $deliveryAmount = 0.0): void
{
// format form attributes
$formInputs = [
'scd' => $this->config->merchantCode,
'su' => $this->config->successUrl,
'fu' => $this->config->failureUrl . '?' . http_build_query(['pid' => $productId]),
'scd' => $this->getMerchantCode(),
'su' => $this->getSuccessUrl(),
'fu' => $this->getFailureUrl() . '?' . http_build_query(['pid' => $productId]),
'pid' => $productId,
'amt' => $amount,
'txAmt' => $taxAmount,
Expand All @@ -32,7 +32,7 @@ public function process(string $productId, float $amount, float $taxAmount, floa
];

// generate form from attributes
$htmlForm = '<form method="POST" action="' . ($this->config->apiUrl . '/epay/main') . '" id="esewa-form">';
$htmlForm = '<form method="POST" action="' . ($this->getApiUrl() . '/epay/main') . '" id="esewa-form">';

foreach ($formInputs as $name => $value):
$htmlForm .= sprintf('<input name="%s" type="hidden" value="%s">', $name, $value);
Expand All @@ -48,13 +48,13 @@ public function process(string $productId, float $amount, float $taxAmount, floa
* This method verifies the payment using the reference ID.
* @throws Exception
*/
public function verify(string $referenceId, string $productId, float $amount): bool
public function verifyPayment(string $referenceId, string $productId, float $amount): bool
{
// Initialize a cURL handle
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $this->config->apiUrl . '/epay/transrec');
curl_setopt($ch, CURLOPT_URL, $this->getApiUrl() . '/epay/transrec');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Expand All @@ -71,7 +71,7 @@ public function verify(string $referenceId, string $productId, float $amount): b

// Set the request data
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'scd' => $this->config->merchantCode,
'scd' => $this->getMerchantCode(),
'rid' => $referenceId,
'pid' => $productId,
'amt' => $amount,
Expand Down
23 changes: 23 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace RemoteMerge\Esewa;

interface ClientInterface
{
/**
* Client constructor.
*/
public function __construct(array $configs = []);

/**
* This method creates the form in runtime and post the data to eSewa server.
*/
public function payment(string $productId, float $amount, float $taxAmount, float $serviceAmount = 0.0, float $deliveryAmount = 0.0): void;

/**
* This method verifies the payment using the reference ID.
*/
public function verifyPayment(string $referenceId, string $productId, float $amount): bool;
}
69 changes: 65 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,79 @@

namespace RemoteMerge\Esewa;

class Config
class Config implements ConfigInterface
{
/**
* The merchant code provided by eSewa
*/
private string $merchantCode = 'EPAYTEST';

/**
* The API url for development mode
*/
public string $apiUrl = 'https://uat.esewa.com.np';
private string $apiUrl = 'https://uat.esewa.com.np';

/**
* The URL to redirect after successful payment
*/
private string $successUrl = '';

/**
* The URL to redirect after failed payment
*/
private string $failureUrl = '';

public function __construct(public string $successUrl, public string $failureUrl, public string $merchantCode = 'EPAYTEST')
public function __construct(private readonly array $configs = [])
{
if ($merchantCode !== 'EPAYTEST') {
// Update the configuration
$this->merchantCode = $this->configs['merchant_code'] ?? $this->merchantCode;
$this->successUrl = $this->configs['success_url'] ?? $this->successUrl;
$this->failureUrl = $this->configs['failure_url'] ?? $this->failureUrl;

// Set the API URL
$this->setApiUrl();
}

/**
* Set the API URL based on the merchant code
*/
private function setApiUrl(): void
{
if ($this->getMerchantCode() !== 'EPAYTEST') {
// set API URL for production
$this->apiUrl = 'https://esewa.com.np';
}
}

/**
* Get the merchant code
*/
public function getMerchantCode(): string
{
return $this->merchantCode;
}

/**
* Get the API URL
*/
public function getApiUrl(): string
{
return $this->apiUrl;
}

/**
* Get the URL to redirect after successful payment
*/
public function getSuccessUrl(): string
{
return $this->successUrl;
}

/**
* Get the URL to redirect after failed payment
*/
public function getFailureUrl(): string
{
return $this->failureUrl;
}
}
33 changes: 33 additions & 0 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace RemoteMerge\Esewa;

interface ConfigInterface
{
/**
* Config constructor.
*/
public function __construct(array $configs = []);

/**
* Get the merchant code
*/
public function getMerchantCode(): string;

/**
* Get the API URL
*/
public function getApiUrl(): string;

/**
* Get the URL to redirect after successful payment
*/
public function getSuccessUrl(): string;

/**
* Get the URL to redirect after failed payment
*/
public function getFailureUrl(): string;
}
4 changes: 2 additions & 2 deletions tests/Feature/VerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class VerificationTest extends ParentTestCase
*/
public function test_with_invalid_data(): void
{
$response = $this->esewa->verify('Apple', 'Google', 105);
$response = $this->esewa->verifyPayment('Apple', 'Google', 105);
self::assertFalse($response);
}

Expand All @@ -28,7 +28,7 @@ public function test_with_valid_data(): void
$productId = $_ENV['ESEWA_PRODUCT_ID'];
$esewaAmount = (float) $_ENV['ESEWA_PAID_AMOUNT'];

$response = $this->esewa->verify($referenceId, $productId, $esewaAmount);
$response = $this->esewa->verifyPayment($referenceId, $productId, $esewaAmount);
self::assertTrue($response);
}
}
Loading

0 comments on commit d71f696

Please sign in to comment.