Skip to content

Commit

Permalink
Capture/Cancel/Refund methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Nov 30, 2020
1 parent aee85b6 commit 676e48f
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ matrix:
env: LARAVEL_VERSION=~7.0 TESTBENCH_VERSION=~5.0 PHPUNIT_VERSION=~9.0
- php: 7.4
env: LARAVEL_VERSION=~8.0 TESTBENCH_VERSION=~6.0 PHPUNIT_VERSION=~9.0
- php: 8.0
env: LARAVEL_VERSION=~8.0 TESTBENCH_VERSION=~6.0 PHPUNIT_VERSION=~9.0

before_install:
- travis_retry composer self-update
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "orkhanahmadov/yandex-checkout",
"description": "Easy and complete YooKassa (Yandex Checkout) integration for Laravel",
"keywords": [
"orkhanahmadov",
"yandex",
"yandex-checkout",
"yoomoney",
"yookassa",
"laravel-payment"
],
Expand All @@ -19,7 +19,7 @@
}
],
"require": {
"php": "^7.3",
"php": "^7.3|^8.0",
"illuminate/container": "^6.0|^7.0|^8.0",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"illuminate/database": "^6.0|^7.0|^8.0",
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
'checked' => \Orkhanahmadov\YandexCheckout\Events\CheckoutChecked::class,
'succeeded' => \Orkhanahmadov\YandexCheckout\Events\CheckoutSucceeded::class,
'canceled' => \Orkhanahmadov\YandexCheckout\Events\CheckoutCanceled::class,
'refunded' => \Orkhanahmadov\YandexCheckout\Events\CheckoutRefunded::class,
]
];
7 changes: 7 additions & 0 deletions src/Events/CheckoutRefunded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

class CheckoutRefunded extends CheckoutEvent
{
}
18 changes: 16 additions & 2 deletions src/Models/Traits/HandlesYandexCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@ public function yandexCheckouts(): MorphMany
return $this->morphMany(YandexCheckout::class, 'payable');
}

public function createPayment(CreatePaymentRequestInterface $paymentRequest): YandexCheckout
/**
* @param CreatePaymentRequestInterface|array $paymentRequest
* @param string|null $idempotenceKey
* @return YandexCheckout
*/
public function createPayment($paymentRequest, ?string $idempotenceKey = null): YandexCheckout
{
/** @var YandexCheckoutService $yandexCheckout */
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class);

return $yandexCheckout->createPayment($this, $paymentRequest);
return $yandexCheckout->createPayment(
$this,
$paymentRequest,
$idempotenceKey ?? $this->yandexCheckoutIdempotenceKey()
);
}

public function yandexCheckoutIdempotenceKey(): ?string
{
return "{$this->getTable()}-{$this->getKey()}";
}
}
62 changes: 62 additions & 0 deletions src/Models/YandexCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Orkhanahmadov\YandexCheckout\Models;

use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Orkhanahmadov\YandexCheckout\YandexCheckoutService;
use YandexCheckout\Request\Payments\Payment\CreateCaptureRequestInterface;
use YandexCheckout\Request\Refunds\CreateRefundRequestInterface;

/**
* @property int $id
Expand Down Expand Up @@ -39,6 +43,44 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);
}

/**
* @param CreateCaptureRequestInterface|array $captureRequest
* @return YandexCheckout
*/
public function capturePayment($captureRequest): YandexCheckout
{
/** @var YandexCheckoutService $yandexCheckout */
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class);

$yandexCheckout->capturePayment($this, $captureRequest, $this->payable->yandexCheckoutIdempotenceKey());

return $this->refresh();
}

public function cancelPayment(): YandexCheckout
{
/** @var YandexCheckoutService $yandexCheckout */
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class);

$yandexCheckout->cancelPayment($this, $this->payable->yandexCheckoutIdempotenceKey());

return $this->refresh();
}

/**
* @param CreateRefundRequestInterface|array $refundRequest
* @return YandexCheckout
*/
public function refundPayment($refundRequest): YandexCheckout
{
/** @var YandexCheckoutService $yandexCheckout */
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class);

$yandexCheckout->refundPayment($this, $refundRequest, $this->payable->yandexCheckoutIdempotenceKey());

return $this->refresh();
}

public function payable(): MorphTo
{
return $this->morphTo();
Expand All @@ -54,6 +96,26 @@ public function getPaidAttribute(): bool
return Arr::get($this->response, 'paid', false);
}

public function getRefundableAttribute(): bool
{
return Arr::get($this->response, 'refundable', false);
}

public function getDescriptionAttribute(): bool
{
return Arr::get($this->response, 'description');
}

public function getCapturedAtAttribute(): bool
{
return Arr::get($this->response, 'captured_at');
}

public function getExpiresAtAttribute(): bool
{
return Arr::get($this->response, 'expires_at');
}

public function getConfirmationUrlAttribute(): ?string
{
return Arr::get($this->response, 'confirmation.confirmation_url');
Expand Down
99 changes: 83 additions & 16 deletions src/YandexCheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use Illuminate\Database\Eloquent\Model;
use Orkhanahmadov\YandexCheckout\Models\YandexCheckout;
use YandexCheckout\Client;
use YandexCheckout\Request\Payments\AbstractPaymentResponse;
use YandexCheckout\Request\Payments\CreatePaymentRequestInterface;
use YandexCheckout\Request\Payments\Payment\CreateCaptureRequestInterface;
use YandexCheckout\Request\Refunds\CreateRefundRequest;
use YandexCheckout\Request\Refunds\CreateRefundRequestInterface;

class YandexCheckoutService
{
Expand All @@ -24,9 +28,15 @@ public function __construct(Repository $config)
);
}

public function createPayment(Model $model, CreatePaymentRequestInterface $paymentRequest): YandexCheckout
/**
* @param Model $model
* @param CreatePaymentRequestInterface|array $paymentRequest
* @param string|null $idempotenceKey
* @return YandexCheckout
*/
public function createPayment(Model $model, $paymentRequest, ?string $idempotenceKey = null): YandexCheckout
{
$paymentResponse = $this->client->createPayment($paymentRequest);
$paymentResponse = $this->client->createPayment($paymentRequest, $idempotenceKey);

$yandexCheckoutModel = new YandexCheckout();
$yandexCheckoutModel->payable_type = get_class($model);
Expand All @@ -36,35 +46,92 @@ public function createPayment(Model $model, CreatePaymentRequestInterface $payme
$yandexCheckoutModel->response = $paymentResponse->jsonSerialize();
$yandexCheckoutModel->save();

$this->dispatchEvent('created', $yandexCheckoutModel);
$this->dispatchEvent($yandexCheckoutModel, 'created');

return $yandexCheckoutModel;
}

/**
* @param YandexCheckout|string $payment
* @param YandexCheckout $model
* @return YandexCheckout
*/
public function paymentInfo($payment): YandexCheckout
public function paymentInfo(YandexCheckout $model): YandexCheckout
{
if (! $payment instanceof YandexCheckout) {
$payment = YandexCheckout::where('payment_id', $payment)->firstOrFail();
}
$paymentResponse = $this->client->getPaymentInfo($model->payment_id);

$model = $this->updateCheckoutModel($model, $paymentResponse);

$this->dispatchEvent($model, 'checked');
$this->dispatchEvent($model);

return $model;
}

/**
* @param YandexCheckout $model
* @param CreateCaptureRequestInterface|array $captureRequest
* @param string|null $idempotenceKey
* @return YandexCheckout
*/
public function capturePayment(YandexCheckout $model, $captureRequest, ?string $idempotenceKey = null): YandexCheckout
{
$captureResponse = $this->client->capturePayment($captureRequest, $model->payment_id, $idempotenceKey);

$model = $this->updateCheckoutModel($model, $captureResponse);

$this->dispatchEvent($model);

return $model;
}

/**
* @param YandexCheckout $model
* @param string|null $idempotenceKey
* @return YandexCheckout
*/
public function cancelPayment(YandexCheckout $model, ?string $idempotenceKey = null): YandexCheckout
{
$cancelResponse = $this->client->cancelPayment($model->payment_id, $idempotenceKey);

$model = $this->updateCheckoutModel($model, $cancelResponse);

$this->dispatchEvent($model);

return $model;
}

/**
* @param YandexCheckout $model
* @param CreateRefundRequestInterface|array $refundRequest
* @param string|null $idempotenceKey
* @return YandexCheckout
*/
public function refundPayment(YandexCheckout $model, $refundRequest, ?string $idempotenceKey = null)
{
$refundResponse = $this->client->createRefund($refundRequest, $idempotenceKey);

$paymentResponse = $this->client->getPaymentInfo($payment->payment_id);
if ($refundResponse->getStatus() === YandexCheckout::STATUS_SUCCEEDED) {
$this->dispatchEvent($model, 'refunded');

$payment->status = $paymentResponse->getStatus();
$payment->response = $paymentResponse->jsonSerialize();
$payment->save();
$model = $this->paymentInfo($model);
}

$this->dispatchEvent('checked', $payment);
$this->dispatchEvent($payment->status, $payment);
return $model;
}

private function updateCheckoutModel(YandexCheckout $model, AbstractPaymentResponse $yandexResponse): YandexCheckout
{
$model->status = $yandexResponse->getStatus();
$model->response = $yandexResponse->jsonSerialize();
$model->save();

return $payment;
return $model;
}

private function dispatchEvent(string $name, YandexCheckout $yandexCheckout): void
private function dispatchEvent(YandexCheckout $yandexCheckout, ?string $name = null): void
{
$name = $name ?? $yandexCheckout->status;

if ($event = config("yandex-checkout.events.{$name}")) {
$event::dispatch($yandexCheckout);
}
Expand Down

0 comments on commit 676e48f

Please sign in to comment.