← All changes
|
app/Modules/Subscriptions/Http/Controllers/SubscriptionController.php
+79
-0
1.6.1
→
1.6.5
View file →
| @@ -1,16 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Modules\Subscriptions\Http\Controllers; |
| 4 | 4 | |
| 5 | +use FluentCart\App\App; | |
| 5 | 6 | use FluentCart\App\Helpers\Status; |
| 6 | 7 | use FluentCart\App\Http\Controllers\Controller; |
| 7 | 8 | use FluentCart\App\Models\Order; |
| 8 | 9 | use FluentCart\App\Models\Subscription; |
| 10 | +use FluentCart\App\Modules\PaymentMethods\Core\AbstractPaymentGateway; | |
| 9 | 11 | use FluentCart\App\Modules\Subscriptions\Services\EarlyPaymentFeature; |
| 10 | 12 | use FluentCart\App\Services\Reminders\ReminderService; |
| 11 | 13 | use FluentCart\App\Http\Requests\UpdateSubscriptionRequest; |
| 14 | +use FluentCart\App\Http\Requests\UpdateVendorIdsRequest; | |
| 12 | 15 | use FluentCart\Framework\Http\Request\Request; |
| 16 | +use FluentCart\Framework\Support\Arr; | |
| 13 | 17 | use FluentCart\App\Modules\StoreManagedRenewal\Services\RenewalService; |
| 14 | 18 | use FluentCart\App\Modules\Subscriptions\Services\SubscriptionService; |
| 15 | 19 | use FluentCart\App\Modules\Subscriptions\Services\Filter\SubscriptionFilter; |
| 16 | 20 | |
| @@ -286,8 +290,83 @@ | ||
| 286 | 290 | |
| 287 | 291 | return $this->sendSuccess([ |
| 288 | 292 | 'message' => __('Subscription has been updated successfully!', 'fluent-cart'), |
| 289 | 293 | 'subscription' => Subscription::query()->find($subscription->id) |
| 294 | + ]); | |
| 295 | + } | |
| 296 | + | |
| 297 | + public function updateVendorIds(UpdateVendorIdsRequest $request, Order $order, Subscription $subscription) | |
| 298 | + { | |
| 299 | + $this->validateSubscription($subscription); | |
| 300 | + if ($error = $this->validateOrderBinding($order, $subscription)) return $error; | |
| 301 | + | |
| 302 | + $data = $request->all(); | |
| 303 | + $payload = []; | |
| 304 | + | |
| 305 | + // Only the keys actually sent are written — an absent key must not blank | |
| 306 | + // the stored id. | |
| 307 | + foreach (['vendor_subscription_id', 'vendor_customer_id'] as $field) { | |
| 308 | + if (array_key_exists($field, $data)) { | |
| 309 | + $payload[$field] = (string) $data[$field]; | |
| 310 | + } | |
| 311 | + } | |
| 312 | + | |
| 313 | + if (empty($payload)) { | |
| 314 | + return $this->sendError([ | |
| 315 | + 'message' => __('No data provided for update.', 'fluent-cart') | |
| 316 | + ]); | |
| 317 | + } | |
| 318 | + | |
| 319 | + $result = SubscriptionService::updateVendorIds($subscription, $payload); | |
| 320 | + | |
| 321 | + if (is_wp_error($result)) { | |
| 322 | + return $this->sendError([ | |
| 323 | + 'message' => $result->get_error_message() | |
| 324 | + ]); | |
| 325 | + } | |
| 326 | + | |
| 327 | + return $this->sendSuccess([ | |
| 328 | + 'message' => __('Vendor IDs have been updated successfully!', 'fluent-cart'), | |
| 329 | + 'subscription' => Subscription::query()->find($subscription->id) | |
| 330 | + ]); | |
| 331 | + } | |
| 332 | + | |
| 333 | + public function verifyVendorIds(UpdateVendorIdsRequest $request, Order $order, Subscription $subscription) | |
| 334 | + { | |
| 335 | + $this->validateSubscription($subscription); | |
| 336 | + if ($error = $this->validateOrderBinding($order, $subscription)) return $error; | |
| 337 | + | |
| 338 | + if (!$subscription->canEditVendorIds()) { | |
| 339 | + return $this->sendError([ | |
| 340 | + 'message' => __('Vendor IDs can only be edited on an active gateway-billed subscription.', 'fluent-cart') | |
| 341 | + ]); | |
| 342 | + } | |
| 343 | + | |
| 344 | + if (!$subscription->canVerifyVendorIds()) { | |
| 345 | + return $this->sendError([ | |
| 346 | + 'message' => __('This payment method does not support subscription lookup.', 'fluent-cart') | |
| 347 | + ]); | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** @var AbstractPaymentGateway $gateway */ | |
| 351 | + $gateway = App::gateway($subscription->current_payment_method); | |
| 352 | + | |
| 353 | + $data = $request->all(); | |
| 354 | + | |
| 355 | + $result = $gateway->subscriptions->verifyVendorSubscription([ | |
| 356 | + 'vendor_subscription_id' => (string) Arr::get($data, 'vendor_subscription_id', ''), | |
| 357 | + 'vendor_customer_id' => (string) Arr::get($data, 'vendor_customer_id', ''), | |
| 358 | + ], $order->mode); | |
| 359 | + | |
| 360 | + if (is_wp_error($result)) { | |
| 361 | + return $this->sendError([ | |
| 362 | + 'message' => $result->get_error_message() | |
| 363 | + ]); | |
| 364 | + } | |
| 365 | + | |
| 366 | + return $this->sendSuccess([ | |
| 367 | + 'message' => __('Subscription found at the payment gateway.', 'fluent-cart'), | |
| 368 | + 'verification' => $result | |
| 290 | 369 | ]); |
| 291 | 370 | } |
| 292 | 371 | |
| 293 | 372 | public function generateEarlyPaymentLink(Request $request, Order $order, Subscription $subscription) |