Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/Payment/Mollie.php
Show First 20 Lines • Show All 287 Lines • ▼ Show 20 Lines | public function webhook(): int | ||||
$payment = Payment::find($payment_id); | $payment = Payment::find($payment_id); | ||||
if (empty($payment)) { | if (empty($payment)) { | ||||
// Mollie recommends to return "200 OK" even if the payment does not exist | // Mollie recommends to return "200 OK" even if the payment does not exist | ||||
return 200; | return 200; | ||||
} | } | ||||
// Get the payment details from Mollie | // Get the payment details from Mollie | ||||
// TODO: Consider https://github.com/mollie/mollie-api-php/issues/502 when it's fixed | |||||
$mollie_payment = mollie()->payments()->get($payment_id); | $mollie_payment = mollie()->payments()->get($payment_id); | ||||
if (empty($mollie_payment)) { | if (empty($mollie_payment)) { | ||||
// Mollie recommends to return "200 OK" even if the payment does not exist | // Mollie recommends to return "200 OK" even if the payment does not exist | ||||
return 200; | return 200; | ||||
} | } | ||||
$refunds = []; | |||||
if ($mollie_payment->isPaid()) { | if ($mollie_payment->isPaid()) { | ||||
if (!$mollie_payment->hasRefunds() && !$mollie_payment->hasChargebacks()) { | // The payment is paid. Update the balance, and notify the user | ||||
// The payment is paid and isn't refunded or charged back. | |||||
// Update the balance, if it wasn't already | |||||
if ($payment->status != self::STATUS_PAID && $payment->amount > 0) { | if ($payment->status != self::STATUS_PAID && $payment->amount > 0) { | ||||
$credit = true; | $credit = true; | ||||
$notify = $payment->type == self::TYPE_RECURRING; | $notify = $payment->type == self::TYPE_RECURRING; | ||||
} | } | ||||
} elseif ($mollie_payment->hasRefunds()) { | |||||
// The payment has been (partially) refunded. | // The payment has been (partially) refunded. | ||||
// The status of the payment is still "paid" | // Let's process refunds with status "refunded". | ||||
// TODO: Update balance | if ($mollie_payment->hasRefunds()) { | ||||
} elseif ($mollie_payment->hasChargebacks()) { | foreach ($mollie_payment->refunds() as $refund) { | ||||
if ($refund->isTransferred() && $refund->amount->value) { | |||||
$refunds[] = [ | |||||
'id' => $refund->id, | |||||
'description' => $refund->description, | |||||
'amount' => round(floatval($refund->amount->value) * 100), | |||||
'type' => self::TYPE_REFUND, | |||||
// Note: we assume this is the original payment/wallet currency | |||||
]; | |||||
} | |||||
} | |||||
} | |||||
// The payment has been (partially) charged back. | // The payment has been (partially) charged back. | ||||
// The status of the payment is still "paid" | // Let's process chargebacks (they have no states as refunds) | ||||
// TODO: Update balance | if ($mollie_payment->hasChargebacks()) { | ||||
foreach ($mollie_payment->chargebacks() as $chargeback) { | |||||
if ($chargeback->amount->value) { | |||||
$refunds[] = [ | |||||
'id' => $chargeback->id, | |||||
'amount' => round(floatval($chargeback->amount->value) * 100), | |||||
'type' => self::TYPE_CHARGEBACK, | |||||
// Note: we assume this is the original payment/wallet currency | |||||
]; | |||||
} | |||||
} | |||||
} | } | ||||
} elseif ($mollie_payment->isFailed()) { | } elseif ($mollie_payment->isFailed()) { | ||||
// Note: I didn't find a way to get any description of the problem with a payment | // Note: I didn't find a way to get any description of the problem with a payment | ||||
\Log::info(sprintf('Mollie payment failed (%s)', $payment->id)); | \Log::info(sprintf('Mollie payment failed (%s)', $payment->id)); | ||||
// Disable the mandate | // Disable the mandate | ||||
if ($payment->type == self::TYPE_RECURRING) { | if ($payment->type == self::TYPE_RECURRING) { | ||||
$notify = true; | $notify = true; | ||||
Show All 11 Lines | public function webhook(): int | ||||
$payment->status = $mollie_payment->status; | $payment->status = $mollie_payment->status; | ||||
$payment->save(); | $payment->save(); | ||||
} | } | ||||
if (!empty($credit)) { | if (!empty($credit)) { | ||||
self::creditPayment($payment, $mollie_payment); | self::creditPayment($payment, $mollie_payment); | ||||
} | } | ||||
foreach ($refunds as $refund) { | |||||
$this->storeRefund($payment->wallet, $refund); | |||||
} | |||||
DB::commit(); | DB::commit(); | ||||
if (!empty($notify)) { | if (!empty($notify)) { | ||||
\App\Jobs\PaymentEmail::dispatch($payment); | \App\Jobs\PaymentEmail::dispatch($payment); | ||||
} | } | ||||
return 200; | return 200; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |