Created
January 20, 2022 12:43
-
-
Save Prometee/503e87e495fa2fae3a9f3f289426140a to your computer and use it in GitHub Desktop.
Payum Extension to allows GiftCard with SyliusPayumStripePlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\GiftCard\Payum\Extension; | |
use FluxSE\PayumStripe\Request\Api\Resource\CreateCoupon; | |
use FluxSE\PayumStripe\Request\Api\Resource\RetrieveCoupon; | |
use FluxSE\SyliusPayumStripePlugin\Action\ConvertPaymentAction; | |
use Payum\Core\Extension\Context; | |
use Payum\Core\Extension\ExtensionInterface; | |
use Payum\Core\Request\Convert; | |
use Stripe\Exception\ApiErrorException; | |
use Sylius\Component\Core\Model\PaymentInterface; | |
/** | |
* Payum extension to create coupon on Stripe while using the SyliusGiftCardPlugin | |
* This extension need a decoration of the DetailsProvider to add coupon IDs | |
* to the "discounts" new array member | |
*/ | |
final class CheckCouponsExtension implements ExtensionInterface | |
{ | |
public function onPreExecute(Context $context) | |
{ | |
} | |
public function onExecute(Context $context) | |
{ | |
} | |
public function onPostExecute(Context $context) | |
{ | |
if ($context->getException()) { | |
return; | |
} | |
if (false === $context->getAction() instanceof ConvertPaymentAction) { | |
return; | |
} | |
/** @var mixed|Convert $request */ | |
$request = $context->getRequest(); | |
if (false === $request instanceof Convert) { | |
return; | |
} | |
/** @var mixed|PaymentInterface $payment */ | |
$payment = $request->getSource(); | |
if (false === $payment instanceof PaymentInterface) { | |
return; | |
} | |
$order = $payment->getOrder(); | |
if (null === $order) { | |
return; | |
} | |
$gateway = $context->getGateway(); | |
foreach ($order->getGiftCardOrders() as $giftCardOrder) { | |
$giftCard = $giftCardOrder->getGiftCard(); | |
$couponId = sprintf('GIFT_CARD_%s', $giftCard->getCode()); | |
$retrieveCouponRequest = new RetrieveCoupon($couponId); | |
try { | |
$gateway->execute($retrieveCouponRequest); | |
} catch (ApiErrorException $e) { | |
$createCouponRequest = new CreateCoupon([ | |
'id' => $couponId, | |
'amount_off' => $giftCard->getAmount() / 100, | |
'currency' => $order->getCurrencyCode(), | |
'metadata' => [ | |
'SYLIUS_GIFTCARD_ID' => $giftCard->getId(), | |
'SYLIUS_GIFTCARD_CODE' => $giftCard->getCode(), | |
], | |
'name' => sprintf('Gift card #%d', $giftCard->getId()), | |
]); | |
$gateway->execute($createCouponRequest); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment