Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

اضافه کردن درگاه تومن #209

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@
'callbackUrl' => '',
'apiKey' => '',
'currency' => 'T', //Can be R, T (Rial, Toman)
],
'toman' => [
'base_url' => 'https://escrow-api.toman.ir/api/v1',
'shop_slug' => '',
'auth_code' => '',
'data' => ''
]
],

Expand Down Expand Up @@ -441,5 +447,6 @@
'aqayepardakht' => \Shetabit\Multipay\Drivers\Aqayepardakht\Aqayepardakht::class,
'azki' => \Shetabit\Multipay\Drivers\Azki\Azki::class,
'payfa' => \Shetabit\Multipay\Drivers\Payfa\Payfa::class,
'toman' => \Shetabit\Multipay\Drivers\Toman\Toman::class,
]
];
100 changes: 100 additions & 0 deletions src/Drivers/Toman/Toman.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Shetabit\Multipay\Drivers\Toman;

use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Receipt;
use Illuminate\Support\Facades\Http;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

دوست عزیز لطفا یک PR ایجاد کنید و این دپندنسی رو با guzzle جایگزین کنید. این پکیج PHP هست و این موردی که شما گذاشتی جزو دپندنسی های پکیج نیست.

use Shetabit\Multipay\RedirectionForm;
use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Contracts\ReceiptInterface;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;

class Toman extends Driver
{
protected $invoice; // Invoice.

protected $settings; // Driver settings.

protected $base_url;

protected $shop_slug;

protected $auth_code;

protected $code;

protected $auth_token;

public function __construct(Invoice $invoice, $settings)
{
$this->invoice($invoice); // Set the invoice.
$this->settings = (object) $settings; // Set settings.
$this->base_url = $this->settings->base_url;
$this->shop_slug = $this->settings->shop_slug;
$this->auth_code = $this->settings->auth_code;
$this->code = $this->shop_slug . ':' . $this->auth_code;
$this->auth_token = base64_encode($this->code);
}

// Purchase the invoice, save its transactionId and finaly return it.
public function purchase()
{
$url = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals";
$data = $this->settings->data;

$response = Http::withHeaders([
'Authorization' => "Basic {$this->auth_token}",
"Content-Type" => 'application/json'
])->post($url, $data);

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

if (isset($result['trace_number'])) {
$this->invoice->transactionId($result['trace_number']);
return $this->invoice->getTransactionId();
} else {
throw new InvalidPaymentException('پرداخت با مشکل مواجه شد، لطفا با ما در ارتباط باشید');
}
}

// Redirect into bank using transactionId, to complete the payment.
public function pay(): RedirectionForm
{
$transactionId = $this->invoice->getTransactionId();
$redirect_url = $this->base_url . '/deals/' . $transactionId . '/redirect';

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

// Verify the payment (we must verify to ensure that user has paid the invoice).
public function verify(): ReceiptInterface
{
$inputs = request()->all();

$transactionId = $this->invoice->getTransactionId();
$verifyUrl = $this->base_url . "/users/me/shops/" . $this->shop_slug . "/deals/" . $transactionId . "/verify";

if ($inputs['state'] == 'funded') {
$result = Http::withHeaders([
'Authorization' => "Basic {$this->auth_token}",
"Content-Type" => 'application/json'
])->patch($verifyUrl);
return $this->createReceipt($transactionId);
} else {
throw new InvalidPaymentException('پرداخت انجام نشد');
}
}

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