Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Nov 19, 2024
1 parent 1cca253 commit 0e05289
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
25 changes: 19 additions & 6 deletions src/Payment/Gateway/Paygreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,28 @@ public function __construct(private PaygreenManager $paygreenManager)

public function authorize(PaymentInterface $payment, array $context = [])
{
// With Paygreen, the payment has already been authorized
// With Paygreen, the payment has already been authorized client-side
// We double-check the status of the payment
if (!$this->paygreenManager->isPaymentOrderAuthorized($context['token'])) {
throw new \Exception('Invalid Payment Order');
if (!$po = $this->paygreenManager->getPaymentOrder($context['token'])) {
throw new \Exception(sprintf('Payment Order "%s" not found', $context['token']));
}

// TODO Retrieve PaymentOrder, and update payments accordingly
// We need to check inside transactions.operations.instrument
// to find the amount & platform of each operation
if ($po['status'] !== 'payment_order.authorized') {
throw new \Exception(sprintf('Payment Order "%s" is not authorized', $context['token']));
}

// There may have been multiple Paygreen operations
// We convert them to Payment objects
$payments = $this->paygreenManager->getPaymentsFromPaymentOrder($context['token']);

$order = $payment->getOrder();
foreach ($order->getPayments() as $p) {
$order->removePayment($p);
}

foreach ($payments as $p) {
$order->addPayment($p);
}
}

public function capture(PaymentInterface $payment)
Expand Down
69 changes: 53 additions & 16 deletions src/Service/PaygreenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
use Paygreen\Sdk\Payment\V3\Client as PaygreenClient;
use Paygreen\Sdk\Payment\V3\Model as PaygreenModel;
use Sylius\Bundle\OrderBundle\NumberAssigner\OrderNumberAssignerInterface;
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;

class PaygreenManager
{
public function __construct(
private PaygreenClient $paygreenClient,
private OrderNumberAssignerInterface $orderNumberAssigner,
private Hashids $hashids8,
private PaymentMethodRepositoryInterface $paymentMethodRepository,
private PaymentFactoryInterface $paymentFactory,
private string $country)
{}

Expand All @@ -27,20 +31,6 @@ private function authenticate()
$this->paygreenClient->setBearer($data->token);
}

public function isPaymentOrderAuthorized(string $paymentOrderId): bool
{
$this->authenticate();

$response = $this->paygreenClient->getPaymentOrder($paymentOrderId);
if ($response->getStatusCode() === 200) {
$data = json_decode($response->getBody()->getContents(), true);

return $data['data']['status'] === 'payment_order.authorized';
}

return false;
}

public function capture(PaymentInterface $payment)
{
$this->authenticate();
Expand Down Expand Up @@ -177,7 +167,7 @@ public function getBuyerForOrder(OrderInterface $order): PaygreenModel\Buyer
$buyer->setEmail($customer->getEmailCanonical());
$buyer->setFirstName(!empty($firstName) ? $firstName : 'N/A');
$buyer->setLastName(!empty($lastName) ? $lastName : 'N/A');
$buyer->setBillingAddress($address);
// $buyer->setBillingAddress($address);

$response = $this->paygreenClient->createBuyer($buyer);
$data = json_decode($response->getBody()->getContents(), true);
Expand All @@ -191,12 +181,59 @@ public function getBuyerForOrder(OrderInterface $order): PaygreenModel\Buyer
return $buyer;
}

/**
* @return array|null
*/
public function getPaymentOrder($id)
{
$this->authenticate();

$response = $this->paygreenClient->getPaymentOrder($id);

return json_decode($response->getBody()->getContents(), true);
if ($response->getStatusCode() === 200) {
$payload = json_decode($response->getBody()->getContents(), true);

return $payload['data'];
}

return null;
}

public function getPaymentsFromPaymentOrder($id)
{
$po = $this->getPaymentOrder($id);

$payments = [];
foreach ($po['transactions'] as $transaction) {
if ('transaction.authorized' === $transaction['status']) {
foreach ($transaction['operations'] as $operation) {
if ('operation.authorized' === $operation['status']) {

$paymentMethodCode = $operation['instrument']['platform'];
if ('bank_card' === $paymentMethodCode) {
$paymentMethodCode = 'card';
}

$method = $this->paymentMethodRepository->findOneByCode(strtoupper($paymentMethodCode));

$payment = $this->paymentFactory->createWithAmountAndCurrencyCode(
$operation['amount'],
strtoupper($po['currency'])
);
$payment->setMethod($method);
$payment->setState(PaymentInterface::STATE_AUTHORIZED);

$payment->setDetails([
'paygreen_payment_order_id' => $id,
'paygreen_operation_id' => $operation['id'],
]);

$payments[] = $payment;
}
}
}
}

return $payments;
}
}

0 comments on commit 0e05289

Please sign in to comment.