File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Payments/PaymentService.php
<?php
namespace App\KommaApp\Payments;
use App\KommaApp\Orders\Models\Order;
use App\KommaApp\Orders\Models\OrderPayment;
use App\KommaApp\Orders\OrderService;
use App\Mail\Events\PaymentReceivedMail;
use App\Mail\PaymentErrorMail;
use Illuminate\Support\Facades\Log;
use Mollie\Laravel\Facades\Mollie;
class PaymentService
{
const PAYMENT_METHOD_IDEAL = 'ideal';
const PAYMENT_METHOD_BANK_TRANSFER = 'banktransfer';
/**
* Create payment for an event and attach it to the belonging order
*
* @param Order $order
* @param string $paymentDescription
* @param string $successRoute
* @param $paymentMethod
* @return string|null
*/
public function createPayment(Order $order, string $paymentDescription, string $successRoute, $paymentMethod): ?string
{
try {
$paymentInformation = [
'amount' => [
'currency' => 'EUR',
'value' => $order->getFormattedPrice(), // You must send the correct number of decimals, thus we enforce the use of strings
],
'description' => $paymentDescription,
'locale' => 'nl_NL',
'billingEmail' => $order->invoice_email,
'webhookUrl' => route('webhooks.mollie'),
'redirectUrl' => $successRoute,
];
if (empty($paymentMethod)) $paymentInformation['method'] = $this::PAYMENT_METHOD_IDEAL;
else $paymentInformation['method'] = $paymentMethod;
$payment = Mollie::api()->payments()->create($paymentInformation);
$order->status = Order::AWAITING_PAYMENT;
$order->save();
$order->payments()->create([
'status' => OrderPayment::OPEN,
'payment_id' => $payment->id,
]);
return $payment->getCheckoutUrl();
} catch (\Exception $exception) {
//Send payment error email
$mail = (new PaymentErrorMail(request(), $exception))->useMailConfigFrom('exception_mail');
\Mail::send($mail);
}
return null;
}
public function updatePaymentStatus($paymentId, $eventRoute)
{
Log::info('PaymentService: Webhook called for:'. $paymentId);
try {
// Get the payment from our database
$orderPayment = OrderPayment::where('payment_id', $paymentId)
->with('order')
->first();
/**
* Prevent interaction when order is already paid or canceled
* Or when the order has already been handed over to WeFact
*
* Note: For example when a payment goes by bank transfer and later it's later chooses to pays through iDeal
* I think it only happens through the choose payment mail, yet shouldn't happen
*/
if(in_array($orderPayment->order->status, [Order::PAID, Order::CANCELED]) || $orderPayment->order->wefact_invoice_send == 1) {
Log::info('PaymentService: Order is already payed, canceled or handed over to WeFact. Therefor the payment status change can be ignored for order id:' . $orderPayment->order->id);
return;
}
// Get the payment from the molli api by payment id
$payment = Mollie::api()->payments()->get($paymentId);
// Convert the mollie payment status string to id
$statusId = OrderPayment::getStatus($payment->status);
/** @var OrderService $orderService */
$orderService = app()->make(OrderService::class);
// If the payment status is different from our payment
if ($statusId !== $orderPayment->status) {
Log::info('PaymentService: Payment status change for:' . $paymentId . ' | from ' . $orderPayment->status . ' to ' . $statusId);
// Save the new status
$orderPayment->status = $statusId;
$orderPayment->save();
$product = $orderPayment->order->products->first()->productable;
// If paid send mail with event confirm
if ($statusId === OrderPayment::PAID) {
Log::info('PaymentService: Send paid mail for order id:' . $orderPayment->order->id);
// Also change the order status to paid
$orderPayment->order->status = Order::PAID;
$orderPayment->order->save();
// Send payment received mail to client (and possible also to the invoice email)
\Mail::send(new PaymentReceivedMail( $orderPayment->order ));
$orderService->sendInvoiceForOrderByWeFact($orderPayment->order, $product->wefact_code, true);
} else {
Log::info('PaymentService: Send invoice so they can pay at later moment through WeFact for order id:' . $orderPayment->order->id);
$orderService->sendInvoiceForOrderByWeFact($orderPayment->order, $product->wefact_code);
// Log::info('PaymentService: Send link to pay at later moment mail for order id:' . $orderPayment->order->id);
// $paymentRoute = url($eventRoute . '/' . $event->translation->slug . '/' . __('site/routes.events.signUp') . '/' . __('site/routes.events.choosePaymentMethod') . '/'.$orderPayment->order->id);
// \Mail::send(new ChoosePaymentMethodMail( $orderPayment->order, $paymentRoute ));
}
}
else {
Log::info("PaymentService: Status didn't change for order id:" . $orderPayment->order->id);
}
} catch (\Exception $exception) {
Log::info('PaymentService: Update failed');
//Send payment error email
$mail = (new PaymentErrorMail(request(), $exception))->useMailConfigFrom('exception_mail');
\Mail::send($mail);
}
}
public function getPaymentStatusByOrder($orderId)
{
$order = Order::find($orderId);
$order = $order->load('products', 'payments');
dd($order);
}
}