|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | use Buy\Paddle;
|
| 4 | +use Buy\Passthrough; |
4 | 5 | use Buy\Product;
|
5 | 6 | use Kirby\Cms\Page;
|
6 | 7 |
|
|
49 | 50 | [
|
50 | 51 | 'pattern' => 'buy/prices',
|
51 | 52 | 'action' => function () {
|
52 |
| - // uncomment to test a specific country |
53 |
| - // Buy\Paddle::visitor(country: 'US'); |
54 |
| - |
55 | 53 | $basic = Product::Basic;
|
56 | 54 | $enterprise = Product::Enterprise;
|
57 |
| - $visitor = Paddle::visitor(); |
| 55 | + $visitor = Paddle::visitor(country: get('country')); |
58 | 56 |
|
59 | 57 | return json_encode([
|
60 |
| - 'basic-regular' => $basic->price()->regular(), |
61 |
| - 'basic-sale' => $basic->price()->sale(), |
62 |
| - 'enterprise-regular' => $enterprise->price()->regular(), |
63 |
| - 'enterprise-sale' => $enterprise->price()->sale(), |
64 |
| - 'currency-sign' => $visitor->currencySign(), |
65 |
| - 'currency-sign-trimmed' => rtrim($visitor->currencySign(), ' '), |
66 |
| - 'revenue-limit' => $visitor->currency() !== 'EUR' ? ' (' . $visitor->revenueLimit(1000000) . ')' : '', |
67 |
| - 'status' => $visitor->error() ?? 'OK' |
| 58 | + 'status' => $visitor->error() ?? 'OK', |
| 59 | + 'country' => $visitor->country(), |
| 60 | + 'currency' => $visitor->currencySign(), |
| 61 | + 'prices' => [ |
| 62 | + 'basic' => [ |
| 63 | + 'regular' => $basic->price()->regular(), |
| 64 | + 'sale' => $basic->price()->sale() |
| 65 | + ], |
| 66 | + 'donation' => [ |
| 67 | + 'customer' => $basic->price()->customerDonation(), |
| 68 | + 'team' => $basic->price()->teamDonation(), |
| 69 | + ], |
| 70 | + 'enterprise' => [ |
| 71 | + 'regular' => $enterprise->price()->regular(), |
| 72 | + 'sale' => $enterprise->price()->sale() |
| 73 | + ], |
| 74 | + ], |
| 75 | + 'revenueLimit' => $visitor->currency() !== 'EUR' ? ' (' . $visitor->revenueLimit() . ')' : '', |
| 76 | + 'vatRate' => $visitor->vatRate() ?? 0, |
68 | 77 | ], JSON_UNESCAPED_UNICODE);
|
69 | 78 | }
|
70 | 79 | ],
|
| 80 | + [ |
| 81 | + 'pattern' => 'buy', |
| 82 | + 'method' => 'POST', |
| 83 | + 'action' => function () { |
| 84 | + $city = get('city'); |
| 85 | + $company = get('company'); |
| 86 | + $country = get('country'); |
| 87 | + $donate = get('donate') === 'on'; |
| 88 | + $email = get('email'); |
| 89 | + $newsletter = get('newsletter') === 'on'; |
| 90 | + $productId = get('product'); |
| 91 | + $postalCode = get('postalCode'); |
| 92 | + $state = get('state'); |
| 93 | + $street = get('street'); |
| 94 | + $quantity = Product::restrictQuantity(get('quantity', 1)); |
| 95 | + $vatId = get('vatId'); |
| 96 | + |
| 97 | + try { |
| 98 | + // use the provided country for the calculation, not the IP address |
| 99 | + Paddle::visitor(country: $country); |
| 100 | + |
| 101 | + $product = Product::from($productId); |
| 102 | + $price = $product->price(); |
| 103 | + $message = $product->revenueLimit(); |
| 104 | + $passthrough = new Passthrough(teamDonation: option('buy.donation.teamAmount') * $quantity); |
| 105 | + |
| 106 | + $eurPrice = $product->price('EUR')->volume($quantity); |
| 107 | + $localizedPrice = $price->volume($quantity); |
| 108 | + |
| 109 | + if ($donate === true) { |
| 110 | + // prices per license |
| 111 | + $customerDonation = option('buy.donation.customerAmount'); |
| 112 | + $eurPrice += $customerDonation; |
| 113 | + $localizedPrice += $price->convert($customerDonation); |
| 114 | + |
| 115 | + // donation overall |
| 116 | + $customerDonation *= $quantity; |
| 117 | + $passthrough->customerDonation = $customerDonation; |
| 118 | + |
| 119 | + $message .= ' We will donate an additional €' . $customerDonation . ' to ' . option('buy.donation.charity') . '. Thank you for your donation!'; |
| 120 | + } |
| 121 | + |
| 122 | + $prices = [ |
| 123 | + 'EUR:' . $eurPrice, |
| 124 | + $price->currency . ':' . $localizedPrice, |
| 125 | + ]; |
| 126 | + |
| 127 | + go($product->checkout('buy', [ |
| 128 | + 'custom_message' => $message, |
| 129 | + 'customer_country' => $country, |
| 130 | + 'customer_email' => $email, |
| 131 | + 'customer_postcode' => $postalCode, |
| 132 | + 'marketing_consent' => $newsletter ? 1 : 0, |
| 133 | + 'passthrough' => $passthrough, |
| 134 | + 'prices' => $prices, |
| 135 | + 'quantity' => $quantity, |
| 136 | + 'vat_city' => $city, |
| 137 | + 'vat_country' => $country, |
| 138 | + 'vat_company_name' => $company, |
| 139 | + 'vat_number' => $vatId, |
| 140 | + 'vat_postcode' => $postalCode, |
| 141 | + 'vat_state' => $state, |
| 142 | + 'vat_street' => $street, |
| 143 | + ])); |
| 144 | + } catch (Throwable $e) { |
| 145 | + die( $e-> getMessage() . '<br>Please contact us: [email protected]'); |
| 146 | + } |
| 147 | + }, |
| 148 | + ], |
71 | 149 | [
|
72 | 150 | 'pattern' => 'buy/(enterprise|basic)',
|
73 |
| - 'action' => function (string $product) { |
| 151 | + 'action' => function (string $productId) { |
74 | 152 | try {
|
75 |
| - $product = Product::from($product); |
76 |
| - $price = $product->price(); |
| 153 | + $product = Product::from($productId); |
| 154 | + $price = $product->price(); |
| 155 | + $passthrough = new Passthrough(teamDonation: option('buy.donation.teamAmount')); |
| 156 | + |
| 157 | + $eurPrice = $product->price('EUR')->sale(); |
| 158 | + $localizedPrice = $price->sale(); |
| 159 | + |
77 | 160 | $prices = [
|
78 |
| - 'EUR:' . $product->price('EUR')->sale(), |
79 |
| - $price->currency . ':' . $price->sale(), |
| 161 | + 'EUR:' . $eurPrice, |
| 162 | + $price->currency . ':' . $localizedPrice, |
80 | 163 | ];
|
81 | 164 |
|
82 |
| - go($product->checkout('buy', compact('prices'))); |
| 165 | + go($product->checkout('buy', compact('prices', 'passthrough'))); |
83 | 166 | } catch (Throwable $e) {
|
84 | 167 | die( $e-> getMessage() . '<br>Please contact us: [email protected]');
|
85 | 168 | }
|
|
89 | 172 | 'pattern' => 'buy/volume',
|
90 | 173 | 'method' => 'POST',
|
91 | 174 | 'action' => function () {
|
92 |
| - $product = get('product', 'basic'); |
93 |
| - $quantity = get('volume', 5); |
| 175 | + $productId = get('product', 'basic'); |
| 176 | + $quantity = Product::restrictQuantity(get('volume', 5)); |
94 | 177 |
|
95 | 178 | try {
|
96 |
| - $product = Product::from($product); |
97 |
| - $price = $product->price(); |
| 179 | + $product = Product::from($productId); |
| 180 | + $price = $product->price(); |
| 181 | + $passthrough = new Passthrough(teamDonation: option('buy.donation.teamAmount') * $quantity); |
| 182 | + |
| 183 | + $eurPrice = $product->price('EUR')->volume($quantity); |
| 184 | + $localizedPrice = $price->volume($quantity); |
| 185 | + |
98 | 186 | $prices = [
|
99 |
| - 'EUR:' . $product->price('EUR')->volume($quantity), |
100 |
| - $price->currency . ':' . $price->volume($quantity), |
| 187 | + 'EUR:' . $eurPrice, |
| 188 | + $price->currency . ':' . $localizedPrice, |
101 | 189 | ];
|
102 | 190 |
|
103 |
| - go($product->checkout('buy', compact('prices', 'quantity'))); |
| 191 | + go($product->checkout('buy', compact('prices', 'quantity', 'passthrough'))); |
104 | 192 | } catch (Throwable $e) {
|
105 | 193 | die( $e-> getMessage() . '<br>Please contact us: [email protected]');
|
106 | 194 | }
|
107 | 195 | }
|
108 | 196 | ],
|
109 | 197 | [
|
110 | 198 | 'pattern' => 'buy/volume/(enterprise|basic)/(:num)',
|
111 |
| - 'action' => function (string $product, int $quantity) { |
| 199 | + 'action' => function (string $productId, int $quantity) { |
| 200 | + $quantity = Product::restrictQuantity($quantity); |
| 201 | + |
112 | 202 | try {
|
113 |
| - $product = Product::from($product); |
114 |
| - $price = $product->price(); |
| 203 | + $product = Product::from($productId); |
| 204 | + $price = $product->price(); |
| 205 | + $passthrough = new Passthrough(teamDonation: option('buy.donation.teamAmount') * $quantity); |
| 206 | + |
115 | 207 | $prices = [
|
116 | 208 | 'EUR:' . $product->price('EUR')->volume($quantity),
|
117 | 209 | $price->currency . ':' . $price->volume($quantity),
|
118 | 210 | ];
|
119 | 211 |
|
120 |
| - go($product->checkout('buy', compact('prices', 'quantity'))); |
| 212 | + go($product->checkout('buy', compact('prices', 'quantity', 'passthrough'))); |
121 | 213 | } catch (Throwable $e) {
|
122 | 214 | die( $e-> getMessage() . '<br>Please contact us: [email protected]');
|
123 | 215 | }
|
|
0 commit comments