Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/Payment/Mollie.php
Show All 12 Lines | class Mollie extends \App\Providers\PaymentProvider | ||||
* Get a link to the customer in the provider's control panel | * Get a link to the customer in the provider's control panel | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* | * | ||||
* @return string|null The string representing <a> tag | * @return string|null The string representing <a> tag | ||||
*/ | */ | ||||
public function customerLink(Wallet $wallet): ?string | public function customerLink(Wallet $wallet): ?string | ||||
{ | { | ||||
$customer_id = self::mollieCustomerId($wallet); | $customer_id = self::mollieCustomerId($wallet, false); | ||||
if (!$customer_id) { | |||||
return null; | |||||
} | |||||
return sprintf( | return sprintf( | ||||
'<a href="https://www.mollie.com/dashboard/customers/%s" target="_blank">%s</a>', | '<a href="https://www.mollie.com/dashboard/customers/%s" target="_blank">%s</a>', | ||||
$customer_id, | $customer_id, | ||||
$customer_id | $customer_id | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Create a new auto-payment mandate for a wallet. | * Create a new auto-payment mandate for a wallet. | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* @param array $payment Payment data: | * @param array $payment Payment data: | ||||
* - amount: Value in cents | * - amount: Value in cents | ||||
* - currency: The operation currency | * - currency: The operation currency | ||||
* - description: Operation desc. | * - description: Operation desc. | ||||
* | * | ||||
* @return array Provider payment data: | * @return array Provider payment data: | ||||
* - id: Operation identifier | * - id: Operation identifier | ||||
* - redirectUrl: the location to redirect to | * - redirectUrl: the location to redirect to | ||||
*/ | */ | ||||
public function createMandate(Wallet $wallet, array $payment): ?array | public function createMandate(Wallet $wallet, array $payment): ?array | ||||
{ | { | ||||
// Register the user in Mollie, if not yet done | // Register the user in Mollie, if not yet done | ||||
$customer_id = self::mollieCustomerId($wallet); | $customer_id = self::mollieCustomerId($wallet, true); | ||||
$request = [ | $request = [ | ||||
'amount' => [ | 'amount' => [ | ||||
'currency' => $payment['currency'], | 'currency' => $payment['currency'], | ||||
'value' => '0.00', | 'value' => '0.00', | ||||
], | ], | ||||
'customerId' => $customer_id, | 'customerId' => $customer_id, | ||||
'sequenceType' => 'first', | 'sequenceType' => 'first', | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | class Mollie extends \App\Providers\PaymentProvider | ||||
*/ | */ | ||||
public function payment(Wallet $wallet, array $payment): ?array | public function payment(Wallet $wallet, array $payment): ?array | ||||
{ | { | ||||
if ($payment['type'] == self::TYPE_RECURRING) { | if ($payment['type'] == self::TYPE_RECURRING) { | ||||
return $this->paymentRecurring($wallet, $payment); | return $this->paymentRecurring($wallet, $payment); | ||||
} | } | ||||
// Register the user in Mollie, if not yet done | // Register the user in Mollie, if not yet done | ||||
$customer_id = self::mollieCustomerId($wallet); | $customer_id = self::mollieCustomerId($wallet, true); | ||||
// Note: Required fields: description, amount/currency, amount/value | // Note: Required fields: description, amount/currency, amount/value | ||||
$request = [ | $request = [ | ||||
'amount' => [ | 'amount' => [ | ||||
'currency' => $payment['currency'], | 'currency' => $payment['currency'], | ||||
// a number with two decimals is required | // a number with two decimals is required | ||||
'value' => sprintf('%.2f', $payment['amount'] / 100), | 'value' => sprintf('%.2f', $payment['amount'] / 100), | ||||
Show All 39 Lines | class Mollie extends \App\Providers\PaymentProvider | ||||
{ | { | ||||
// Check if there's a valid mandate | // Check if there's a valid mandate | ||||
$mandate = self::mollieMandate($wallet); | $mandate = self::mollieMandate($wallet); | ||||
if (empty($mandate) || !$mandate->isValid() || $mandate->isPending()) { | if (empty($mandate) || !$mandate->isValid() || $mandate->isPending()) { | ||||
return null; | return null; | ||||
} | } | ||||
$customer_id = self::mollieCustomerId($wallet); | $customer_id = self::mollieCustomerId($wallet, true); | ||||
// Note: Required fields: description, amount/currency, amount/value | // Note: Required fields: description, amount/currency, amount/value | ||||
$request = [ | $request = [ | ||||
'amount' => [ | 'amount' => [ | ||||
'currency' => $payment['currency'], | 'currency' => $payment['currency'], | ||||
// a number with two decimals is required | // a number with two decimals is required | ||||
'value' => sprintf('%.2f', $payment['amount'] / 100), | 'value' => sprintf('%.2f', $payment['amount'] / 100), | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | public function webhook(): int | ||||
return 200; | return 200; | ||||
} | } | ||||
/** | /** | ||||
* Get Mollie customer identifier for specified wallet. | * Get Mollie customer identifier for specified wallet. | ||||
* Create one if does not exist yet. | * Create one if does not exist yet. | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* @param bool $create Create the customer if does not exist yet | |||||
* | * | ||||
* @return string Mollie customer identifier | * @return ?string Mollie customer identifier | ||||
*/ | */ | ||||
protected static function mollieCustomerId(Wallet $wallet): string | protected static function mollieCustomerId(Wallet $wallet, bool $create = false): ?string | ||||
{ | { | ||||
$customer_id = $wallet->getSetting('mollie_id'); | $customer_id = $wallet->getSetting('mollie_id'); | ||||
// Register the user in Mollie | // Register the user in Mollie | ||||
if (empty($customer_id)) { | if (empty($customer_id) && $create) { | ||||
$customer = mollie()->customers()->create([ | $customer = mollie()->customers()->create([ | ||||
'name' => $wallet->owner->name(), | 'name' => $wallet->owner->name(), | ||||
'email' => $wallet->id . '@private.' . \config('app.domain'), | 'email' => $wallet->id . '@private.' . \config('app.domain'), | ||||
]); | ]); | ||||
$customer_id = $customer->id; | $customer_id = $customer->id; | ||||
$wallet->setSetting('mollie_id', $customer->id); | $wallet->setSetting('mollie_id', $customer->id); | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |