From d8f48f67d6c7b4c02198d4c192525d3c00b9b96c Mon Sep 17 00:00:00 2001 From: Zura Gabievi Date: Wed, 18 Nov 2020 16:34:38 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=90=20Created=20methods=20to=20add=20p?= =?UTF-8?q?arameters=20separately=20per=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 199 +++++++++++++++ src/Contracts/IPay.php | 332 ++++++++++++++++++++++++ src/IPay.php | 568 +++++++++++++++++++++++++---------------- src/config/ipay.php | 2 +- 4 files changed, 883 insertions(+), 218 deletions(-) create mode 100644 src/Contracts/IPay.php diff --git a/README.md b/README.md index ea3afab..266e2c9 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,34 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; +use Zorb\IPay\Enums\Intent; +use Zorb\IPay\Enums\CaptureMethod; + +class PaymentController +{ + public function __invoke() + { + $order_id = 1; + + $response = IPay::setIntent(Intent::Capture) + ->setShopOrder($order_id) + ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) + ->setItems([ + IPay::purchaseItem(1, 10, 1, 'Item #1'), + IPay::purchaseItem(2, 10, 1, 'Item #2'), + ]) + ->setToken('IPAY_JWT_TOKEN') + ->setCaptureMethod(CaptureMethod::Manual) + ->setTransaction('IPAY_TRANSACTION_ID') + ->checkout(); + } +} +``` + Example `$response`: ```json @@ -178,6 +206,40 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; +use Zorb\IPay\Enums\Intent; +use Zorb\IPay\Enums\CaptureMethod; +use Zorb\IPay\Enums\CheckoutStatus; + +class PaymentController +{ + public function __invoke() + { + $order_id = 1; + + $response = IPay::setIntent(Intent::Capture) + ->setShopOrder($order_id) + ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) + ->setItems([ + IPay::purchaseItem(1, 10, 1, 'Item #1'), + IPay::purchaseItem(2, 10, 1, 'Item #2'), + ]) + ->setToken('IPAY_JWT_TOKEN') + ->setCaptureMethod(CaptureMethod::Manual) + ->setTransaction('IPAY_TRANSACTION_ID') + ->checkout(); + + if (isset($response->status) && $response->status === CheckoutStatus::Created) { + return IPay::setResponse($response)->redirect(); + // IPay::setResponse($response)->redirectUrl(); + } + } +} +``` + Redirect method will find redirect link for payment and redirect user to that page. ### Recurring @@ -215,6 +277,37 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; +use Zorb\IPay\Enums\Intent; +use Zorb\IPay\Enums\CheckoutStatus; + +class PaymentController +{ + public function __invoke() + { + $order_id = 1; + $transaction_id = '899318B1CE0D5885CB7'; + + $response = IPay::setIntent(Intent::Capture) + ->setShopOrder($order_id) + ->setPurchaseUnits([ IPay::purchaseUnit(10) ]) + ->setItems([ + IPay::purchaseItem(1, 10, 1, 'Item #1'), + IPay::purchaseItem(2, 10, 1, 'Item #2'), + ]) + ->setTransaction($transaction_id) + ->repeat(); + + if (isset($response->status) && $response->status === CheckoutStatus::Created) { + return IPay::setResponse($response)->redirect(); + } + } +} +``` + ### Refund In order to refund money you need to have order_id of payment. @@ -236,6 +329,24 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; + +class PaymentController +{ + public function __invoke() + { + $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; + + $response = IPay::setOrder($order_id) + ->setAmount(10) + ->refund(); + } +} +``` + If response is OK, it means refund process was successful. ### Order Details @@ -258,6 +369,23 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; + +class PaymentController +{ + public function __invoke() + { + $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; + + $response = IPay::setOrder($order_id) + ->orderDetails(); + } +} +``` + Example `$response`: ```json @@ -336,6 +464,23 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; + +class PaymentController +{ + public function __invoke() + { + $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; + + $response = IPay::setOrder($order_id) + ->orderStatus(); + } +} +``` + Example `$response`: ```json @@ -364,6 +509,23 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; + +class PaymentController +{ + public function __invoke() + { + $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; + + $response = IPay::setOrder($order_id) + ->paymentDetails(); + } +} +``` + Example `$response`: ```json @@ -402,6 +564,43 @@ class PaymentController } ``` +You can set parameters separately: + +```php +use Zorb\IPay\Facades\IPay; + +class PaymentController +{ + public function __invoke() + { + $order_id = '899318b1ce0d5885cb7405fe86e3930178ff90be'; + + $response = IPay::setOrder($order_id) + ->completePreAuth(); + } +} +``` + +## All helper methods to set parameters separately +| Method | Description | Possible values | Default | Used in | +| :----- | :---------- | :-------------- | :------ | :------ | +| setResponse | Response from other iPay request | - | - | redirect, redirectUrl | +| setRel | Rel to point correct link from response | approve, self | approve | redirect, redirectUrl | +| setAmount | Money amount in cents (tetris) | - | - | purchaseUnit, purchaseItem, refund | +| setCurrency | Currency of the amount | GEL, USD, EUR | GEL | purchaseUnit | +| setIndustryType | Industry type of purchase unit | ECOMMERCE | ECOMMERCE | purchaseUnit | +| setProduct | Your product id | - | - | purchaseItem | +| setQuantity | Quantity of purchase item | - | - | purchaseItem | +| setDescription | Description of purchase item | - | - | purchaseItem | +| setTransaction | Transaction id for recurring | - | - | checkout, repeat | +| setIntent | Intent for payment | CAPTURE, AUTHORIZE, LOAN | CAPTURE | checkout, repeat | +| setShopOrder | Your order id | - | - | checkout, repeat | +| setPurchaseUnits | One purchase unit as an array | - | - | checkout, repeat | +| setItems | List of items of purchase | - | - | checkout, repeat | +| setToken | JWT Token from iPay | - | - | checkout, repeat, refund, orderDetails, orderStatus, paymentDetails, completePreAuth | +| setCaptureMethod | Method for checkout | AUTOMATIC, MANUAL | AUTOMATIC | checkout, repeat | +| setOrder | Order id from iPay responses | - | - | refund, orderDetails, orderStatus, paymentDetails, completePreAuth | + ## Environment Variables | Key | Meaning | Type | Default | diff --git a/src/Contracts/IPay.php b/src/Contracts/IPay.php new file mode 100644 index 0000000..dace243 --- /dev/null +++ b/src/Contracts/IPay.php @@ -0,0 +1,332 @@ +links)) { + $link = collect($response->links)->filter(function ($item) use ($rel) { + return isset($item->rel) && $item->rel === $rel; + })->first(); + + if (!$link || !isset($link->href)) { + return back(); + } + + return redirect($link->href); + } + + return back(); + } + + /** + * Get redirect url + * + * @param \stdClass $response + * @param string $rel + * @return string|null + */ + public function redirectUrl(\stdClass $response, string $rel = 'approve'): ?string + { + if (isset($response->links)) { + $link = collect($response->links)->filter(function ($item) use ($rel) { + return isset($item->rel) && $item->rel === $rel; + })->first(); + + if (!$link || !isset($link->href)) { + return back(); + } + + return $link->href; + } + + return null; + } + + /** + * Generate array for purchase unit + * + * @param int $amount + * @param string|null $currency + * @param string|null $industry_type + * @return array + */ + public function purchaseUnit(int $amount, string $currency = null, string $industry_type = null): array + { + return [ + 'amount' => [ + 'currency_code' => $currency ?: Currency::GEL, + 'value' => $amount / 100, + ], + 'industry_type' => $industry_type ?: IndustryType::Ecommerce, + ]; + } + + /** + * Generate array for purchase item + * + * @param int $product_id + * @param int $amount + * @param int $quantity + * @param string $description + * @return array + */ + public function purchaseItem(int $product_id, int $amount, int $quantity = 1, string $description = ''): array + { + return [ + 'amount' => $amount / 100, + 'description' => $description, + 'quantity' => $quantity, + 'product_id' => $product_id, + ]; + } + + /** + * Recurring method to repeat transaction + * + * @param string $transaction_id + * @param string $intent + * @param string|null $token + * @param int $order_id + * @param array $units + * @param array $items + * @param string $capture_method + * @return \stdClass|void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function repeat(string $transaction_id, string $intent, int $order_id, array $units, array $items = [], string $token = null, string $capture_method = ''): \stdClass + { + return $this->checkout($intent, $order_id, $units, $items, $token, $capture_method, $transaction_id); + } + + /** + * Start checkout process + * + * @param string $intent + * @param int $order_id + * @param array $units + * @param string|null $token + * @param array $items + * @param string $capture_method + * @param string $transaction_id + * @return \stdClass|void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function checkout(string $intent, int $order_id, array $units, array $items = [], string $token = null, string $capture_method = '', string $transaction_id = ''): \stdClass + { + $url = config('ipay.url') . '/checkout/orders'; + + return $this->postRequest($url, [ + 'intent' => $intent, + 'redirect_url' => url(config('ipay.redirect_url')) . '?order_id=' . $order_id, + 'shop_order_id' => $order_id, + 'locale' => config('ipay.language'), + 'show_shop_order_id_on_extract' => true, + 'capture_method' => $capture_method ?: CaptureMethod::Automatic, + 'card_transaction_id' => $transaction_id, + 'purchase_units' => $units, + 'items' => $items, + ], $token, null, 'json'); + } + + /** + * Reusable http post request + * + * @param string $url + * @param array $data + * @param string|null $token + * @param string|null $authorization + * @param string $type + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function postRequest(string $url, array $data, string $token = null, string $authorization = null, string $type = 'form_params'): \stdClass + { + if (!$authorization) { + $token = $this->requestToken($token); + } + + $client = new Client(); + try { + $params = $type === 'json' ? ['json' => $data] : ['form_params' => $data]; + $response = $client->post($url, array_merge($params, [ + 'headers' => [ + 'Authorization' => $authorization ?: 'Bearer ' . $token, + ], + ])); + } catch (ClientException $exception) { + $error = json_decode($exception->getResponse()->getBody()); + + if (!isset($error->error_code)) { + return $error; + } + + abort($error->error_code, isset($error->error_message) ? $error->error_message : ''); + } + + return json_decode($response->getBody()); + } + + /** + * Use or get token for next request + * + * @param string|null $token + * @return string + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function requestToken(string $token = null): string + { + if (!$token) { + $request = self::token(); + + if (isset($request->access_token)) { + return $request->access_token; + } + } + + return $token; + } + + /** + * Request token for other operations + * + * @return \stdClass|void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function token() + { + $client_id = config('ipay.client_id'); + $secret_key = config('ipay.secret_key'); + $url = config('ipay.url') . '/oauth2/token'; + $authorization = 'Basic ' . base64_encode($client_id . ':' . $secret_key); + + return $this->postRequest($url, [ + 'grant_type' => 'client_credentials', + ], null, $authorization); + } + + /** + * Refund some amount back + * + * @param string $order_id + * @param int $amount + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function refund(string $order_id, int $amount, string $token = null): \stdClass + { + $url = config('ipay.url') . '/checkout/refund'; + + return $this->postRequest($url, [ + 'order_id' => $order_id, + 'amount' => $amount / 100, + ], $token); + } + + /** + * Get details of order + * + * @param string $order_id + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function orderDetails(string $order_id, string $token = null): \stdClass + { + $url = config('ipay.url') . '/checkout/orders/' . $order_id; + + return $this->getRequest($url, $token); + } + + /** + * Reusable http get request + * + * @param string $url + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function getRequest(string $url, string $token = null): \stdClass + { + $token = $this->requestToken($token); + $client = new Client(); + try { + $response = $client->get($url, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $token, + ], + ]); + } catch (ClientException $exception) { + $error = json_decode($exception->getResponse()->getBody()); + + if (!isset($error->error_code)) { + return $error; + } + + abort($error->error_code, isset($error->error_message) ? $error->error_message : ''); + } + + return json_decode($response->getBody()); + } + + /** + * Get status of order + * + * @param string $order_id + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function orderStatus(string $order_id, string $token = null): \stdClass + { + $url = config('ipay.url') . '/checkout/orders/status/' . $order_id; + + return $this->getRequest($url, $token); + } + + /** + * Get details of payment + * + * @param string $order_id + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function paymentDetails(string $order_id, string $token = null): \stdClass + { + $url = config('ipay.url') . '/checkout/payment/' . $order_id; + + return $this->getRequest($url, $token); + } + + /** + * Complete pre authorized orders + * + * @param string $order_id + * @param string|null $token + * @return \stdClass + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function completePreAuth(string $order_id, string $token = null): \stdClass + { + $url = config('ipay.url') . '/checkout/payment/pre-auth/complete/' . $order_id; + + return $this->getRequest($url, $token); + } +} diff --git a/src/IPay.php b/src/IPay.php index fd50a4a..ab20add 100644 --- a/src/IPay.php +++ b/src/IPay.php @@ -2,331 +2,465 @@ namespace Zorb\IPay; -use GuzzleHttp\Client; +use Zorb\IPay\Enums\Intent; use Zorb\IPay\Enums\Currency; use Zorb\IPay\Enums\IndustryType; use Zorb\IPay\Enums\CaptureMethod; -use GuzzleHttp\Exception\ClientException; +use Illuminate\Http\RedirectResponse; +use Zorb\IPay\Contracts\IPay as IPayContract; class IPay { /** - * Redirect to payment link - * - * @param \stdClass $response - * @param string $rel - * @return \Illuminate\Http\RedirectResponse + * @var \stdClass */ - public function redirect(\stdClass $response, string $rel = 'approve'): \Illuminate\Http\RedirectResponse - { - if (isset($response->links)) { - $link = collect($response->links)->filter(function ($item) use ($rel) { - return isset($item->rel) && $item->rel === $rel; - })->first(); + protected $response; - if (!$link || !isset($link->href)) { - return back(); - } + /** + * @var string + */ + protected $rel = 'approve'; + + /** + * @var int + */ + protected $amount; + + /** + * @var string + */ + protected $currency = Currency::GEL; + + /** + * @var string + */ + protected $industry_type = IndustryType::Ecommerce; + + /** + * @var int + */ + protected $product_id; + + /** + * @var int + */ + protected $quantity = 1; - return redirect($link->href); - } + /** + * @var string + */ + protected $description = ''; + + /** + * @var string + */ + protected $txn_id = ''; + + /** + * @var string + */ + protected $intent = Intent::Capture; + + /** + * @var string|int + */ + protected $shop_order_id; + + /** + * @var array + */ + protected $purchase_units = []; + + /** + * @var array + */ + protected $items = []; + + /** + * @var string + */ + protected $token; - return back(); + /** + * @var string + */ + protected $order_id; + + /** + * @var string + */ + protected $capture_method = CaptureMethod::Automatic; + + /** + * @var IPayContract + */ + protected $ipay; + + /** + * IPay constructor. + * @param IPayContract $ipay + */ + public function __construct(IPayContract $ipay) + { + $this->ipay = $ipay; } /** - * Get redirect url - * * @param \stdClass $response - * @param string $rel - * @return string|null + * @return $this */ - public function redirectUrl(\stdClass $response, string $rel = 'approve'): ?string + public function setResponse(\stdClass $response): self { - if (isset($response->links)) { - $link = collect($response->links)->filter(function ($item) use ($rel) { - return isset($item->rel) && $item->rel === $rel; - })->first(); + $this->response = $response; + return $this; + } - if (!$link || !isset($link->href)) { - return back(); - } + /** + * @param string $rel + * @return $this + */ + public function setRel(string $rel): self + { + $this->rel = $rel; + return $this; + } - return $link->href; - } + /** + * @param int $amount + * @return $this + */ + public function setAmount(int $amount): self + { + $this->amount = $amount; + return $this; + } - return null; + /** + * @param string $currency + * @return $this + */ + public function setCurrency(string $currency): self + { + $this->currency = $currency; + return $this; } /** - * Generate array for purchase unit - * - * @param int $amount - * @param string|null $currency - * @param string|null $industry_type - * @return array + * @param string $industry_type + * @return $this */ - public function purchaseUnit(int $amount, string $currency = null, string $industry_type = null): array + public function setIndustryType(string $industry_type): self { - return [ - 'amount' => [ - 'currency_code' => $currency ?: Currency::GEL, - 'value' => $amount / 100, - ], - 'industry_type' => $industry_type ?: IndustryType::Ecommerce, - ]; + $this->industry_type = $industry_type; + return $this; } /** - * Generate array for purchase item - * * @param int $product_id - * @param int $amount + * @return $this + */ + public function setProduct(int $product_id): self + { + $this->product_id = $product_id; + return $this; + } + + /** * @param int $quantity + * @return $this + */ + public function setQuantity(int $quantity): self + { + $this->quantity = $quantity; + return $this; + } + + /** * @param string $description - * @return array + * @return $this */ - public function purchaseItem(int $product_id, int $amount, int $quantity = 1, string $description = ''): array + public function setDescription(string $description): self { - return [ - 'amount' => $amount / 100, - 'description' => $description, - 'quantity' => $quantity, - 'product_id' => $product_id, - ]; + $this->description = $description; + return $this; } /** - * Recurring method to repeat transaction - * - * @param string $transaction_id - * @param string $intent - * @param string|null $token - * @param int $order_id - * @param array $units - * @param array $items - * @param string $capture_method - * @return \stdClass|void - * @throws \GuzzleHttp\Exception\GuzzleException + * @param string $txn_id + * @return $this */ - public function repeat(string $transaction_id, string $intent, int $order_id, array $units, array $items = [], string $token = null, string $capture_method = ''): \stdClass + public function setTransaction(string $txn_id): self { - return $this->checkout($intent, $order_id, $units, $items, $token, $capture_method, $transaction_id); + $this->txn_id = $txn_id; + return $this; } /** - * Start checkout process - * * @param string $intent - * @param int $order_id + * @return $this + */ + public function setIntent(string $intent): self + { + $this->intent = $intent; + return $this; + } + + /** + * @param string|int $shop_order_id + * @return $this + */ + public function setShopOrder($shop_order_id): self + { + $this->shop_order_id = $shop_order_id; + return $this; + } + + /** * @param array $units - * @param string|null $token + * @return $this + */ + public function setPurchaseUnits(array $units = []): self + { + $this->purchase_units = $units; + return $this; + } + + /** * @param array $items + * @return $this + */ + public function setItems(array $items = []): self + { + $this->items = $items; + return $this; + } + + /** + * @param string $token + * @return $this + */ + public function setToken(string $token): self + { + $this->token = $token; + return $this; + } + + /** * @param string $capture_method - * @param string $transaction_id - * @return \stdClass|void - * @throws \GuzzleHttp\Exception\GuzzleException + * @return $this */ - public function checkout(string $intent, int $order_id, array $units, array $items = [], string $token = null, string $capture_method = '', string $transaction_id = ''): \stdClass + public function setCaptureMethod(string $capture_method): self { - $url = config('ipay.url') . '/checkout/orders'; - - return $this->postRequest($url, [ - 'intent' => $intent, - 'redirect_url' => url(config('ipay.redirect_url')) . '?order_id=' . $order_id, - 'shop_order_id' => $order_id, - 'locale' => config('ipay.language'), - 'show_shop_order_id_on_extract' => true, - 'capture_method' => $capture_method ?: CaptureMethod::Automatic, - 'card_transaction_id' => $transaction_id, - 'purchase_units' => $units, - 'items' => $items, - ], $token, null, 'json'); + $this->capture_method = $capture_method; + return $this; } /** - * Reusable http post request - * - * @param string $url - * @param array $data - * @param string|null $token - * @param string|null $authorization - * @param string $type - * @return \stdClass - * @throws \GuzzleHttp\Exception\GuzzleException + * @param string $order_id + * @return $this */ - protected function postRequest(string $url, array $data, string $token = null, string $authorization = null, string $type = 'form_params'): \stdClass + public function setOrder(string $order_id): self { - if (!$authorization) { - $token = $this->requestToken($token); - } - - $client = new Client(); - try { - $params = $type === 'json' ? ['json' => $data] : ['form_params' => $data]; - $response = $client->post($url, array_merge($params, [ - 'headers' => [ - 'Authorization' => $authorization ?: 'Bearer ' . $token, - ], - ])); - } catch (ClientException $exception) { - $error = json_decode($exception->getResponse()->getBody()); - - if (!isset($error->error_code)) { - return $error; - } - - abort($error->error_code, isset($error->error_message) ? $error->error_message : ''); - } - - return json_decode($response->getBody()); + $this->order_id = $order_id; + return $this; } /** - * Use or get token for next request - * - * @param string|null $token - * @return string - * @throws \GuzzleHttp\Exception\GuzzleException + * @param IPayContract $ipay + * @param \stdClass|null $response + * @param string|null $rel + * @return \Illuminate\Http\RedirectResponse + */ + public function redirect(\stdClass $response = null, string $rel = null): RedirectResponse + { + return $this->ipay->redirect( + $response ?: $this->response, + $rel ?: $this->rel + ); + } + + /** + * @param IPayContract $ipay + * @param \stdClass|null $response + * @param string|null $rel + * @return string|null */ - protected function requestToken(string $token = null): string + public function redirectUrl(\stdClass $response = null, string $rel = null): ?string { - if (!$token) { - $request = self::token(); + return $this->ipay->redirectUrl( + $response ?: $this->response, + $rel ?: $this->rel + ); + } - if (isset($request->access_token)) { - return $request->access_token; - } - } + /** + * @param IPayContract $ipay + * @param int|null $amount + * @param string|null $currency + * @param string|null $industry_type + * @return array + */ + public function purchaseUnit(int $amount = null, string $currency = null, string $industry_type = null): array + { + return $this->ipay->purchaseUnit( + $amount ?: $this->amount, + $currency ?: $this->currency, + $industry_type ?: $this->industry_type + ); + } - return $token; + /** + * @param IPayContract $ipay + * @param int|null $product_id + * @param int|null $amount + * @param int|null $quantity + * @param string|null $description + * @return array + */ + public function purchaseItem(int $product_id = null, int $amount = null, int $quantity = null, string $description = null): array + { + return $this->ipay->purchaseItem( + $product_id ?: $this->product_id, + $amount ?: $this->amount, + $quantity ?: $this->quantity, + $description ?: $this->description + ); } /** - * Request token for other operations - * + * @param IPayContract $ipay + * @param string|null $txn_id + * @param string|null $intent + * @param string|int|null $shop_order_id + * @param array|null $purchase_units + * @param array|null $items + * @param string|null $token + * @param string|null $capture_method * @return \stdClass|void * @throws \GuzzleHttp\Exception\GuzzleException */ - public function token() + public function repeat(string $txn_id = null, string $intent = null, $shop_order_id = null, array $purchase_units = null, array $items = null, string $token = null, string $capture_method = null): ?\stdClass { - $client_id = config('ipay.client_id'); - $secret_key = config('ipay.secret_key'); - $url = config('ipay.url') . '/oauth2/token'; - $authorization = 'Basic ' . base64_encode($client_id . ':' . $secret_key); - - return $this->postRequest($url, [ - 'grant_type' => 'client_credentials', - ], null, $authorization); + return $this->ipay->repeat( + $txn_id ?: $this->txn_id, + $intent ?: $this->intent, + $shop_order_id ?: $this->shop_order_id, + $purchase_units ?: $this->purchase_units, + $items ?: $this->items, + $token ?: $this->token, + $capture_method ?: $this->capture_method + ); } /** - * Refund some amount back - * - * @param string $order_id - * @param int $amount + * @param IPayContract $ipay + * @param string|null $intent + * @param null $shop_order_id + * @param array|null $purchase_units + * @param array|null $items * @param string|null $token - * @return \stdClass + * @param string|null $capture_method + * @param string|null $txn_id + * @return \stdClass|void * @throws \GuzzleHttp\Exception\GuzzleException */ - public function refund(string $order_id, int $amount, string $token = null): \stdClass + public function checkout(string $intent = null, $shop_order_id = null, array $purchase_units = null, array $items = null, string $token = null, string $capture_method = null, string $txn_id = null): ?\stdClass { - $url = config('ipay.url') . '/checkout/refund'; + return $this->ipay->checkout( + $intent ?: $this->intent, + $shop_order_id ?: $this->shop_order_id, + $purchase_units ?: $this->purchase_units, + $items ?: $this->items, + $token ?: $this->token, + $capture_method ?: $this->capture_method, + $txn_id ?: $this->txn_id + ); + } - return $this->postRequest($url, [ - 'order_id' => $order_id, - 'amount' => $amount / 100, - ], $token); + /** + * @param IPayContract $ipay + * @return \stdClass|void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function token(IPayContract $ipay): ?\stdClass + { + return $this->ipay->token(); } /** - * Get details of order - * - * @param string $order_id + * @param IPayContract $ipay + * @param string|null $order_id + * @param int|null $amount * @param string|null $token - * @return \stdClass + * @return \stdClass|null * @throws \GuzzleHttp\Exception\GuzzleException */ - public function orderDetails(string $order_id, string $token = null): \stdClass + public function refund(string $order_id = null, int $amount = null, string $token = null): ?\stdClass { - $url = config('ipay.url') . '/checkout/orders/' . $order_id; - - return $this->getRequest($url, $token); + return $this->ipay->refund( + $order_id ?: $this->order_id, + $amount ?: $this->amount, + $token ?: $this->token + ); } /** - * Reusable http get request - * - * @param string $url + * @param IPayContract $ipay + * @param string|null $order_id * @param string|null $token - * @return \stdClass + * @return \stdClass|null * @throws \GuzzleHttp\Exception\GuzzleException */ - protected function getRequest(string $url, string $token = null): \stdClass + public function orderDetails(string $order_id = null, string $token = null): ?\stdClass { - $token = $this->requestToken($token); - $client = new Client(); - try { - $response = $client->get($url, [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $token, - ], - ]); - } catch (ClientException $exception) { - $error = json_decode($exception->getResponse()->getBody()); - - if (!isset($error->error_code)) { - return $error; - } - - abort($error->error_code, isset($error->error_message) ? $error->error_message : ''); - } - - return json_decode($response->getBody()); + return $this->ipay->orderDetails( + $order_id ?: $this->order_id, + $token ?: $this->token + ); } /** - * Get status of order - * - * @param string $order_id + * @param IPayContract $ipay + * @param string|null $order_id * @param string|null $token - * @return \stdClass + * @return \stdClass|null * @throws \GuzzleHttp\Exception\GuzzleException */ - public function orderStatus(string $order_id, string $token = null): \stdClass + public function orderStatus(string $order_id = null, string $token = null): ?\stdClass { - $url = config('ipay.url') . '/checkout/orders/status/' . $order_id; - - return $this->getRequest($url, $token); + return $this->ipay->orderStatus( + $order_id ?: $this->order_id, + $token ?: $this->token + ); } /** - * Get details of payment - * - * @param string $order_id + * @param IPayContract $ipay + * @param string|null $order_id * @param string|null $token - * @return \stdClass + * @return \stdClass|null * @throws \GuzzleHttp\Exception\GuzzleException */ - public function paymentDetails(string $order_id, string $token = null): \stdClass + public function paymentDetails(string $order_id = null, string $token = null): ?\stdClass { - $url = config('ipay.url') . '/checkout/payment/' . $order_id; - - return $this->getRequest($url, $token); + return $this->ipay->paymentDetails( + $order_id ?: $this->order_id, + $token ?: $this->token + ); } /** - * Complete pre authorized orders - * - * @param string $order_id + * @param IPayContract $ipay + * @param string|null $order_id * @param string|null $token - * @return \stdClass + * @return \stdClass|null * @throws \GuzzleHttp\Exception\GuzzleException */ - public function completePreAuth(string $order_id, string $token = null): \stdClass + public function completePreAuth(string $order_id = null, string $token = null): ?\stdClass { - $url = config('ipay.url') . '/checkout/payment/pre-auth/complete/' . $order_id; - - return $this->getRequest($url, $token); + return $this->ipay->completePreAuth( + $order_id ?: $this->order_id, + $token ?: $this->token + ); } } diff --git a/src/config/ipay.php b/src/config/ipay.php index 994251e..9753645 100644 --- a/src/config/ipay.php +++ b/src/config/ipay.php @@ -10,7 +10,7 @@ /** * Payment url from Bank of Georgia */ - 'url' => env('IPAY_URL', 'https://ipay.ge/opay/api/v1'), + 'url' => env('IPAY_URL', 'https://dev.ipay.ge/opay/api/v1'), /** * Callback url where will be redirected after a success/failure payment