Skip to content

Commit

Permalink
Add payment description generator to Mollie configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tdwesten committed Mar 13, 2024
1 parent 584497c commit ae9a053
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
11 changes: 11 additions & 0 deletions config/mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
*/
'cancel_url_generator' => \Pixelpillow\LunarApiMollieAdapter\Generators\RedirectOnFailureUrlGenerator::class,

/*
|--------------------------------------------------------------------------
| Payment description generator
|--------------------------------------------------------------------------
|
| This generator is used to generate the payment description. This generator
| is instantiated with the current Lunar Cart.
|
*/
'payment_description_generator' => \Pixelpillow\LunarApiMollieAdapter\Generators\PaymentIntentDescriptionGenerator::class,

/*
|--------------------------------------------------------------------------
| Testing Webhook URL
Expand Down
14 changes: 14 additions & 0 deletions src/Generators/PaymentIntentDescriptionGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Pixelpillow\LunarApiMollieAdapter\Generators;

class PaymentIntentDescriptionGenerator extends BaseUrlGenerator
{
/**
* Generate the webhook URL.
*/
public function generate(): string
{
return 'Payment for order #'.$this->cart->id;
}
}
27 changes: 25 additions & 2 deletions src/Managers/MollieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function createPayment(
Cart $cart,
string $paymentMethod,
?string $issuer = null,
?string $description = null,
?int $amount = null
): Payment {
$amount = $amount ?? $cart->total->value;
Expand All @@ -59,7 +58,7 @@ public function createPayment(
'currency' => $cart->currency->code,
'value' => self::normalizeAmountToString($amount, $currency->decimal_places),
],
'description' => $description,
'description' => self::getPaymentDescription($cart),
'redirectUrl' => self::getRedirectUrl($cart),
'cancelUrl' => self::getCancelUrl($cart),
'webhookUrl' => self::getWebhookUrl(),
Expand Down Expand Up @@ -149,6 +148,30 @@ public static function getRedirectUrl(Cart $cart): string
return $redirectUrlGenerator->generate();
}

/**
* Get the payment description from the config
*
* @param Cart $cart The cart to get the payment description for.
* @return string The payment description
*
* @throws InvalidConfigurationException When the payment description is not set
*/
public static function getPaymentDescription(Cart $cart): string
{
$paymentDescriptionGeneratorClass = Config::get('lunar-api.mollie.payment_description_generator');

if (! $paymentDescriptionGeneratorClass && ! class_exists($paymentDescriptionGeneratorClass)) {
throw new InvalidConfigurationException('Mollie payment description generator not set in config');
}

/**
* @var BaseUrlGenerator $paymentDescriptionGenerator
*/
$paymentDescriptionGenerator = new $paymentDescriptionGeneratorClass($cart);

return $paymentDescriptionGenerator->generate();
}

/**
* Get the cancel URL from the config
*
Expand Down
1 change: 0 additions & 1 deletion src/MolliePaymentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function createIntent(Cart $cart, array $meta = [], ?int $amount = null):
cart: $cart->calculate(),
paymentMethod: $paymentMethodType,
issuer: $paymentMethodIssuer ?? null,
description: "Lunar web payment for cart #{$cart->id}",
amount: $amount,
);
} catch (Throwable $e) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/CreatePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Mollie\Api\Resources\Payment;
use Mollie\Api\Types\PaymentMethod;
use Mollie\Api\Types\PaymentStatus;
use Pixelpillow\LunarApiMollieAdapter\Generators\PaymentIntentDescriptionGenerator;
use Pixelpillow\LunarApiMollieAdapter\Managers\MollieManager;
use Pixelpillow\LunarApiMollieAdapter\Tests\TestCase;

Expand Down Expand Up @@ -367,3 +368,14 @@
expect($amount)->toBeString();
expect($amount)->toBe('20.60');
});

test('can generate a payment intent description', function () {
$cart = Cart::factory()->create();

$generator = new PaymentIntentDescriptionGenerator($cart);

$description = $generator->generate();

expect($description)->toBeString();
expect($description)->toBe('Payment for order #'.$cart->id);
});

0 comments on commit ae9a053

Please sign in to comment.