Skip to content

Commit

Permalink
Merge pull request #49 from farayaz/48-add-refahbeta-gateway
Browse files Browse the repository at this point in the history
add RefahBeta gateway
  • Loading branch information
mehrdadx10 authored Jun 1, 2024
2 parents b33e1f7 + 199308c commit 36becd9
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 27 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ Larapay is a Laravel package for integrating Iranian payment gateways.

## Gateways | درگاه‌ها

| Class | Name (en) | Name (fa) | Requirements |
| ------------------- | ---------------------------------------------- | ---------------------------- | ----------------------------------------------------- |
| **Azkivam** | [Azkivam](https://azkivam.com/) | ازکی وام | `merchant_id`,`api_key` |
| **BehPardakht** | [Beh Pardakht Mellat](https://behpardakht.com) | به‌پرداخت ملت | `terminalId`, `username`, `password` |
| **Digipay** | [Digipay](https://www.mydigipay.com) | دیجی‌پی | `username`, `password`, `client_id`, `client_secret` |
| **IdPay** | [IdPay](https://idpay.ir) | آیدی‌پی | `apiKey`, `sandbox` |
| **IranKish** | [Iran Kish](https://www.irankish.com) | ایران کیش | `terminalId`, `password`, `acceptorId`, `pubKey` |
| **IsipaymentSamin** | [Isipayment Samin](https://isipayment.ir) | ایزایران ثمین | `merchant_code`, `merchant_password`, `terminal_code` |
| **Keepa** | [Keepa - Kipaa](https://keepa.ir) | کیپا | `token` |
| **MehrIran** | [MehrIran](https://qmb.ir) | بانک مهر ایران | `terminal_id`, `merchant_nid`, `encrypt_key` |
| **Omidpay** | [Omidpay - Sayan Card](https://omidpayment.ir) | امید پی (سایان کارت) | `user_id`, `password` |
| **PardakhtNovin** | [Pardakht Novin](https://pna.co.ir) | پرداخت نوین | `userId`, `password`, `terminalId` |
| **Payir** | [Pay.ir](https://www.pay.ir) | پی.آی‌آر | `api` |
| **PayPing** | [PayPing](https://payping.ir) | پی پینگ | `token` |
| **Polam** | [Polam(Poolam)](https://polam.io) | پولام | `api_key` |
| **Sadad** | [Sadad](https://sadadpsp.ir) | پرداخت الکترونیک سداد (ملی) | `terminal_id`, `merchant_id`, `key` |
| **Sep** | [Saman Electronic Payment](https://www.sep.ir) | پرداخت الکترونیک سامان (سپ) | `terminalId` |
| **SepehrPay** | [Sepehr Pay](https://www.sepehrpay.com) | پرداخت الکترونیک سپهر (مبنا) | `terminalId` |
| **SnappPay** | [SnappPay](https://snapppay.ir) | اسنپ‌پی | `username`, `password`, `client_id`, `client_secret` |
| **TejaratBajet** | [Tejarat Bajet](https://mybajet.ir) | بانک تجارت - باجت | `client_id`, `client_secret`, `sandbox` |
| **ZarinPal** | [Zarin Pal](https://www.zarinpal.com) | زرین پال | `merchant_id` |
| **Zibal** | [Zibal](https://zibal.ir) | زیبال | `merchant` |
| ... | | | |

If you don't find the gate you want, let us know or contribute to add it
| Class | Name (en) | Name (fa) | Requirements |
| ------------------- | ---------------------------------------------- | ---------------------------- | ------------------------------------------------------------------ |
| **Azkivam** | [Azkivam](https://azkivam.com/) | ازکی وام | `merchant_id`,`api_key` |
| **BehPardakht** | [Beh Pardakht Mellat](https://behpardakht.com) | به‌پرداخت ملت | `terminalId`, `username`, `password` |
| **Digipay** | [Digipay](https://www.mydigipay.com) | دیجی‌پی | `username`, `password`, `client_id`, `client_secret` |
| **IdPay** | [IdPay](https://idpay.ir) | آیدی‌پی | `apiKey`, `sandbox` |
| **IranKish** | [Iran Kish](https://www.irankish.com) | ایران کیش | `terminalId`, `password`, `acceptorId`, `pubKey` |
| **IsipaymentSamin** | [Isipayment Samin](https://isipayment.ir) | ایزایران ثمین | `merchant_code`, `merchant_password`, `terminal_code` |
| **Keepa** | [Keepa - Kipaa](https://keepa.ir) | کیپا | `token` |
| **MehrIran** | [MehrIran](https://qmb.ir) | بانک مهر ایران | `terminal_id`, `merchant_nid`, `encrypt_key` |
| **Omidpay** | [Omidpay - Sayan Card](https://omidpayment.ir) | امید پی (سایان کارت) | `user_id`, `password` |
| **PardakhtNovin** | [Pardakht Novin](https://pna.co.ir) | پرداخت نوین | `userId`, `password`, `terminalId` |
| **Payir** | [Pay.ir](https://www.pay.ir) | پی.آی‌آر | `api` |
| **PayPing** | [PayPing](https://payping.ir) | پی پینگ | `token` |
| **Polam** | [Polam(Poolam)](https://polam.io) | پولام | `api_key` |
| **RefahBeta** | [Refah Beta](https://beta.refah-bank.ir) | بانک رفاه بتا | `client_id` , `client_secret`, `api_key`, `number_of_installments` |
| **Sadad** | [Sadad](https://sadadpsp.ir) | پرداخت الکترونیک سداد (ملی) | `terminal_id`, `merchant_id`, `key` |
| **Sep** | [Saman Electronic Payment](https://www.sep.ir) | پرداخت الکترونیک سامان (سپ) | `terminalId` |
| **SepehrPay** | [Sepehr Pay](https://www.sepehrpay.com) | پرداخت الکترونیک سپهر (مبنا) | `terminalId` |
| **SnappPay** | [SnappPay](https://snapppay.ir) | اسنپ‌پی | `username`, `password`, `client_id`, `client_secret` |
| **TejaratBajet** | [Tejarat Bajet](https://mybajet.ir) | بانک تجارت - باجت | `client_id`, `client_secret`, `sandbox` |
| **ZarinPal** | [Zarin Pal](https://www.zarinpal.com) | زرین پال | `merchant_id` |
| **Zibal** | [Zibal](https://zibal.ir) | زیبال | `merchant` |

If you don't find the gateway you want, let us know or contribute to add it
****
اگر درگاه مورد نظر خود را پیدا نکردید، به ما اطلاع دهید یا در اضافه کردن آن مشارکت کنید

Expand Down
File renamed without changes.
159 changes: 159 additions & 0 deletions src/Gateways/RefahBeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Farayaz\Larapay\Gateways;

use Farayaz\Larapay\Exceptions\LarapayException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\View;
use Morilog\Jalali\Jalalian;

final class RefahBeta extends GatewayAbstract
{
protected $statuses = [
'invalid_client' => 'invalid_client: خطای سرویس گیرنده',
'not-enough-credit' => 'اعتبار کافی نیست، اعتبار ماهانه: ',
'rial' => ' ریال',
];

protected $requirements = [
'client_id',
'client_secret',
'api_key',
'number_of_installments',
];

public function request(
int $id,
int $amount,
string $nationalId,
string $mobile,
string $callbackUrl
): array {

$url = 'beta/1.0/credit/' . $nationalId . '/inquiry';
$result = $this->_request('post', $url);
if ($result['status'] != 200) {
throw new LarapayException($result['message']);
}
if ($amount / $this->config['number_of_installments'] > $result['data']['credit']) {
$message = $this->translateStatus('not-enough-credit');
$message .= number_format($result['data']['credit']);
$message .= $this->translateStatus('rial');
throw new LarapayException($message);
}

$url = 'beta/1.0/credit/' . $nationalId . '/request';
$data = [
'title' => 'transaction' . $id,
'startDate' => Jalalian::now()->addMonths()->getEndDayOfMonth()->toCarbon()->toIso8601String(),
'amount' => $amount,
'numberOfInstallments' => $this->config['number_of_installments'],
'requestId' => $id,
];
$result = $this->_request('post', $url, $data);
if ($result['status'] != 200) {
throw new LarapayException($result['message']);
}

return [
'token' => $id,
'fee' => 0,
];
}

public function redirect(int $id, string $token, string $callbackUrl)
{
return View::make('larapay::otp', compact('callbackUrl'));
}

public function verify(
int $id,
int $amount,
string $nationalId,
string $mobile,
string $token,
array $params
): array {
$default = [
'otp' => null,
];
$params = array_merge($default, $params);

$url = 'beta/1.0/credit/' . $nationalId . '/consume';
$data = [
'otp' => $params['otp'],
'title' => 'transaction' . $id,
'startDate' => Jalalian::now()->addMonths()->getEndDayOfMonth()->toCarbon()->toIso8601String(),
'amount' => $amount,
'numberOfInstallments' => $this->config['number_of_installments'],
'requestId' => $id,
];
$result = $this->_request('post', $url, $data);
if ($result['status'] != 200) {
throw new LarapayException($result['message']);
}

return [
'result' => 'OK',
'card' => null,
'tracking_code' => $result['data'],
'reference_id' => $result['data'],
'fee' => 0,
];
}

private function authenticate()
{
if (Cache::get(__CLASS__ . 'token')) {
return Cache::get(__CLASS__ . 'token');
}

$data = [
'client_id' => $this->config['client_id'],
'client_secret' => $this->config['client_secret'],
'grant_type' => 'client_credentials',
];
$result = $this->_request('post', 'connect/token', $data);

Cache::put(__CLASS__ . 'token', $result['access_token'], $result['expires_in'] - 10);

return $result['access_token'];
}

private function _request($method, $url, array $data = [], array $headers = [])
{
$fullUrl = 'https://api.rb24.ir/' . $url;
$as = 'asJson';

if ($url == 'connect/token') {
$as = 'asForm';
} else {
$headers['Authorization'] = 'Bearer ' . $this->authenticate();
$headers['apikey'] = $this->config['api_key'];
}

try {
return Http::timeout(10)
->$as()
->withHeaders($headers)
->$method($fullUrl, $data)
->throw()
->json();
} catch (RequestException $e) {
$message = $e->getMessage();
$result = $e->response->json();
if (isset($result['title'])) {
$message = $result['title'];
}
if (isset($result['errors'])) {
$message = implode(', ', array_merge(...array_values($result['errors'])));
}
throw new LarapayException('بتا رفاه: ' . $message);
} catch (ConnectionException $e) {
throw new LarapayException($this->translateStatus('connection-exception'));
}
}
}
2 changes: 1 addition & 1 deletion src/Gateways/TejaratBajet.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function request(

public function redirect(int $id, string $token, string $callbackUrl)
{
return View::make('larapay::tejarat-bajet', compact('callbackUrl'));
return View::make('larapay::otp', compact('callbackUrl'));
}

public function verify(
Expand Down
2 changes: 1 addition & 1 deletion src/LarapayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function boot(): void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'larapay');
// $this->publishes([
// __DIR__ . '/../resources/views' => resource_path('views/vendor/courier'),
// __DIR__ . '/../resources/views' => resource_path('views/vendor/larapay'),
// ]);
}
}

0 comments on commit 36becd9

Please sign in to comment.