| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\PaymentGateways\Gateways\PayLater; |
| 6 |
|
| 7 |
use Yatra\PaymentGateways\AbstractPaymentGateway; |
| 8 |
|
| 9 |
class PayLaterGateway extends AbstractPaymentGateway |
| 10 |
{ |
| 11 |
protected string $id = 'pay_later'; |
| 12 |
protected string $title = 'Book Now, Pay Later'; |
| 13 |
protected string $description = 'Reserve now and pay before the trip'; |
| 14 |
protected string $icon = 'pay-later.svg'; |
| 15 |
protected bool $isOffline = true; |
| 16 |
protected array $supports = ['pay_later', 'reservation']; |
| 17 |
|
| 18 |
/** |
| 19 |
* Translatable display title. The raw `$title` property can't carry a |
| 20 |
* `__()` call (PHP property defaults must be constant), so the customer- |
| 21 |
* facing label is translated here. An admin-set custom title (via gateway |
| 22 |
* config) still takes precedence in PaymentGatewayRegistry::getForCheckout(). |
| 23 |
*/ |
| 24 |
public function getTitle(): string |
| 25 |
{ |
| 26 |
return __('Book Now, Pay Later', 'yatra'); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Translatable description shown under the gateway option at checkout. |
| 31 |
*/ |
| 32 |
public function getDescription(): string |
| 33 |
{ |
| 34 |
return __('Reserve now and pay before the trip', 'yatra'); |
| 35 |
} |
| 36 |
|
| 37 |
public function getConfigFields(): array |
| 38 |
{ |
| 39 |
return [ |
| 40 |
[ |
| 41 |
'id' => 'payment_deadline_days', |
| 42 |
'type' => 'number', |
| 43 |
'label' => __('Payment Deadline (Days Before Trip)', 'yatra'), |
| 44 |
'description' => __('Number of days before the trip when full payment is required', 'yatra'), |
| 45 |
'placeholder' => '7', |
| 46 |
'default' => '7', |
| 47 |
'min' => 1, |
| 48 |
'max' => 60, |
| 49 |
], |
| 50 |
[ |
| 51 |
'id' => 'auto_cancel_days', |
| 52 |
'type' => 'number', |
| 53 |
'label' => __('Auto-Cancel After (Days)', 'yatra'), |
| 54 |
'description' => __('Automatically cancel unpaid bookings after this many days (0 to disable)', 'yatra'), |
| 55 |
'placeholder' => '3', |
| 56 |
'default' => '3', |
| 57 |
'min' => 0, |
| 58 |
'max' => 30, |
| 59 |
], |
| 60 |
[ |
| 61 |
'id' => 'require_deposit', |
| 62 |
'type' => 'checkbox', |
| 63 |
'label' => __('Require Small Deposit', 'yatra'), |
| 64 |
'description' => __('Collect a small deposit to confirm the booking', 'yatra'), |
| 65 |
'default' => false, |
| 66 |
], |
| 67 |
[ |
| 68 |
'id' => 'deposit_amount', |
| 69 |
'type' => 'number', |
| 70 |
'label' => __('Deposit Amount (%)', 'yatra'), |
| 71 |
'description' => __('Percentage of total amount required as deposit', 'yatra'), |
| 72 |
'placeholder' => '10', |
| 73 |
'default' => '10', |
| 74 |
'min' => 1, |
| 75 |
'max' => 50, |
| 76 |
'condition' => 'require_deposit', |
| 77 |
], |
| 78 |
[ |
| 79 |
'id' => 'reminder_days', |
| 80 |
'type' => 'text', |
| 81 |
'label' => __('Payment Reminder Days', 'yatra'), |
| 82 |
'description' => __('Send payment reminders X days before deadline (comma-separated, e.g., 7,3,1)', 'yatra'), |
| 83 |
'placeholder' => '7,3,1', |
| 84 |
'default' => '7,3,1', |
| 85 |
], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
public function processPayment(array $paymentData): array |
| 90 |
{ |
| 91 |
$bookingId = $paymentData['booking_id'] ?? 0; |
| 92 |
$tripDate = $paymentData['trip_date'] ?? ''; |
| 93 |
$amount = (float) ($paymentData['amount'] ?? 0); |
| 94 |
|
| 95 |
$deadlineDays = (int) ($this->config['payment_deadline_days'] ?? 7); |
| 96 |
$requireDeposit = !empty($this->config['require_deposit']); |
| 97 |
$depositPercentage = (int) ($this->config['deposit_amount'] ?? 10); |
| 98 |
|
| 99 |
// Calculate payment deadline |
| 100 |
$paymentDeadline = null; |
| 101 |
if (!empty($tripDate)) { |
| 102 |
$tripDateTime = strtotime($tripDate); |
| 103 |
$paymentDeadline = date('Y-m-d', strtotime("-{$deadlineDays} days", $tripDateTime)); |
| 104 |
} |
| 105 |
|
| 106 |
$depositAmount = 0; |
| 107 |
if ($requireDeposit) { |
| 108 |
$depositAmount = ($amount * $depositPercentage) / 100; |
| 109 |
} |
| 110 |
|
| 111 |
return [ |
| 112 |
'success' => true, |
| 113 |
'payment_method' => 'pay_later', |
| 114 |
'status' => $requireDeposit ? 'deposit_required' : 'reserved', |
| 115 |
'booking_id' => $bookingId, |
| 116 |
'total_amount' => $amount, |
| 117 |
'deposit_required' => $requireDeposit, |
| 118 |
'deposit_amount' => $depositAmount, |
| 119 |
'remaining_amount' => $amount - $depositAmount, |
| 120 |
'payment_deadline' => $paymentDeadline, |
| 121 |
'deadline_days' => $deadlineDays, |
| 122 |
'message' => $requireDeposit |
| 123 |
? sprintf( |
| 124 |
/* translators: 1: formatted deposit amount, 2: number of days before the trip. */ |
| 125 |
__('A deposit of %1$s is required to confirm your booking. Full payment is due %2$d days before your trip.', 'yatra'), |
| 126 |
yatra_format_price((float) $depositAmount, null, false), |
| 127 |
$deadlineDays |
| 128 |
) |
| 129 |
: sprintf( |
| 130 |
/* translators: 1: formatted total amount, 2: number of days before the trip. */ |
| 131 |
__('Your booking is reserved! Full payment of %1$s is due %2$d days before your trip.', 'yatra'), |
| 132 |
yatra_format_price((float) $amount, null, false), |
| 133 |
$deadlineDays |
| 134 |
), |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
public function verifyPayment(string $transactionId): array |
| 139 |
{ |
| 140 |
// Pay Later takes no money at checkout — the balance is settled offline |
| 141 |
// later. There is nothing to verify online, so this MUST NOT report |
| 142 |
// success: callers such as confirm_payment() / handle_callback() treat a |
| 143 |
// successful verify as a captured payment and would otherwise mark the |
| 144 |
// booking paid + completed before any money has arrived. |
| 145 |
return [ |
| 146 |
'success' => false, |
| 147 |
'status' => 'reserved', |
| 148 |
'error' => __('Pay Later bookings are settled offline; there is no online payment to verify.', 'yatra'), |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get auto-cancel days setting |
| 154 |
*/ |
| 155 |
public function getAutoCancelDays(): int |
| 156 |
{ |
| 157 |
return (int) ($this->config['auto_cancel_days'] ?? 3); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get reminder days as array |
| 162 |
*/ |
| 163 |
public function getReminderDays(): array |
| 164 |
{ |
| 165 |
$reminderStr = $this->config['reminder_days'] ?? '7,3,1'; |
| 166 |
return array_map('intval', array_filter(explode(',', $reminderStr))); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|