| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\Cod; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\OrderResource; |
| 6 |
use FluentCart\App\Events\Order\OrderStatusUpdated; |
| 7 |
use FluentCart\App\Services\DateTime\DateTime; |
| 8 |
use FluentCart\App\Helpers\Status; |
| 9 |
use FluentCart\App\Models\Subscription; |
| 10 |
use FluentCart\App\Modules\PaymentMethods\Core\AbstractPaymentGateway; |
| 11 |
use FluentCart\App\Modules\PaymentMethods\Core\BaseGatewaySettings; |
| 12 |
use FluentCart\App\Services\Payments\PaymentInstance; |
| 13 |
use FluentCart\App\Vite; |
| 14 |
use FluentCart\Framework\Support\Arr; |
| 15 |
|
| 16 |
|
| 17 |
class Cod extends AbstractPaymentGateway |
| 18 |
{ |
| 19 |
public array $supportedFeatures = ['payment', 'refund', 'offline']; |
| 20 |
|
| 21 |
public BaseGatewaySettings $settings; |
| 22 |
|
| 23 |
public function boot() |
| 24 |
{ |
| 25 |
add_action('fluent_cart/payment_paid', [$this, 'handlePaymentPaid'], 10, 1); |
| 26 |
add_filter('fluent_cart/payment_method_list_class', [$this, 'getPaymentMethodClass'], 10, 2); |
| 27 |
} |
| 28 |
|
| 29 |
public function getPaymentMethodClass($class, $data): string |
| 30 |
{ |
| 31 |
$route = Arr::get($data, 'route'); |
| 32 |
return $route === 'offline_payment'?'no-padding' : ''; |
| 33 |
} |
| 34 |
|
| 35 |
public function meta(): array |
| 36 |
{ |
| 37 |
return [ |
| 38 |
'title' => __('Cash', 'fluent-cart'), |
| 39 |
'route' => 'offline_payment', |
| 40 |
'slug' => 'offline_payment', |
| 41 |
'description' => esc_html__('Pay with cash upon delivery', 'fluent-cart'), |
| 42 |
'logo' => Vite::getAssetUrl("images/payment-methods/offline-payment.svg"), |
| 43 |
'logo_light' => Vite::getAssetUrl("images/payment-methods/offline-payment-light.svg"), |
| 44 |
'icon' => Vite::getAssetUrl("images/payment-methods/cod-icon.svg"), |
| 45 |
'brand_color' => '#136196', |
| 46 |
'upcoming' => false, |
| 47 |
'status' => $this->settings->get('is_active') === 'yes', |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function __construct() |
| 52 |
{ |
| 53 |
parent::__construct(new CodSettingsBase()); |
| 54 |
} |
| 55 |
|
| 56 |
public function makePaymentFromPaymentInstance(PaymentInstance $paymentInstance) |
| 57 |
{ |
| 58 |
$this->maybeConvertToManualSubscription($paymentInstance); |
| 59 |
|
| 60 |
try { |
| 61 |
return [ |
| 62 |
'status' => 'success', |
| 63 |
'message' => __('Order has been placed successfully', 'fluent-cart'), |
| 64 |
'redirect_to' => (new CodHandler($this))->handlePayment($paymentInstance) |
| 65 |
]; |
| 66 |
|
| 67 |
} catch (\Exception $e) { |
| 68 |
return [ |
| 69 |
'status' => 'failed', |
| 70 |
'message' => $e->getMessage(), |
| 71 |
]; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public function handlePaymentPaid($params) |
| 77 |
{ |
| 78 |
$orderId = Arr::get($params, 'order.id'); |
| 79 |
// get the order with the id |
| 80 |
$order = OrderResource::getQuery()->find($orderId); |
| 81 |
|
| 82 |
if (!$order) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$orderStatus = Arr::get($params, 'order.status'); |
| 87 |
$fulfillmentType = Arr::get($params, 'order.fulfillment_type'); |
| 88 |
$paymentMethod = Arr::get($params, 'order.payment_method'); |
| 89 |
|
| 90 |
//if new status is processing and payment_method is offline_payment and fulfillment_type is digital then make it completed |
| 91 |
if ($orderStatus === Status::ORDER_PROCESSING && $paymentMethod === 'offline_payment' && $fulfillmentType === 'digital') { |
| 92 |
$order->status = Status::ORDER_COMPLETED; |
| 93 |
$order->completed_at = DateTime::gmtNow(); |
| 94 |
$order->save(); |
| 95 |
|
| 96 |
$actionActivity = [ |
| 97 |
'title' => __('Order status updated', 'fluent-cart'), |
| 98 |
'content' => sprintf( |
| 99 |
/* translators: 1: old status, 2: new status */ |
| 100 |
__('Order status has been updated from %1$s to %2$s', 'fluent-cart'), |
| 101 |
$orderStatus, |
| 102 |
$order->status |
| 103 |
) |
| 104 |
]; |
| 105 |
|
| 106 |
(new OrderStatusUpdated($order, $orderStatus, Status::ORDER_COMPLETED, (bool) true, $actionActivity, 'order_status'))->dispatch(); |
| 107 |
|
| 108 |
$this->maybeUpdateSubscription($params); |
| 109 |
|
| 110 |
} else if ($orderStatus === Status::ORDER_PROCESSING && $paymentMethod === 'offline_payment' && $fulfillmentType === 'physical') { |
| 111 |
$this->maybeUpdateSubscription($params); |
| 112 |
} |
| 113 |
|
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
public function maybeUpdateSubscription($params) |
| 118 |
{ |
| 119 |
$order = Arr::get($params, 'order'); |
| 120 |
$orderId = Arr::get($params, 'order.id'); |
| 121 |
$orderStatus = Arr::get($params, 'order.status'); |
| 122 |
|
| 123 |
if (!$order) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if (!in_array($orderStatus, [Status::ORDER_PROCESSING, Status::ORDER_COMPLETED])) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// just search in subscription table with order id get every subscription |
| 132 |
$subscriptions = Subscription::query()->where('parent_order_id', $orderId)->get(); |
| 133 |
|
| 134 |
// update subscription status |
| 135 |
foreach ($subscriptions as $subscription) { |
| 136 |
if ($subscription->status === 'active') { |
| 137 |
continue; |
| 138 |
} |
| 139 |
$subscription->status = 'active'; |
| 140 |
$subscription->save(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function getEnqueueScriptSrc($hasSubscription = 'no'): array |
| 145 |
{ |
| 146 |
return [ |
| 147 |
[ |
| 148 |
'handle' => 'fluent-cart-checkout-handler-cod', |
| 149 |
'src' => Vite::getEnqueuePath('public/payment-methods/cod-checkout.js'), |
| 150 |
] |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
public function getLocalizeData(): array |
| 155 |
{ |
| 156 |
return [ |
| 157 |
'fct_cod_data' => [ |
| 158 |
'translations' => [ |
| 159 |
'Cash upon delivery, bank transfer or other manual process.' => __('Cash upon delivery, bank transfer or other manual process.', 'fluent-cart'), |
| 160 |
] |
| 161 |
] |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
public function fields() |
| 166 |
{ |
| 167 |
return array( |
| 168 |
'cod_description' => array( |
| 169 |
'value' => |
| 170 |
wp_kses(sprintf( |
| 171 |
"<div class='pt-4'> |
| 172 |
<p>%s</p> |
| 173 |
</div>", |
| 174 |
__('� |
| 175 |
Customers can pay for their orders by cash upon delivery. You may receive using bank transfer or other manual process as well. |
| 176 |
You may remind them to have the exact amount ready for our delivery personnel!', 'fluent-cart') |
| 177 |
), |
| 178 |
[ |
| 179 |
'p' => [], |
| 180 |
'div' => ['class' => true], |
| 181 |
'i' => [], |
| 182 |
]), |
| 183 |
'label' => __('Webhook URL', 'fluent-cart'), |
| 184 |
'type' => 'html_attr' |
| 185 |
), |
| 186 |
); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
public static function validateSettings($data): array |
| 191 |
{ |
| 192 |
return [ |
| 193 |
'status' => 'success', |
| 194 |
'message' => __('Settings saved successfully', 'fluent-cart') |
| 195 |
]; |
| 196 |
} |
| 197 |
|
| 198 |
public function maybeUpdatePayments($orderHash) |
| 199 |
{ |
| 200 |
$updateData = [ |
| 201 |
'status' => Status::PAYMENT_PENDING |
| 202 |
]; |
| 203 |
$order = OrderResource::getOrderByHash($orderHash); |
| 204 |
$this->updateOrderDataByOrder($order, $updateData, null); |
| 205 |
} |
| 206 |
|
| 207 |
public function refund($refundInfo, $orderData, $transaction) |
| 208 |
{ |
| 209 |
// cod does not have any refund process |
| 210 |
} |
| 211 |
|
| 212 |
public function webHookPaymentMethodName() |
| 213 |
{ |
| 214 |
return $this->getMeta('route'); |
| 215 |
} |
| 216 |
|
| 217 |
public function handleIPN() |
| 218 |
{ |
| 219 |
// cod does not have any ipn |
| 220 |
} |
| 221 |
|
| 222 |
public function getOrderInfo(array $data) |
| 223 |
{ |
| 224 |
// TODO: Implement getOrderInfo() method. |
| 225 |
} |
| 226 |
} |
| 227 |
|