Shop, menu, and booking storefronts with OIDC SSO, Pay checkout, and merchant.ladill.com deployment workflow. Co-authored-by: Cursor <cursoragent@cursor.com>
202 lines
7.2 KiB
PHP
202 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Merchant;
|
|
|
|
use App\Models\QrSaleOrder;
|
|
use App\Models\QrCode;
|
|
use App\Services\Billing\BillingClient;
|
|
use App\Services\Billing\PaystackService;
|
|
use App\Services\Billing\SmsService;
|
|
use App\Services\Pay\PayClient;
|
|
use Illuminate\Support\Str;
|
|
use RuntimeException;
|
|
|
|
class MerchantSaleService
|
|
{
|
|
public function __construct(
|
|
private PayClient $pay,
|
|
private PaystackService $paystack,
|
|
private BillingClient $billing,
|
|
private SmsService $sms,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{customer_name: string, customer_email: string, customer_phone: string, items: list<array{name: string, price: float, qty: int}>} $data
|
|
* @return array{order: QrSaleOrder, checkout_url: string, callback_url: string}
|
|
*/
|
|
public function initiateOrder(QrCode $qrCode, array $data): array
|
|
{
|
|
if ($qrCode->type !== QrCode::TYPE_SHOP) {
|
|
throw new RuntimeException('This QR is not a storefront.');
|
|
}
|
|
|
|
$items = $data['items'] ?? [];
|
|
if (empty($items)) {
|
|
throw new RuntimeException('Enter an amount to give.');
|
|
}
|
|
|
|
$item = $items[0];
|
|
$amountGhs = round((float) ($item['price'] ?? 0), 2);
|
|
if ($amountGhs <= 0) {
|
|
throw new RuntimeException('Enter an amount greater than zero.');
|
|
}
|
|
|
|
$collectionType = trim((string) ($item['name'] ?? 'Order'));
|
|
$qrCode->loadMissing('user');
|
|
$amountMinor = (int) round($amountGhs * 100);
|
|
$reference = 'MCHT-'.strtoupper(Str::random(16));
|
|
$orgName = $qrCode->content()['name'] ?? $qrCode->label ?? 'Storefront';
|
|
$callbackUrl = route('qr.public.order.callback', ['shortCode' => $qrCode->short_code]);
|
|
|
|
$order = QrSaleOrder::create([
|
|
'qr_code_id' => $qrCode->id,
|
|
'user_id' => $qrCode->user_id,
|
|
'reference' => $reference,
|
|
'collection_type' => $collectionType,
|
|
'amount_minor' => $amountMinor,
|
|
'currency' => $qrCode->content()['currency'] ?? 'GHS',
|
|
'payer_name' => trim((string) ($data['customer_name'] ?? '')),
|
|
'payer_email' => trim((string) ($data['customer_email'] ?? '')),
|
|
'payer_phone' => trim((string) ($data['customer_phone'] ?? '')),
|
|
'status' => QrSaleOrder::STATUS_PENDING,
|
|
'payment_reference' => null,
|
|
]);
|
|
|
|
$payOrder = $this->pay->createCheckout([
|
|
'merchant' => $qrCode->user->public_id,
|
|
'fee_tier' => 'orders',
|
|
'source_service' => 'merchant',
|
|
'source_ref' => (string) $qrCode->id,
|
|
'callback_url' => $callbackUrl,
|
|
'customer_name' => $order->payer_name,
|
|
'customer_email' => $order->payer_email,
|
|
'customer_phone' => $order->payer_phone,
|
|
'line_items' => [
|
|
[
|
|
'name' => $collectionType.' — '.$orgName,
|
|
'unit_price_minor' => $amountMinor,
|
|
'quantity' => 1,
|
|
],
|
|
],
|
|
'metadata' => [
|
|
'give_order_id' => $order->id,
|
|
'give_reference' => $reference,
|
|
'qr_code_id' => $qrCode->id,
|
|
'collection_type' => $collectionType,
|
|
],
|
|
]);
|
|
|
|
$order->update([
|
|
'pay_order_id' => $payOrder['id'] ?? null,
|
|
'payment_reference' => $payOrder['reference'],
|
|
'platform_fee_minor' => $payOrder['platform_fee_minor'] ?? null,
|
|
'merchant_amount_minor' => $payOrder['merchant_amount_minor'] ?? null,
|
|
]);
|
|
|
|
$checkoutUrl = (string) ($payOrder['checkout_url'] ?? '');
|
|
if ($checkoutUrl === '') {
|
|
throw new RuntimeException('Could not start checkout. Please try again.');
|
|
}
|
|
|
|
return [
|
|
'order' => $order->fresh(),
|
|
'checkout_url' => $checkoutUrl,
|
|
'callback_url' => $callbackUrl,
|
|
];
|
|
}
|
|
|
|
public function complete(string $paymentReference): QrSaleOrder
|
|
{
|
|
if (str_starts_with($paymentReference, 'LP-')) {
|
|
return $this->completeLadillPay($paymentReference);
|
|
}
|
|
|
|
return $this->completeLegacy($paymentReference);
|
|
}
|
|
|
|
private function completeLadillPay(string $reference): QrSaleOrder
|
|
{
|
|
$order = QrSaleOrder::where('payment_reference', $reference)
|
|
->where('status', QrSaleOrder::STATUS_PENDING)
|
|
->firstOrFail();
|
|
|
|
$payOrder = $this->pay->verify($reference);
|
|
|
|
$order->update([
|
|
'status' => QrSaleOrder::STATUS_PAID,
|
|
'amount_minor' => (int) ($payOrder['amount_minor'] ?? $order->amount_minor),
|
|
'platform_fee_minor' => (int) ($payOrder['platform_fee_minor'] ?? 0),
|
|
'merchant_amount_minor' => (int) ($payOrder['merchant_amount_minor'] ?? 0),
|
|
'pay_order_id' => $payOrder['id'] ?? $order->pay_order_id,
|
|
'paid_at' => now(),
|
|
'metadata' => array_merge((array) $order->metadata, ['ladill_pay' => $payOrder]),
|
|
]);
|
|
|
|
$this->notifyDonor($order->fresh(['qrCode', 'merchant']));
|
|
|
|
return $order;
|
|
}
|
|
|
|
private function completeLegacy(string $paymentReference): QrSaleOrder
|
|
{
|
|
$order = QrSaleOrder::where('payment_reference', $paymentReference)
|
|
->where('status', QrSaleOrder::STATUS_PENDING)
|
|
->firstOrFail();
|
|
|
|
$data = $this->paystack->verifyTransaction($paymentReference);
|
|
|
|
if (($data['status'] ?? '') !== 'success') {
|
|
$order->update(['status' => QrSaleOrder::STATUS_FAILED]);
|
|
throw new RuntimeException('Payment was not successful.');
|
|
}
|
|
|
|
$paidMinor = (int) ($data['amount'] ?? $order->amount_minor);
|
|
$platformFeeMinor = (int) round($paidMinor * QrSaleOrder::PLATFORM_FEE_RATE);
|
|
$merchantMinor = $paidMinor - $platformFeeMinor;
|
|
|
|
$order->update([
|
|
'status' => QrSaleOrder::STATUS_PAID,
|
|
'amount_minor' => $paidMinor,
|
|
'platform_fee_minor' => $platformFeeMinor,
|
|
'merchant_amount_minor' => $merchantMinor,
|
|
'paid_at' => now(),
|
|
'metadata' => array_merge((array) $order->metadata, ['paystack' => $data, 'legacy' => true]),
|
|
]);
|
|
|
|
$orgName = $order->qrCode?->content()['name'] ?? $order->qrCode?->label ?? 'Storefront';
|
|
$this->billing->credit(
|
|
$order->merchant->public_id,
|
|
$merchantMinor,
|
|
'give',
|
|
'pay',
|
|
$paymentReference,
|
|
$order->id,
|
|
sprintf('Order via %s', $orgName),
|
|
);
|
|
|
|
$this->notifyDonor($order->fresh(['qrCode', 'merchant']));
|
|
|
|
return $order;
|
|
}
|
|
|
|
private function notifyDonor(QrSaleOrder $order): void
|
|
{
|
|
if (! $order->payer_phone) {
|
|
return;
|
|
}
|
|
|
|
$orgName = $order->qrCode?->content()['name'] ?? $order->qrCode?->label ?? 'Storefront';
|
|
|
|
$this->sms->send(
|
|
$order->payer_phone,
|
|
sprintf(
|
|
'Thank you! Your %s %s order to %s is confirmed. Ref: %s',
|
|
$order->currency,
|
|
number_format($order->amount_minor / 100, 2),
|
|
$orgName,
|
|
$order->reference
|
|
)
|
|
);
|
|
}
|
|
}
|