Skip to content

Commit

Permalink
WIP Use latest MP SDK.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Nov 18, 2024
1 parent 89e734a commit f209fa5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/Payment/Gateway/MercadoPago.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function authorize(PaymentInterface $payment, array $context = [])
{
$payment->setStripeToken($context['token']);
$p = $this->mercadopagoManager->authorize($payment);

// FIXME Use setMercadopagoPaymentId
$payment->setCharge($p->id);
}

Expand Down
54 changes: 24 additions & 30 deletions src/Service/MercadopagoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ public function configure()

/**
* @see https://mercadopago.com/developers/en/docs/checkout-bricks/card-payment-brick/payment-submission
* @return MercadoPago\Payment
* @return Payment
*/
public function authorize(PaymentInterface $payment): Payment
{
$order = $payment->getOrder();
$restaurant = $order->getRestaurant();

$options = [];

$applicationFee = 0;
$accessToken = null;
if (null !== $restaurant) {
Expand Down Expand Up @@ -97,16 +95,15 @@ public function authorize(PaymentInterface $payment): Payment
}

/**
* @return MercadoPago\Payment
* @see https://www.mercadopago.com/developers/en/docs/checkout-api/payment-management/capture-authorized-payment
* @return Payment
*/
public function capture(PaymentInterface $payment)
public function capture(PaymentInterface $payment): Payment
{
// FIXME: should be refactored

$order = $payment->getOrder();

$options = [];

$accessToken = null;
if (null !== $order->getRestaurant()) {
$account = $order->getRestaurant()->getMercadopagoAccount();
Expand All @@ -116,48 +113,45 @@ public function capture(PaymentInterface $payment)
}

if (null !== $accessToken) {
MercadoPago\SDK::setAccessToken($accessToken);
MercadoPagoConfig::setAccessToken($accessToken);
} else {
$this->configure();
}

$payment = MercadoPago\Payment::read(["id" => $payment->getCharge()]);

if (!$payment->capture) {
$payment->capture = true;
$client = new PaymentClient();

if (!$payment->update()) {
throw new \Exception((string) $payment->error);
}
}
$requestOptions = new RequestOptions();
$requestOptions->setCustomHeaders([
sprintf('X-Idempotency-Key: %s', Uuid::uuid4()->toString())
]);

return $payment;
return $client->capture($payment->getCharge(), $requestOptions);
}

/**
* @return MercadoPago\Payment
* @return Payment
*/
public function getPayment(PaymentInterface $payment)
public function getPayment(PaymentInterface $payment): Payment
{
if ($this->settingsManager->isMercadopagoLivemode()) {
$this->configure();
} else {
MercadoPago\SDK::setAccessToken($this->settingsManager->get('mercadopago_access_token_for_test'));
}

$order = $payment->getOrder();

$options = [];

$accessToken = null;
if (null !== $order->getRestaurant()) {
$account = $order->getRestaurant()->getMercadopagoAccount();
if ($account) {
// @see MercadoPago\Manager::processOptions()
$options['custom_access_token'] = $account->getAccessToken();
$accessToken = $account->getAccessToken();
}
}

return MercadoPago\Payment::read(["id" => $payment->getMercadopagoPaymentId()], ["custom_access_token" => $options['custom_access_token']]);
if (null !== $accessToken) {
MercadoPagoConfig::setAccessToken($accessToken);
} else {
$this->configure();
}

$client = new PaymentClient();

return $client->get($payment->getMercadopagoPaymentId());
}

/**
Expand Down

0 comments on commit f209fa5

Please sign in to comment.