|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DoubleThreeDigital\SimpleCommerce\Http\Controllers\DigitalProducts; |
| 4 | + |
| 5 | +use DoubleThreeDigital\SimpleCommerce\Facades\Order; |
| 6 | +use DoubleThreeDigital\SimpleCommerce\Products\ProductType; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Routing\Controller; |
| 9 | +use Statamic\Assets\Asset; |
| 10 | +use Statamic\Facades\AssetContainer; |
| 11 | +use ZipArchive; |
| 12 | + |
| 13 | +class DownloadController extends Controller |
| 14 | +{ |
| 15 | + public function __invoke(Request $request) |
| 16 | + { |
| 17 | + $order = Order::find($request->order_id); |
| 18 | + $item = $order->lineItems()->firstWhere('id', $request->item_id); |
| 19 | + |
| 20 | + if (! $item->metadata()->has('license_key') || $item->metadata()->get('license_key') !== $request->get('license_key')) { |
| 21 | + abort(401); |
| 22 | + } |
| 23 | + |
| 24 | + $product = $item->product(); |
| 25 | + |
| 26 | + $zip = new ZipArchive; |
| 27 | + $zip->open(storage_path("{$order->id()}__{$item->id()}__{$product->id()}.zip"), ZipArchive::CREATE | ZipArchive::OVERWRITE); |
| 28 | + |
| 29 | + if ($product->purchasableType() === ProductType::Product) { |
| 30 | + if (! $product->has('downloadable_asset')) { |
| 31 | + throw new \Exception("Product [{$product->id()}] does not have any digital downloadable assets."); |
| 32 | + } |
| 33 | + |
| 34 | + $product->toAugmentedArray()['downloadable_asset']->value()->get() |
| 35 | + ->each(function (Asset $asset) use ($request, $order, $item, $product, &$zip) { |
| 36 | + if ($item->metadata()->has('download_history') && $product->has('download_limit')) { |
| 37 | + if (collect($item->metadata()->get('download_history'))->count() >= $product->get('download_limit')) { |
| 38 | + abort(405, "You've reached the download limit for this product."); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + $order->updateLineItem($item->id(), [ |
| 43 | + 'metadata' => array_merge($item->metadata()->toArray(), [ |
| 44 | + 'download_history' => array_merge([ |
| 45 | + [ |
| 46 | + 'timestamp' => now()->timestamp, |
| 47 | + 'ip_address' => $request->ip(), |
| 48 | + ], |
| 49 | + ], $item->metadata()->get('download_history', [])), |
| 50 | + ]), |
| 51 | + ]); |
| 52 | + |
| 53 | + $zip->addFile($asset->resolvedPath(), "{$product->get('slug')}/{$asset->basename()}"); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + if ($product->purchasableType() === ProductType::Variant) { |
| 58 | + $productVariant = $product->variant($item->variant()['variant']); |
| 59 | + |
| 60 | + if (! $productVariant->has('downloadable_asset')) { |
| 61 | + throw new \Exception("Product [{$product->id()}] does not have any digital downloadable assets."); |
| 62 | + } |
| 63 | + |
| 64 | + $productVariantsField = $product->resource()->blueprint()->field('product_variants'); |
| 65 | + |
| 66 | + $downloadableAssetField = collect($productVariantsField->get('option_fields')) |
| 67 | + ->where('handle', 'downloadable_asset') |
| 68 | + ->first(); |
| 69 | + |
| 70 | + collect($productVariant->get('downloadable_asset')) |
| 71 | + ->map(function ($assetPath) use ($downloadableAssetField) { |
| 72 | + $assetContainer = isset($downloadableAssetField['field']['container']) |
| 73 | + ? AssetContainer::findByHandle($downloadableAssetField['field']['container']) |
| 74 | + : AssetContainer::all()->first(); |
| 75 | + |
| 76 | + return $assetContainer->asset($assetPath); |
| 77 | + }) |
| 78 | + ->each(function (Asset $asset) use ($request, $order, $item, $product, $productVariant, &$zip) { |
| 79 | + if (config('sc-digital-products.download_history')) { |
| 80 | + if ($item->metadata()->has('download_history') && $productVariant->has('download_limit') && $product->get('download_limit') !== null) { |
| 81 | + if (collect($item->metadata()->get('download_history'))->count() >= $productVariant->get('download_limit')) { |
| 82 | + abort(405, "You've reached the download limit for this product."); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + $order->updateLineItem($item->id(), [ |
| 87 | + 'metadata' => array_merge($item->metadata()->toArray(), [ |
| 88 | + 'download_history' => array_merge([ |
| 89 | + [ |
| 90 | + 'timestamp' => now()->timestamp, |
| 91 | + 'ip_address' => $request->ip(), |
| 92 | + ], |
| 93 | + ], $item->metadata()->get('download_history', [])), |
| 94 | + ]), |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + $zip->addFile($asset->resolvedPath(), "{$productVariant->key()}/{$asset->basename()}"); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + $zip->close(); |
| 103 | + |
| 104 | + return response()->download(storage_path("{$order->id()}__{$item->id()}__{$product->id()}.zip"), "{$product->get('slug')}.zip"); |
| 105 | + } |
| 106 | +} |
0 commit comments