Skip to content

Commit

Permalink
Add shepa driver (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadegh19b committed Sep 2, 2024
1 parent a1a9606 commit 65457ac
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ For **Laravel** integration you can use [shetabit/payment](https://github.com/sh
- [sep (saman electronic payment) Keshavarzi & Saderat](https://www.sep.ir) :heavy_check_mark:
- [sepehr (saderat)](https://www.sepehrpay.com/) :heavy_check_mark:
- [sepordeh](https://sepordeh.com/) :heavy_check_mark:
- [shepa](https://shepa.com/) :heavy_check_mark:
- [sizpay](https://www.sizpay.ir/) :heavy_check_mark:
- [snapppay](https://snapppay.ir/) :heavy_check_mark:
- [toman](https://tomanpay.net/) :heavy_check_mark:
Expand Down
17 changes: 17 additions & 0 deletions config/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@
'callbackUrl' => '',
'currency' => 'R', //Can be R, T (Rial, Toman)
],
'shepa' => [
/* Normal api */
'apiPurchaseUrl' => 'https://merchant.shepa.com/api/v1/token',
'apiPaymentUrl' => 'https://merchant.shepa.com/v1/',
'apiVerificationUrl' => 'https://merchant.shepa.com/api/v1/verify',

/* Sandbox api */
'sandboxApiPurchaseUrl' => 'https://sandbox.shepa.com/api/v1/token',
'sandboxApiPaymentUrl' => 'https://sandbox.shepa.com/v1/',
'sandboxApiVerificationUrl' => 'https://sandbox.shepa.com/api/v1/verify',

'sandbox' => false, // Set it to true for test environments
'merchantId' => '', // Set `sandbox` for test environments (sandbox)
'callbackUrl' => '',
'currency' => 'R', //Can be R, T (Rial, Toman)
],
'sizpay' => [
'apiPurchaseUrl' => 'https://rt.sizpay.ir/KimiaIPGRouteService.asmx?WSDL',
'apiPaymentUrl' => 'https://rt.sizpay.ir/Route/Payment',
Expand Down Expand Up @@ -496,6 +512,7 @@
'zibal' => \Shetabit\Multipay\Drivers\Zibal\Zibal::class,
'sepordeh' => \Shetabit\Multipay\Drivers\Sepordeh\Sepordeh::class,
'rayanpay' => \Shetabit\Multipay\Drivers\Rayanpay\Rayanpay::class,
'shepa' => \Shetabit\Multipay\Drivers\Shepa\Shepa::class,
'sizpay' => \Shetabit\Multipay\Drivers\Sizpay\Sizpay::class,
'vandar' => \Shetabit\Multipay\Drivers\Vandar\Vandar::class,
'aqayepardakht' => \Shetabit\Multipay\Drivers\Aqayepardakht\Aqayepardakht::class,
Expand Down
242 changes: 242 additions & 0 deletions src/Drivers/Shepa/Shepa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?php

namespace Shetabit\Multipay\Drivers\Shepa;

use GuzzleHttp\Client;
use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Contracts\ReceiptInterface;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Receipt;
use Shetabit\Multipay\RedirectionForm;
use Shetabit\Multipay\Request;

class Shepa extends Driver
{
/**
* HTTP Client.
*
* @var object
*/
protected $client;

/**
* Invoice
*
* @var Invoice
*/
protected $invoice;

/**
* Driver settings
*
* @var object
*/
protected $settings;

/**
* Shepa constructor.
* Construct the class with the relevant settings.
*
* @param Invoice $invoice
* @param $settings
*/
public function __construct(Invoice $invoice, $settings)
{
$this->invoice($invoice);
$this->settings = (object) $settings;
$this->client = new Client();
}

/**
* Retrieve data from details using its name.
*
* @return string
*/
private function extractDetails($name)
{
return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
}

/**
* Purchase Invoice.
*
* @return string
*
* @throws PurchaseFailedException
*/
public function purchase()
{
$data = [
'api' => $this->settings->merchantId,
'amount' => $this->getInvoiceAmount(),
'callback' => $this->settings->callbackUrl,
'mobile' => $this->extractDetails('mobile'),
'email' => $this->extractDetails('email'),
'cardnumber' => $this->extractDetails('cardnumber'),
'description' => $this->extractDetails('description'),
];

$response = $this->client->request(
'POST',
$this->getPurchaseUrl(),
[
'form_params' => $data,
'http_errors' => false,
]
);

$body = json_decode($response->getBody()->getContents(), true);

if (!empty($body['error']) || !empty($body['errors'])) {
$errors = !empty($body['error'])
? $body['error']
: $body['errors'];

throw new PurchaseFailedException(implode(', ', $errors));
}

$this->invoice->transactionId($body['result']['token']);

// return the transaction's id
return $this->invoice->getTransactionId();
}

/**
* Pay the Invoice
*
* @return RedirectionForm
*/
public function pay(): RedirectionForm
{
$payUrl = $this->getPaymentUrl() . $this->invoice->getTransactionId();

return $this->redirectWithForm($payUrl, [], 'GET');
}

/**
* Verify payment
*
* @return ReceiptInterface
*
* @throws InvalidPaymentException
*/
public function verify(): ReceiptInterface
{
$paymentStatus = Request::input('status');

if ($paymentStatus !== 'success') {
throw new InvalidPaymentException('تراکنش از سوی کاربر لغو شد.', $paymentStatus);
}

$data = [
'api' => $this->settings->merchantId,
'token' => $this->invoice->getTransactionId() ?? Request::input('token'),
'amount' => $this->getInvoiceAmount()
];

$response = $this->client->request(
'POST',
$this->getVerificationUrl(),
[
'json' => $data,
"headers" => [
'Content-Type' => 'application/json',
],
"http_errors" => false,
]
);

$body = json_decode($response->getBody()->getContents(), true);

if (!empty($body['error']) || !empty($body['errors'])) {
$errors = !empty($body['error'])
? $body['error']
: $body['errors'];

throw new InvalidPaymentException(implode(', ', $errors));
}

$refId = $body['result']['refid'];
$receipt = $this->createReceipt($refId);

$receipt->detail([
'refid' => $refId,
'transaction_id' => $body['result']['transaction_id'],
'amount' => $body['result']['amount'],
'card_pan' => $body['result']['card_pan'],
'date' => $body['result']['date'],
]);

return $receipt;
}

/**
* Generate the payment's receipt
*
* @param $referenceId
*
* @return Receipt
*/
public function createReceipt($referenceId)
{
return new Receipt('shepa', $referenceId);
}

/**
* Retrieve invoice amount
*
* @return int|float
*/
protected function getInvoiceAmount()
{
return $this->invoice->getAmount() * (strtolower($this->settings->currency) === 't' ? 10 : 1); // convert to rial
}

/**
* Retrieve purchase url
*
* @return string
*/
protected function getPurchaseUrl(): string
{
return $this->isSandboxMode()
? $this->settings->sandboxApiPurchaseUrl
: $this->settings->apiPurchaseUrl;
}

/**
* Retrieve Payment url
*
* @return string
*/
protected function getPaymentUrl(): string
{
return $this->isSandboxMode()
? $this->settings->sandboxApiPaymentUrl
: $this->settings->apiPaymentUrl;
}

/**
* Retrieve verification url
*
* @return string
*/
protected function getVerificationUrl(): string
{
return $this->isSandboxMode()
? $this->settings->sandboxApiVerificationUrl
: $this->settings->apiVerificationUrl;
}

/**
* Retrieve payment in sandbox mode?
*
* @return bool
*/
protected function isSandboxMode() : bool
{
return $this->settings->sandbox;
}
}

0 comments on commit 65457ac

Please sign in to comment.