@@ -15,60 +15,60 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class QuantityField extends NumericField |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - private static $allowed_actions = [ |
|
| 22 | - 'newvalue', |
|
| 23 | - ]; |
|
| 18 | + /** |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + private static $allowed_actions = [ |
|
| 22 | + 'newvalue', |
|
| 23 | + ]; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param array $properties |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function Field($properties = []) |
|
| 30 | - { |
|
| 31 | - //Requirements::javascript('dynamic/foxystripe: javascript/quantity.js'); |
|
| 32 | - //Requirements::css('dynamic/foxystripe: client/dist/css/quantityfield.css'); |
|
| 25 | + /** |
|
| 26 | + * @param array $properties |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function Field($properties = []) |
|
| 30 | + { |
|
| 31 | + //Requirements::javascript('dynamic/foxystripe: javascript/quantity.js'); |
|
| 32 | + //Requirements::css('dynamic/foxystripe: client/dist/css/quantityfield.css'); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - $this->setAttribute('data-link', $this->Link('newvalue')); |
|
| 36 | - $this->setAttribute('data-code', $this->getProduct()->Code); |
|
| 37 | - $this->setAttribute('data-id', $this->getProduct()->ID); |
|
| 35 | + $this->setAttribute('data-link', $this->Link('newvalue')); |
|
| 36 | + $this->setAttribute('data-code', $this->getProduct()->Code); |
|
| 37 | + $this->setAttribute('data-id', $this->getProduct()->ID); |
|
| 38 | 38 | |
| 39 | - return parent::Field($properties); |
|
| 40 | - } |
|
| 39 | + return parent::Field($properties); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return ProductPage |
|
| 44 | - */ |
|
| 45 | - public function getProduct() |
|
| 46 | - { |
|
| 47 | - return $this->getForm()->getProduct(); |
|
| 48 | - } |
|
| 42 | + /** |
|
| 43 | + * @return ProductPage |
|
| 44 | + */ |
|
| 45 | + public function getProduct() |
|
| 46 | + { |
|
| 47 | + return $this->getForm()->getProduct(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param SS_HTTPRequest $request |
|
| 52 | - * @return bool|string |
|
| 53 | - */ |
|
| 54 | - public function newvalue(HTTPRequest $request) |
|
| 55 | - { |
|
| 56 | - if (!$value = $request->getVar('value')) { |
|
| 57 | - return ''; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * @param SS_HTTPRequest $request |
|
| 52 | + * @return bool|string |
|
| 53 | + */ |
|
| 54 | + public function newvalue(HTTPRequest $request) |
|
| 55 | + { |
|
| 56 | + if (!$value = $request->getVar('value')) { |
|
| 57 | + return ''; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - if (!$code = $request->getVar('code')) { |
|
| 61 | - return ''; |
|
| 62 | - } |
|
| 60 | + if (!$code = $request->getVar('code')) { |
|
| 61 | + return ''; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $this->extend('updateQuantity', $value); |
|
| 64 | + $this->extend('updateQuantity', $value); |
|
| 65 | 65 | |
| 66 | - $data = array( |
|
| 67 | - 'quantity' => $value, |
|
| 68 | - 'quantityGenerated' => ProductPage::getGeneratedValue($code, 'quantity', $value, 'value'), |
|
| 69 | - ); |
|
| 66 | + $data = array( |
|
| 67 | + 'quantity' => $value, |
|
| 68 | + 'quantityGenerated' => ProductPage::getGeneratedValue($code, 'quantity', $value, 'value'), |
|
| 69 | + ); |
|
| 70 | 70 | |
| 71 | - $this->extend('updateData', $data); |
|
| 72 | - return json_encode($data); |
|
| 73 | - } |
|
| 71 | + $this->extend('updateData', $data); |
|
| 72 | + return json_encode($data); |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -13,319 +13,319 @@ |
||
| 13 | 13 | |
| 14 | 14 | class FoxyStripeController extends \PageController |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - const URLSEGMENT = 'foxystripe'; |
|
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - private static $allowed_actions = array( |
|
| 24 | - 'index', |
|
| 25 | - 'sso', |
|
| 26 | - ); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return string |
|
| 30 | - */ |
|
| 31 | - public function getURLSegment() |
|
| 32 | - { |
|
| 33 | - return self::URLSEGMENT; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @return string |
|
| 38 | - * |
|
| 39 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 40 | - */ |
|
| 41 | - public function index() |
|
| 42 | - { |
|
| 43 | - // handle POST from FoxyCart API transaction |
|
| 44 | - if ((isset($_POST['FoxyData']) or isset($_POST['FoxySubscriptionData']))) { |
|
| 45 | - $FoxyData_encrypted = (isset($_POST['FoxyData'])) ? |
|
| 46 | - urldecode($_POST['FoxyData']) : |
|
| 47 | - urldecode($_POST['FoxySubscriptionData']); |
|
| 48 | - $FoxyData_decrypted = \rc4crypt::decrypt(FoxyCart::getStoreKey(), $FoxyData_encrypted); |
|
| 49 | - |
|
| 50 | - // parse the response and save the order |
|
| 51 | - self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted); |
|
| 52 | - |
|
| 53 | - // extend to allow for additional integrations with Datafeed |
|
| 54 | - $this->extend('addIntegrations', $FoxyData_encrypted); |
|
| 55 | - |
|
| 56 | - return 'foxy'; |
|
| 57 | - } else { |
|
| 58 | - return 'No FoxyData or FoxySubscriptionData received.'; |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param $encrypted |
|
| 64 | - * @param $decrypted |
|
| 65 | - * |
|
| 66 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 67 | - */ |
|
| 68 | - public function handleDataFeed($encrypted, $decrypted) |
|
| 69 | - { |
|
| 70 | - $orders = new \SimpleXMLElement($decrypted); |
|
| 71 | - |
|
| 72 | - // loop over each transaction to find FoxyCart Order ID |
|
| 73 | - foreach ($orders->transactions->transaction as $transaction) { |
|
| 74 | - // if FoxyCart order id, then parse order |
|
| 75 | - if (isset($transaction->id)) { |
|
| 76 | - $order = Order::get()->filter('Order_ID', (int)$transaction->id)->First(); |
|
| 77 | - if (!$order) { |
|
| 78 | - $order = Order::create(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // save base order info |
|
| 82 | - $order->Order_ID = (int)$transaction->id; |
|
| 83 | - $order->Response = urlencode($encrypted); |
|
| 84 | - // first write needed otherwise it creates a duplicates |
|
| 85 | - $order->write(); |
|
| 86 | - $this->parseOrder($orders, $order); |
|
| 87 | - $order->write(); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param array $transactions |
|
| 94 | - * @param Order $order |
|
| 95 | - */ |
|
| 96 | - public function parseOrder($transactions, $order) |
|
| 97 | - { |
|
| 98 | - $this->parseOrderInfo($transactions, $order); |
|
| 99 | - $this->parseOrderCustomer($transactions, $order); |
|
| 100 | - $this->parseOrderDetails($transactions, $order); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param array $orders |
|
| 105 | - * @param Order $transaction |
|
| 106 | - */ |
|
| 107 | - public function parseOrderInfo($orders, $transaction) |
|
| 108 | - { |
|
| 109 | - foreach ($orders->transactions->transaction as $order) { |
|
| 110 | - // Record transaction data from FoxyCart Datafeed: |
|
| 111 | - $transaction->Store_ID = (int)$order->store_id; |
|
| 112 | - $transaction->TransactionDate = (string)$order->transaction_date; |
|
| 113 | - $transaction->ProductTotal = (float)$order->product_total; |
|
| 114 | - $transaction->TaxTotal = (float)$order->tax_total; |
|
| 115 | - $transaction->ShippingTotal = (float)$order->shipping_total; |
|
| 116 | - $transaction->OrderTotal = (float)$order->order_total; |
|
| 117 | - $transaction->ReceiptURL = (string)$order->receipt_url; |
|
| 118 | - $transaction->OrderStatus = (string)$order->status; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param array $orders |
|
| 124 | - * @param Order $transaction |
|
| 125 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 126 | - */ |
|
| 127 | - public function parseOrderCustomer($orders, $transaction) |
|
| 128 | - { |
|
| 129 | - foreach ($orders->transactions->transaction as $order) { |
|
| 130 | - if (!isset($order->customer_email) || $order->is_anonymous != 0) { |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // if Customer is existing member, associate with current order |
|
| 135 | - if (Member::get()->filter('Email', $order->customer_email)->First()) { |
|
| 136 | - $customer = Member::get()->filter('Email', $order->customer_email)->First(); |
|
| 137 | - /* todo: make sure local password is updated if changed on FoxyCart |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + const URLSEGMENT = 'foxystripe'; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + private static $allowed_actions = array( |
|
| 24 | + 'index', |
|
| 25 | + 'sso', |
|
| 26 | + ); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return string |
|
| 30 | + */ |
|
| 31 | + public function getURLSegment() |
|
| 32 | + { |
|
| 33 | + return self::URLSEGMENT; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @return string |
|
| 38 | + * |
|
| 39 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 40 | + */ |
|
| 41 | + public function index() |
|
| 42 | + { |
|
| 43 | + // handle POST from FoxyCart API transaction |
|
| 44 | + if ((isset($_POST['FoxyData']) or isset($_POST['FoxySubscriptionData']))) { |
|
| 45 | + $FoxyData_encrypted = (isset($_POST['FoxyData'])) ? |
|
| 46 | + urldecode($_POST['FoxyData']) : |
|
| 47 | + urldecode($_POST['FoxySubscriptionData']); |
|
| 48 | + $FoxyData_decrypted = \rc4crypt::decrypt(FoxyCart::getStoreKey(), $FoxyData_encrypted); |
|
| 49 | + |
|
| 50 | + // parse the response and save the order |
|
| 51 | + self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted); |
|
| 52 | + |
|
| 53 | + // extend to allow for additional integrations with Datafeed |
|
| 54 | + $this->extend('addIntegrations', $FoxyData_encrypted); |
|
| 55 | + |
|
| 56 | + return 'foxy'; |
|
| 57 | + } else { |
|
| 58 | + return 'No FoxyData or FoxySubscriptionData received.'; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param $encrypted |
|
| 64 | + * @param $decrypted |
|
| 65 | + * |
|
| 66 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 67 | + */ |
|
| 68 | + public function handleDataFeed($encrypted, $decrypted) |
|
| 69 | + { |
|
| 70 | + $orders = new \SimpleXMLElement($decrypted); |
|
| 71 | + |
|
| 72 | + // loop over each transaction to find FoxyCart Order ID |
|
| 73 | + foreach ($orders->transactions->transaction as $transaction) { |
|
| 74 | + // if FoxyCart order id, then parse order |
|
| 75 | + if (isset($transaction->id)) { |
|
| 76 | + $order = Order::get()->filter('Order_ID', (int)$transaction->id)->First(); |
|
| 77 | + if (!$order) { |
|
| 78 | + $order = Order::create(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // save base order info |
|
| 82 | + $order->Order_ID = (int)$transaction->id; |
|
| 83 | + $order->Response = urlencode($encrypted); |
|
| 84 | + // first write needed otherwise it creates a duplicates |
|
| 85 | + $order->write(); |
|
| 86 | + $this->parseOrder($orders, $order); |
|
| 87 | + $order->write(); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param array $transactions |
|
| 94 | + * @param Order $order |
|
| 95 | + */ |
|
| 96 | + public function parseOrder($transactions, $order) |
|
| 97 | + { |
|
| 98 | + $this->parseOrderInfo($transactions, $order); |
|
| 99 | + $this->parseOrderCustomer($transactions, $order); |
|
| 100 | + $this->parseOrderDetails($transactions, $order); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param array $orders |
|
| 105 | + * @param Order $transaction |
|
| 106 | + */ |
|
| 107 | + public function parseOrderInfo($orders, $transaction) |
|
| 108 | + { |
|
| 109 | + foreach ($orders->transactions->transaction as $order) { |
|
| 110 | + // Record transaction data from FoxyCart Datafeed: |
|
| 111 | + $transaction->Store_ID = (int)$order->store_id; |
|
| 112 | + $transaction->TransactionDate = (string)$order->transaction_date; |
|
| 113 | + $transaction->ProductTotal = (float)$order->product_total; |
|
| 114 | + $transaction->TaxTotal = (float)$order->tax_total; |
|
| 115 | + $transaction->ShippingTotal = (float)$order->shipping_total; |
|
| 116 | + $transaction->OrderTotal = (float)$order->order_total; |
|
| 117 | + $transaction->ReceiptURL = (string)$order->receipt_url; |
|
| 118 | + $transaction->OrderStatus = (string)$order->status; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param array $orders |
|
| 124 | + * @param Order $transaction |
|
| 125 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 126 | + */ |
|
| 127 | + public function parseOrderCustomer($orders, $transaction) |
|
| 128 | + { |
|
| 129 | + foreach ($orders->transactions->transaction as $order) { |
|
| 130 | + if (!isset($order->customer_email) || $order->is_anonymous != 0) { |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // if Customer is existing member, associate with current order |
|
| 135 | + if (Member::get()->filter('Email', $order->customer_email)->First()) { |
|
| 136 | + $customer = Member::get()->filter('Email', $order->customer_email)->First(); |
|
| 137 | + /* todo: make sure local password is updated if changed on FoxyCart |
|
| 138 | 138 | $this->updatePasswordFromData($customer, $order); |
| 139 | 139 | */ |
| 140 | - } else { |
|
| 141 | - // create new Member, set password info from FoxyCart |
|
| 142 | - $customer = Member::create(); |
|
| 143 | - $customer->Customer_ID = (int)$order->customer_id; |
|
| 144 | - $customer->FirstName = (string)$order->customer_first_name; |
|
| 145 | - $customer->Surname = (string)$order->customer_last_name; |
|
| 146 | - $customer->Email = (string)$order->customer_email; |
|
| 147 | - $this->updatePasswordFromData($customer, $order); |
|
| 148 | - } |
|
| 149 | - $customer->write(); |
|
| 150 | - // set Order MemberID |
|
| 151 | - $transaction->MemberID = $customer->ID; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Updates a customer's password. Sets password encryption to 'none' to avoid encryting it again. |
|
| 157 | - * |
|
| 158 | - * @param Member $customer |
|
| 159 | - * @param $order |
|
| 160 | - */ |
|
| 161 | - public function updatePasswordFromData($customer, $order) |
|
| 162 | - { |
|
| 163 | - $password_encryption_algorithm = Security::config()->get('password_encryption_algorithm'); |
|
| 164 | - Security::config()->update('password_encryption_algorithm', 'none'); |
|
| 165 | - |
|
| 166 | - $customer->PasswordEncryption = 'none'; |
|
| 167 | - $customer->Password = (string) $order->customer_password; |
|
| 168 | - $customer->write(); |
|
| 169 | - |
|
| 170 | - $customer->PasswordEncryption = $this->getEncryption((string) $order->customer_password_hash_type); |
|
| 171 | - $customer->Salt = (string) $order->customer_password_salt; |
|
| 172 | - |
|
| 173 | - Security::config()->update('password_encryption_algorithm', $password_encryption_algorithm); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param string $hashType |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - private function getEncryption($hashType) |
|
| 181 | - { |
|
| 182 | - // TODO - update this with new/correct types |
|
| 183 | - switch (true) { |
|
| 184 | - case stristr($hashType, 'sha1'): |
|
| 185 | - return 'sha1_v2.4'; |
|
| 186 | - case stristr($hashType, 'sha256'): |
|
| 187 | - return 'sha256'; |
|
| 188 | - case stristr($hashType, 'md5'): |
|
| 189 | - return 'md5'; |
|
| 190 | - case stristr($hashType, 'bcrypt'): |
|
| 191 | - return 'bcrypt'; |
|
| 192 | - default: |
|
| 193 | - return 'none'; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param array $orders |
|
| 199 | - * @param Order $transaction |
|
| 200 | - */ |
|
| 201 | - public function parseOrderDetails($orders, $transaction) |
|
| 202 | - { |
|
| 203 | - // remove previous OrderDetails so we don't end up with duplicates |
|
| 204 | - foreach ($transaction->Details() as $detail) { |
|
| 205 | - $detail->delete(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - foreach ($orders->transactions->transaction as $order) { |
|
| 209 | - // Associate ProductPages, Options, Quanity with Order |
|
| 210 | - foreach ($order->transaction_details->transaction_detail as $product) { |
|
| 211 | - $this->orderDetailFromProduct($product, $transaction); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param $product |
|
| 218 | - * @param $transaction |
|
| 219 | - */ |
|
| 220 | - public function orderDetailFromProduct($product, $transaction) |
|
| 221 | - { |
|
| 222 | - $OrderDetail = OrderDetail::create(); |
|
| 223 | - $OrderDetail->Quantity = (int)$product->product_quantity; |
|
| 224 | - $OrderDetail->Price = (float)$product->product_price; |
|
| 225 | - // Find product via product_id custom variable |
|
| 226 | - |
|
| 227 | - foreach ($this->getTransactionOptions($product) as $productID) { |
|
| 228 | - $productPage = $this->getProductPage($product); |
|
| 229 | - $this->modifyOrderDetailPrice($productPage, $OrderDetail, $product); |
|
| 230 | - // associate with this order |
|
| 231 | - $OrderDetail->OrderID = $transaction->ID; |
|
| 232 | - // extend OrderDetail parsing, allowing for recording custom fields from FoxyCart |
|
| 233 | - $this->extend('handleOrderItem', $decrypted, $product, $OrderDetail); |
|
| 234 | - // write |
|
| 235 | - $OrderDetail->write(); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @param $product |
|
| 241 | - * @return \Generator |
|
| 242 | - */ |
|
| 243 | - public function getTransactionOptions($product) |
|
| 244 | - { |
|
| 245 | - foreach ($product->transaction_detail_options->transaction_detail_option as $productOption) { |
|
| 246 | - yield $productOption; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param $product |
|
| 252 | - * @return bool|ProductPage |
|
| 253 | - */ |
|
| 254 | - public function getProductPage($product) |
|
| 255 | - { |
|
| 256 | - foreach ($this->getTransactionOptions($product) as $productOptions) { |
|
| 257 | - if ($productOptions->product_option_name != 'product_id') { |
|
| 258 | - continue; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - return ProductPage::get() |
|
| 262 | - ->filter('ID', (int) $productOptions->product_option_value) |
|
| 263 | - ->First(); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @param bool|ProductPage $OrderProduct |
|
| 269 | - * @param OrderDetail $OrderDetail |
|
| 270 | - */ |
|
| 271 | - public function modifyOrderDetailPrice($OrderProduct, $OrderDetail, $product) |
|
| 272 | - { |
|
| 273 | - if (!$OrderProduct) { |
|
| 274 | - return; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - $OrderDetail->ProductID = $OrderProduct->ID; |
|
| 278 | - |
|
| 279 | - foreach ($this->getTransactionOptions($product) as $option) { |
|
| 280 | - $OptionItem = OptionItem::get()->filter(array( |
|
| 281 | - 'ProductID' => (string)$OrderProduct->ID, |
|
| 282 | - 'Title' => (string)$option->product_option_value |
|
| 283 | - ))->First(); |
|
| 284 | - |
|
| 285 | - if (!$OptionItem) { |
|
| 286 | - continue; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - $OrderDetail->OptionItems()->add($OptionItem); |
|
| 290 | - // modify product price |
|
| 291 | - if ($priceMod = $option->price_mod) { |
|
| 292 | - $OrderDetail->Price += $priceMod; |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Single Sign on integration with FoxyCart. |
|
| 299 | - */ |
|
| 300 | - public function sso() |
|
| 301 | - { |
|
| 302 | - |
|
| 303 | - // GET variables from FoxyCart Request |
|
| 304 | - $fcsid = $this->request->getVar('fcsid'); |
|
| 305 | - $timestampNew = strtotime('+30 days'); |
|
| 306 | - |
|
| 307 | - // get current member if logged in. If not, create a 'fake' user with Customer_ID = 0 |
|
| 308 | - // fake user will redirect to FC checkout, ask customer to log in |
|
| 309 | - // to do: consider a login/registration form here if not logged in |
|
| 310 | - if ($Member = Security::getCurrentUser()) { |
|
| 311 | - $Member = Security::getCurrentUser(); |
|
| 312 | - } else { |
|
| 313 | - $Member = new Member(); |
|
| 314 | - $Member->Customer_ID = 0; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey()); |
|
| 318 | - |
|
| 319 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 320 | - if ($config->CustomSSL) { |
|
| 321 | - $link = FoxyCart::getFoxyCartStoreName(); |
|
| 322 | - } else { |
|
| 323 | - $link = FoxyCart::getFoxyCartStoreName() . '.foxycart.com'; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $redirect_complete = 'https://'.$link.'/checkout?fc_auth_token='.$auth_token.'&fcsid='.$fcsid. |
|
| 327 | - '&fc_customer_id='.$Member->Customer_ID.'×tamp='.$timestampNew; |
|
| 328 | - |
|
| 329 | - $this->redirect($redirect_complete); |
|
| 330 | - } |
|
| 140 | + } else { |
|
| 141 | + // create new Member, set password info from FoxyCart |
|
| 142 | + $customer = Member::create(); |
|
| 143 | + $customer->Customer_ID = (int)$order->customer_id; |
|
| 144 | + $customer->FirstName = (string)$order->customer_first_name; |
|
| 145 | + $customer->Surname = (string)$order->customer_last_name; |
|
| 146 | + $customer->Email = (string)$order->customer_email; |
|
| 147 | + $this->updatePasswordFromData($customer, $order); |
|
| 148 | + } |
|
| 149 | + $customer->write(); |
|
| 150 | + // set Order MemberID |
|
| 151 | + $transaction->MemberID = $customer->ID; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Updates a customer's password. Sets password encryption to 'none' to avoid encryting it again. |
|
| 157 | + * |
|
| 158 | + * @param Member $customer |
|
| 159 | + * @param $order |
|
| 160 | + */ |
|
| 161 | + public function updatePasswordFromData($customer, $order) |
|
| 162 | + { |
|
| 163 | + $password_encryption_algorithm = Security::config()->get('password_encryption_algorithm'); |
|
| 164 | + Security::config()->update('password_encryption_algorithm', 'none'); |
|
| 165 | + |
|
| 166 | + $customer->PasswordEncryption = 'none'; |
|
| 167 | + $customer->Password = (string) $order->customer_password; |
|
| 168 | + $customer->write(); |
|
| 169 | + |
|
| 170 | + $customer->PasswordEncryption = $this->getEncryption((string) $order->customer_password_hash_type); |
|
| 171 | + $customer->Salt = (string) $order->customer_password_salt; |
|
| 172 | + |
|
| 173 | + Security::config()->update('password_encryption_algorithm', $password_encryption_algorithm); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param string $hashType |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + private function getEncryption($hashType) |
|
| 181 | + { |
|
| 182 | + // TODO - update this with new/correct types |
|
| 183 | + switch (true) { |
|
| 184 | + case stristr($hashType, 'sha1'): |
|
| 185 | + return 'sha1_v2.4'; |
|
| 186 | + case stristr($hashType, 'sha256'): |
|
| 187 | + return 'sha256'; |
|
| 188 | + case stristr($hashType, 'md5'): |
|
| 189 | + return 'md5'; |
|
| 190 | + case stristr($hashType, 'bcrypt'): |
|
| 191 | + return 'bcrypt'; |
|
| 192 | + default: |
|
| 193 | + return 'none'; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param array $orders |
|
| 199 | + * @param Order $transaction |
|
| 200 | + */ |
|
| 201 | + public function parseOrderDetails($orders, $transaction) |
|
| 202 | + { |
|
| 203 | + // remove previous OrderDetails so we don't end up with duplicates |
|
| 204 | + foreach ($transaction->Details() as $detail) { |
|
| 205 | + $detail->delete(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + foreach ($orders->transactions->transaction as $order) { |
|
| 209 | + // Associate ProductPages, Options, Quanity with Order |
|
| 210 | + foreach ($order->transaction_details->transaction_detail as $product) { |
|
| 211 | + $this->orderDetailFromProduct($product, $transaction); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param $product |
|
| 218 | + * @param $transaction |
|
| 219 | + */ |
|
| 220 | + public function orderDetailFromProduct($product, $transaction) |
|
| 221 | + { |
|
| 222 | + $OrderDetail = OrderDetail::create(); |
|
| 223 | + $OrderDetail->Quantity = (int)$product->product_quantity; |
|
| 224 | + $OrderDetail->Price = (float)$product->product_price; |
|
| 225 | + // Find product via product_id custom variable |
|
| 226 | + |
|
| 227 | + foreach ($this->getTransactionOptions($product) as $productID) { |
|
| 228 | + $productPage = $this->getProductPage($product); |
|
| 229 | + $this->modifyOrderDetailPrice($productPage, $OrderDetail, $product); |
|
| 230 | + // associate with this order |
|
| 231 | + $OrderDetail->OrderID = $transaction->ID; |
|
| 232 | + // extend OrderDetail parsing, allowing for recording custom fields from FoxyCart |
|
| 233 | + $this->extend('handleOrderItem', $decrypted, $product, $OrderDetail); |
|
| 234 | + // write |
|
| 235 | + $OrderDetail->write(); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param $product |
|
| 241 | + * @return \Generator |
|
| 242 | + */ |
|
| 243 | + public function getTransactionOptions($product) |
|
| 244 | + { |
|
| 245 | + foreach ($product->transaction_detail_options->transaction_detail_option as $productOption) { |
|
| 246 | + yield $productOption; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param $product |
|
| 252 | + * @return bool|ProductPage |
|
| 253 | + */ |
|
| 254 | + public function getProductPage($product) |
|
| 255 | + { |
|
| 256 | + foreach ($this->getTransactionOptions($product) as $productOptions) { |
|
| 257 | + if ($productOptions->product_option_name != 'product_id') { |
|
| 258 | + continue; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + return ProductPage::get() |
|
| 262 | + ->filter('ID', (int) $productOptions->product_option_value) |
|
| 263 | + ->First(); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @param bool|ProductPage $OrderProduct |
|
| 269 | + * @param OrderDetail $OrderDetail |
|
| 270 | + */ |
|
| 271 | + public function modifyOrderDetailPrice($OrderProduct, $OrderDetail, $product) |
|
| 272 | + { |
|
| 273 | + if (!$OrderProduct) { |
|
| 274 | + return; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + $OrderDetail->ProductID = $OrderProduct->ID; |
|
| 278 | + |
|
| 279 | + foreach ($this->getTransactionOptions($product) as $option) { |
|
| 280 | + $OptionItem = OptionItem::get()->filter(array( |
|
| 281 | + 'ProductID' => (string)$OrderProduct->ID, |
|
| 282 | + 'Title' => (string)$option->product_option_value |
|
| 283 | + ))->First(); |
|
| 284 | + |
|
| 285 | + if (!$OptionItem) { |
|
| 286 | + continue; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + $OrderDetail->OptionItems()->add($OptionItem); |
|
| 290 | + // modify product price |
|
| 291 | + if ($priceMod = $option->price_mod) { |
|
| 292 | + $OrderDetail->Price += $priceMod; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Single Sign on integration with FoxyCart. |
|
| 299 | + */ |
|
| 300 | + public function sso() |
|
| 301 | + { |
|
| 302 | + |
|
| 303 | + // GET variables from FoxyCart Request |
|
| 304 | + $fcsid = $this->request->getVar('fcsid'); |
|
| 305 | + $timestampNew = strtotime('+30 days'); |
|
| 306 | + |
|
| 307 | + // get current member if logged in. If not, create a 'fake' user with Customer_ID = 0 |
|
| 308 | + // fake user will redirect to FC checkout, ask customer to log in |
|
| 309 | + // to do: consider a login/registration form here if not logged in |
|
| 310 | + if ($Member = Security::getCurrentUser()) { |
|
| 311 | + $Member = Security::getCurrentUser(); |
|
| 312 | + } else { |
|
| 313 | + $Member = new Member(); |
|
| 314 | + $Member->Customer_ID = 0; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey()); |
|
| 318 | + |
|
| 319 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 320 | + if ($config->CustomSSL) { |
|
| 321 | + $link = FoxyCart::getFoxyCartStoreName(); |
|
| 322 | + } else { |
|
| 323 | + $link = FoxyCart::getFoxyCartStoreName() . '.foxycart.com'; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $redirect_complete = 'https://'.$link.'/checkout?fc_auth_token='.$auth_token.'&fcsid='.$fcsid. |
|
| 327 | + '&fc_customer_id='.$Member->Customer_ID.'×tamp='.$timestampNew; |
|
| 328 | + |
|
| 329 | + $this->redirect($redirect_complete); |
|
| 330 | + } |
|
| 331 | 331 | } |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class DonationProductController extends ProductPageController |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - private static $allowed_actions = [ |
|
| 24 | - 'PurchaseForm', |
|
| 25 | - 'updatevalue', |
|
| 26 | - ]; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - */ |
|
| 31 | - public function init() |
|
| 32 | - { |
|
| 33 | - parent::init(); |
|
| 34 | - //Requirements::javascript('framework/thirdparty/jquery/jquery.js'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @return FoxyStripePurchaseForm |
|
| 39 | - */ |
|
| 40 | - public function PurchaseForm() |
|
| 41 | - { |
|
| 42 | - $form = parent::PurchaseForm(); |
|
| 43 | - |
|
| 44 | - $fields = $form->Fields(); |
|
| 45 | - |
|
| 46 | - $fields->dataFieldByName('price') |
|
| 47 | - ->setAttribute('id', 'price'); |
|
| 48 | - |
|
| 49 | - $fields->insertAfter('price', $currencyField = CurrencyField::create('x:visiblePrice', 'Amount')); |
|
| 50 | - |
|
| 51 | - $currencyField->setAttribute('id', 'visiblePrice'); |
|
| 52 | - |
|
| 53 | - $fields->removeByName([ |
|
| 54 | - 'x:visibleQuantity', |
|
| 55 | - ProductPage::getGeneratedValue($this->Code, 'weight', $this->Weight), |
|
| 56 | - ]); |
|
| 57 | - |
|
| 58 | - if (FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
|
| 59 | - Requirements::javascript("silverstripe/userforms:client/dist/js/jquery-validation/jquery.validate.min.js"); |
|
| 60 | - Requirements::javascriptTemplate('_resources/vendor/dynamic/foxystripe/javascript/donationProduct.js', [ |
|
| 61 | - 'Trigger' => (string)$currencyField->getAttribute('id'), |
|
| 62 | - 'UpdateURL' => Director::absoluteURL($this->Link('updatevalue')), |
|
| 63 | - ]); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $form->setAttribute('data-updateurl', $this->Link('updatevalue')); |
|
| 67 | - |
|
| 68 | - return $form; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * create new encrypted price value based on user input. |
|
| 73 | - * |
|
| 74 | - * @param $request |
|
| 75 | - * |
|
| 76 | - * @return string|\SilverStripe\Control\HTTPResponse |
|
| 77 | - */ |
|
| 78 | - public function updatevalue(\SilverStripe\Control\HTTPRequest $request) |
|
| 79 | - { |
|
| 80 | - if ($request->getVar('Price') && FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
|
| 81 | - $price = $request->getVar('Price'); |
|
| 82 | - $signedPrice = \FoxyCart_Helper::fc_hash_value($this->Code, 'price', $price, 'value', false); |
|
| 83 | - $json = json_encode(['Price' => $signedPrice]); |
|
| 84 | - |
|
| 85 | - $this->response->setBody($json); |
|
| 86 | - $this->response->addHeader('Content-Type', 'application/json'); |
|
| 87 | - |
|
| 88 | - return $this->response; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return 'false'; |
|
| 92 | - } |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + private static $allowed_actions = [ |
|
| 24 | + 'PurchaseForm', |
|
| 25 | + 'updatevalue', |
|
| 26 | + ]; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + */ |
|
| 31 | + public function init() |
|
| 32 | + { |
|
| 33 | + parent::init(); |
|
| 34 | + //Requirements::javascript('framework/thirdparty/jquery/jquery.js'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @return FoxyStripePurchaseForm |
|
| 39 | + */ |
|
| 40 | + public function PurchaseForm() |
|
| 41 | + { |
|
| 42 | + $form = parent::PurchaseForm(); |
|
| 43 | + |
|
| 44 | + $fields = $form->Fields(); |
|
| 45 | + |
|
| 46 | + $fields->dataFieldByName('price') |
|
| 47 | + ->setAttribute('id', 'price'); |
|
| 48 | + |
|
| 49 | + $fields->insertAfter('price', $currencyField = CurrencyField::create('x:visiblePrice', 'Amount')); |
|
| 50 | + |
|
| 51 | + $currencyField->setAttribute('id', 'visiblePrice'); |
|
| 52 | + |
|
| 53 | + $fields->removeByName([ |
|
| 54 | + 'x:visibleQuantity', |
|
| 55 | + ProductPage::getGeneratedValue($this->Code, 'weight', $this->Weight), |
|
| 56 | + ]); |
|
| 57 | + |
|
| 58 | + if (FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
|
| 59 | + Requirements::javascript("silverstripe/userforms:client/dist/js/jquery-validation/jquery.validate.min.js"); |
|
| 60 | + Requirements::javascriptTemplate('_resources/vendor/dynamic/foxystripe/javascript/donationProduct.js', [ |
|
| 61 | + 'Trigger' => (string)$currencyField->getAttribute('id'), |
|
| 62 | + 'UpdateURL' => Director::absoluteURL($this->Link('updatevalue')), |
|
| 63 | + ]); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $form->setAttribute('data-updateurl', $this->Link('updatevalue')); |
|
| 67 | + |
|
| 68 | + return $form; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * create new encrypted price value based on user input. |
|
| 73 | + * |
|
| 74 | + * @param $request |
|
| 75 | + * |
|
| 76 | + * @return string|\SilverStripe\Control\HTTPResponse |
|
| 77 | + */ |
|
| 78 | + public function updatevalue(\SilverStripe\Control\HTTPRequest $request) |
|
| 79 | + { |
|
| 80 | + if ($request->getVar('Price') && FoxyStripeSetting::current_foxystripe_setting()->CartValidation) { |
|
| 81 | + $price = $request->getVar('Price'); |
|
| 82 | + $signedPrice = \FoxyCart_Helper::fc_hash_value($this->Code, 'price', $price, 'value', false); |
|
| 83 | + $json = json_encode(['Price' => $signedPrice]); |
|
| 84 | + |
|
| 85 | + $this->response->setBody($json); |
|
| 86 | + $this->response->addHeader('Content-Type', 'application/json'); |
|
| 87 | + |
|
| 88 | + return $this->response; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return 'false'; |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -20,158 +20,158 @@ |
||
| 20 | 20 | class DataTestController extends Controller |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - private static $data = [ |
|
| 27 | - "TransactionDate" => "now", |
|
| 28 | - "OrderID" => "auto", |
|
| 29 | - "Email" => "auto", |
|
| 30 | - "Password" => "password", |
|
| 31 | - "OrderDetails" => [], |
|
| 32 | - ]; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 36 | - */ |
|
| 37 | - public function index() |
|
| 38 | - { |
|
| 39 | - $rules = Director::config()->get('rules'); |
|
| 40 | - $rule = array_search(FoxyStripeController::class, $rules); |
|
| 41 | - $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
| 42 | - $myKey = FoxyCart::getStoreKey(); |
|
| 43 | - |
|
| 44 | - $this->updateConfig(); |
|
| 45 | - $config = static::config()->get('data'); |
|
| 46 | - $config['OrderDetails'] = ArrayList::create($config['OrderDetails']); |
|
| 47 | - $xml = $this->renderWith('TestData', $config); |
|
| 48 | - $XMLOutput = $xml->RAW(); |
|
| 49 | - |
|
| 50 | - $XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
| 51 | - $XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
|
| 52 | - |
|
| 53 | - $ch = curl_init(); |
|
| 54 | - curl_setopt($ch, CURLOPT_URL, $myURL); |
|
| 55 | - curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
|
| 56 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 57 | - curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
|
| 58 | - |
|
| 59 | - $response = curl_exec($ch); |
|
| 60 | - $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 61 | - curl_close($ch); |
|
| 62 | - |
|
| 63 | - $configString = print_r(static::config()->get('data'), true); |
|
| 64 | - /** @var DebugView $view */ |
|
| 65 | - $view = Injector::inst()->create(DebugView::class); |
|
| 66 | - echo $view->renderHeader(); |
|
| 67 | - echo '<div class="info">'; |
|
| 68 | - echo "<h2>Config:</h2><pre>$configString</pre>"; |
|
| 69 | - if ($this->getRequest()->getVar('data')) { |
|
| 70 | - echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; |
|
| 71 | - } |
|
| 72 | - echo "<h2>Response:</h2><pre>$response</pre>"; |
|
| 73 | - echo '<p></p>'; |
|
| 74 | - echo '</div>'; |
|
| 75 | - echo $view->renderFooter(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * |
|
| 80 | - */ |
|
| 81 | - private function updateConfig() |
|
| 82 | - { |
|
| 83 | - $data = static::config()->get('data'); |
|
| 84 | - $transactionDate = $data['TransactionDate']; |
|
| 85 | - static::config()->merge('data', [ |
|
| 86 | - 'TransactionDate' => strtotime($transactionDate), |
|
| 87 | - ]); |
|
| 88 | - |
|
| 89 | - $order_id = $data['OrderID']; |
|
| 90 | - if ($order_id === 'auto' || $order_id < 1) { |
|
| 91 | - $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
|
| 92 | - static::config()->merge('data', [ |
|
| 93 | - 'OrderID' => $lastOrderID + 1, |
|
| 94 | - ]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $email = $data['Email']; |
|
| 98 | - if ($email === 'auto') { |
|
| 99 | - static::config()->merge('data', [ |
|
| 100 | - 'Email' => $this->generateEmail(), |
|
| 101 | - ]); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $orderDetails = $data['OrderDetails']; |
|
| 105 | - if (count($orderDetails) === 0) { |
|
| 106 | - static::config()->merge('data', [ |
|
| 107 | - 'OrderDetails' => [ |
|
| 108 | - $this->generateOrderDetail() |
|
| 109 | - ], |
|
| 110 | - ]); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if (!array_key_exists('Salt', $data)) { |
|
| 114 | - static::config()->merge('data', [ |
|
| 115 | - 'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', |
|
| 116 | - ]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - if (!array_key_exists('HashType', $data)) { |
|
| 120 | - static::config()->merge('data', [ |
|
| 121 | - 'HashType' => 'sha1_v2.4', |
|
| 122 | - ]); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $data = static::config()->get('data'); |
|
| 126 | - if (!array_key_exists('HashedPassword', $data)) { |
|
| 127 | - $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); |
|
| 128 | - static::config()->merge('data', [ |
|
| 129 | - 'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), |
|
| 130 | - ]); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - private function generateEmail() |
|
| 138 | - { |
|
| 139 | - $emails = Member::get()->filter([ |
|
| 140 | - 'Email:EndsWith' => '@example.com', |
|
| 141 | - ])->column('Email'); |
|
| 142 | - |
|
| 143 | - if ($emails && count($emails)) { |
|
| 144 | - $email = $emails[count($emails) - 1]; |
|
| 145 | - return preg_replace_callback( |
|
| 146 | - "|(\d+)|", |
|
| 147 | - function ($mathces) { |
|
| 148 | - return ++$mathces[1]; |
|
| 149 | - }, |
|
| 150 | ||
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - return '[email protected]'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return array |
|
| 158 | - */ |
|
| 159 | - private function generateOrderDetail() |
|
| 160 | - { |
|
| 161 | - return [ |
|
| 162 | - 'Title' => 'foo', |
|
| 163 | - 'Price' => 20.00, |
|
| 164 | - 'Quantity' => 1, |
|
| 165 | - 'Weight' => 0.1, |
|
| 166 | - 'DeliveryType' => 'shipped', |
|
| 167 | - 'CategoryDescription' => 'Default cateogry', |
|
| 168 | - 'CategoryCode' => 'DEFAULT', |
|
| 169 | - 'Options' => [ |
|
| 170 | - 'Name' => 'color', |
|
| 171 | - 'OptionValue' => 'blue', |
|
| 172 | - 'PriceMod' => '', |
|
| 173 | - 'WeightMod' => '', |
|
| 174 | - ], |
|
| 175 | - ]; |
|
| 176 | - } |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + private static $data = [ |
|
| 27 | + "TransactionDate" => "now", |
|
| 28 | + "OrderID" => "auto", |
|
| 29 | + "Email" => "auto", |
|
| 30 | + "Password" => "password", |
|
| 31 | + "OrderDetails" => [], |
|
| 32 | + ]; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 36 | + */ |
|
| 37 | + public function index() |
|
| 38 | + { |
|
| 39 | + $rules = Director::config()->get('rules'); |
|
| 40 | + $rule = array_search(FoxyStripeController::class, $rules); |
|
| 41 | + $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
| 42 | + $myKey = FoxyCart::getStoreKey(); |
|
| 43 | + |
|
| 44 | + $this->updateConfig(); |
|
| 45 | + $config = static::config()->get('data'); |
|
| 46 | + $config['OrderDetails'] = ArrayList::create($config['OrderDetails']); |
|
| 47 | + $xml = $this->renderWith('TestData', $config); |
|
| 48 | + $XMLOutput = $xml->RAW(); |
|
| 49 | + |
|
| 50 | + $XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
| 51 | + $XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
|
| 52 | + |
|
| 53 | + $ch = curl_init(); |
|
| 54 | + curl_setopt($ch, CURLOPT_URL, $myURL); |
|
| 55 | + curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
|
| 56 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 57 | + curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
|
| 58 | + |
|
| 59 | + $response = curl_exec($ch); |
|
| 60 | + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 61 | + curl_close($ch); |
|
| 62 | + |
|
| 63 | + $configString = print_r(static::config()->get('data'), true); |
|
| 64 | + /** @var DebugView $view */ |
|
| 65 | + $view = Injector::inst()->create(DebugView::class); |
|
| 66 | + echo $view->renderHeader(); |
|
| 67 | + echo '<div class="info">'; |
|
| 68 | + echo "<h2>Config:</h2><pre>$configString</pre>"; |
|
| 69 | + if ($this->getRequest()->getVar('data')) { |
|
| 70 | + echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; |
|
| 71 | + } |
|
| 72 | + echo "<h2>Response:</h2><pre>$response</pre>"; |
|
| 73 | + echo '<p></p>'; |
|
| 74 | + echo '</div>'; |
|
| 75 | + echo $view->renderFooter(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * |
|
| 80 | + */ |
|
| 81 | + private function updateConfig() |
|
| 82 | + { |
|
| 83 | + $data = static::config()->get('data'); |
|
| 84 | + $transactionDate = $data['TransactionDate']; |
|
| 85 | + static::config()->merge('data', [ |
|
| 86 | + 'TransactionDate' => strtotime($transactionDate), |
|
| 87 | + ]); |
|
| 88 | + |
|
| 89 | + $order_id = $data['OrderID']; |
|
| 90 | + if ($order_id === 'auto' || $order_id < 1) { |
|
| 91 | + $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
|
| 92 | + static::config()->merge('data', [ |
|
| 93 | + 'OrderID' => $lastOrderID + 1, |
|
| 94 | + ]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $email = $data['Email']; |
|
| 98 | + if ($email === 'auto') { |
|
| 99 | + static::config()->merge('data', [ |
|
| 100 | + 'Email' => $this->generateEmail(), |
|
| 101 | + ]); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $orderDetails = $data['OrderDetails']; |
|
| 105 | + if (count($orderDetails) === 0) { |
|
| 106 | + static::config()->merge('data', [ |
|
| 107 | + 'OrderDetails' => [ |
|
| 108 | + $this->generateOrderDetail() |
|
| 109 | + ], |
|
| 110 | + ]); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if (!array_key_exists('Salt', $data)) { |
|
| 114 | + static::config()->merge('data', [ |
|
| 115 | + 'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', |
|
| 116 | + ]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + if (!array_key_exists('HashType', $data)) { |
|
| 120 | + static::config()->merge('data', [ |
|
| 121 | + 'HashType' => 'sha1_v2.4', |
|
| 122 | + ]); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $data = static::config()->get('data'); |
|
| 126 | + if (!array_key_exists('HashedPassword', $data)) { |
|
| 127 | + $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); |
|
| 128 | + static::config()->merge('data', [ |
|
| 129 | + 'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), |
|
| 130 | + ]); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + private function generateEmail() |
|
| 138 | + { |
|
| 139 | + $emails = Member::get()->filter([ |
|
| 140 | + 'Email:EndsWith' => '@example.com', |
|
| 141 | + ])->column('Email'); |
|
| 142 | + |
|
| 143 | + if ($emails && count($emails)) { |
|
| 144 | + $email = $emails[count($emails) - 1]; |
|
| 145 | + return preg_replace_callback( |
|
| 146 | + "|(\d+)|", |
|
| 147 | + function ($mathces) { |
|
| 148 | + return ++$mathces[1]; |
|
| 149 | + }, |
|
| 150 | ||
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + return '[email protected]'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return array |
|
| 158 | + */ |
|
| 159 | + private function generateOrderDetail() |
|
| 160 | + { |
|
| 161 | + return [ |
|
| 162 | + 'Title' => 'foo', |
|
| 163 | + 'Price' => 20.00, |
|
| 164 | + 'Quantity' => 1, |
|
| 165 | + 'Weight' => 0.1, |
|
| 166 | + 'DeliveryType' => 'shipped', |
|
| 167 | + 'CategoryDescription' => 'Default cateogry', |
|
| 168 | + 'CategoryCode' => 'DEFAULT', |
|
| 169 | + 'Options' => [ |
|
| 170 | + 'Name' => 'color', |
|
| 171 | + 'OptionValue' => 'blue', |
|
| 172 | + 'PriceMod' => '', |
|
| 173 | + 'WeightMod' => '', |
|
| 174 | + ], |
|
| 175 | + ]; |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -12,35 +12,35 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class OrderHistoryPageController extends \PageController |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - private static $allowed_actions = array( |
|
| 19 | - 'index', |
|
| 20 | - ); |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + private static $allowed_actions = array( |
|
| 19 | + 'index', |
|
| 20 | + ); |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @return bool|\SilverStripe\Control\HTTPResponse |
|
| 24 | - */ |
|
| 25 | - public function checkMember() |
|
| 26 | - { |
|
| 27 | - if (Security::getCurrentUser()) { |
|
| 28 | - return true; |
|
| 29 | - } else { |
|
| 30 | - return Security::permissionFailure($this, _t( |
|
| 31 | - 'AccountPage.CANNOTCONFIRMLOGGEDIN', |
|
| 32 | - 'Please login to view this page.' |
|
| 33 | - )); |
|
| 34 | - } |
|
| 35 | - } |
|
| 22 | + /** |
|
| 23 | + * @return bool|\SilverStripe\Control\HTTPResponse |
|
| 24 | + */ |
|
| 25 | + public function checkMember() |
|
| 26 | + { |
|
| 27 | + if (Security::getCurrentUser()) { |
|
| 28 | + return true; |
|
| 29 | + } else { |
|
| 30 | + return Security::permissionFailure($this, _t( |
|
| 31 | + 'AccountPage.CANNOTCONFIRMLOGGEDIN', |
|
| 32 | + 'Please login to view this page.' |
|
| 33 | + )); |
|
| 34 | + } |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function Index() |
|
| 41 | - { |
|
| 42 | - $this->checkMember(); |
|
| 37 | + /** |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function Index() |
|
| 41 | + { |
|
| 42 | + $this->checkMember(); |
|
| 43 | 43 | |
| 44 | - return array(); |
|
| 45 | - } |
|
| 44 | + return array(); |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -13,41 +13,41 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ProductPageController extends \PageController |
| 15 | 15 | { |
| 16 | - private static $allowed_actions = array( |
|
| 17 | - 'PurchaseForm', |
|
| 18 | - ); |
|
| 19 | - |
|
| 20 | - public function init() |
|
| 21 | - { |
|
| 22 | - parent::init(); |
|
| 23 | - Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); |
|
| 24 | - if ($this->data()->Available && $this->ProductOptions()->exists()) { |
|
| 25 | - $formName = $this->PurchaseForm()->FormName(); |
|
| 26 | - /*Requirements::javascriptTemplate( |
|
| 16 | + private static $allowed_actions = array( |
|
| 17 | + 'PurchaseForm', |
|
| 18 | + ); |
|
| 19 | + |
|
| 20 | + public function init() |
|
| 21 | + { |
|
| 22 | + parent::init(); |
|
| 23 | + Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); |
|
| 24 | + if ($this->data()->Available && $this->ProductOptions()->exists()) { |
|
| 25 | + $formName = $this->PurchaseForm()->FormName(); |
|
| 26 | + /*Requirements::javascriptTemplate( |
|
| 27 | 27 | 'dynamic/foxystripe: javascript/out_of_stock.js', |
| 28 | 28 | [ |
| 29 | 29 | 'FormName' => $formName, |
| 30 | 30 | ], |
| 31 | 31 | 'foxystripe.out_of_stock' |
| 32 | 32 | );*/ |
| 33 | - Requirements::javascript('dynamic/foxystripe: javascript/product_options.js'); |
|
| 34 | - } |
|
| 33 | + Requirements::javascript('dynamic/foxystripe: javascript/product_options.js'); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - Requirements::customScript(<<<JS |
|
| 36 | + Requirements::customScript(<<<JS |
|
| 37 | 37 | var productID = {$this->data()->ID}; |
| 38 | 38 | JS |
| 39 | - ); |
|
| 40 | - } |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @return FoxyStripePurchaseForm |
|
| 44 | - */ |
|
| 45 | - public function PurchaseForm() |
|
| 46 | - { |
|
| 47 | - $form = FoxyStripePurchaseForm::create($this, __FUNCTION__, null, null, null, $this->data()); |
|
| 42 | + /** |
|
| 43 | + * @return FoxyStripePurchaseForm |
|
| 44 | + */ |
|
| 45 | + public function PurchaseForm() |
|
| 46 | + { |
|
| 47 | + $form = FoxyStripePurchaseForm::create($this, __FUNCTION__, null, null, null, $this->data()); |
|
| 48 | 48 | |
| 49 | - $this->extend('updateFoxyStripePurchaseForm', $form); |
|
| 49 | + $this->extend('updateFoxyStripePurchaseForm', $form); |
|
| 50 | 50 | |
| 51 | - return $form; |
|
| 52 | - } |
|
| 51 | + return $form; |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -18,163 +18,163 @@ |
||
| 18 | 18 | |
| 19 | 19 | class FoxyStripeAdmin extends LeftAndMain |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - private static $url_segment = 'foxystripe'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private static $url_rule = '/$Action/$ID/$OtherID'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - private static $menu_priority = 4; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private static $menu_title = 'FoxyStripe'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private static $menu_icon_class = 'font-icon-cog'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private static $tree_class = FoxyStripeSetting::class; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var array |
|
| 53 | - */ |
|
| 54 | - private static $required_permission_codes = ['EDIT_GLOBAL_PERMISSION']; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Initialises the {@link FoxyStripeSetting} controller. |
|
| 58 | - */ |
|
| 59 | - public function init() |
|
| 60 | - { |
|
| 61 | - parent::init(); |
|
| 62 | - |
|
| 63 | - Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js'); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param null $id |
|
| 68 | - * @param null $fields |
|
| 69 | - * |
|
| 70 | - * @return $this|Form |
|
| 71 | - */ |
|
| 72 | - public function getEditForm($id = null, $fields = null) |
|
| 73 | - { |
|
| 74 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 75 | - $fields = $config->getCMSFields(); |
|
| 76 | - |
|
| 77 | - // Tell the CMS what URL the preview should show |
|
| 78 | - $home = Director::absoluteBaseURL(); |
|
| 79 | - $fields->push(new HiddenField('PreviewURL', 'Preview URL', $home)); |
|
| 80 | - |
|
| 81 | - // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load |
|
| 82 | - $fields->push($navField = new LiteralField( |
|
| 83 | - 'SilverStripeNavigator', |
|
| 84 | - $this->getSilverStripeNavigator() |
|
| 85 | - )); |
|
| 86 | - $navField->setAllowHTML(true); |
|
| 87 | - |
|
| 88 | - // Retrieve validator, if one has been setup (e.g. via data extensions). |
|
| 89 | - if ($config->hasMethod('getCMSValidator')) { |
|
| 90 | - $validator = $config->getCMSValidator(); |
|
| 91 | - } else { |
|
| 92 | - $validator = null; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $actions = $config->getCMSActions(); |
|
| 96 | - $negotiator = $this->getResponseNegotiator(); |
|
| 97 | - $form = Form::create( |
|
| 98 | - $this, |
|
| 99 | - 'EditForm', |
|
| 100 | - $fields, |
|
| 101 | - $actions, |
|
| 102 | - $validator |
|
| 103 | - )->setHTMLID('Form_EditForm'); |
|
| 104 | - $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) { |
|
| 105 | - $request = $this->getRequest(); |
|
| 106 | - if ($request->isAjax() && $negotiator) { |
|
| 107 | - $result = $form->forTemplate(); |
|
| 108 | - |
|
| 109 | - return $negotiator->respond($request, array( |
|
| 110 | - 'CurrentForm' => function () use ($result) { |
|
| 111 | - return $result; |
|
| 112 | - }, |
|
| 113 | - )); |
|
| 114 | - } |
|
| 115 | - return null; |
|
| 116 | - }); |
|
| 117 | - $form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form'); |
|
| 118 | - $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
|
| 119 | - |
|
| 120 | - if ($form->Fields()->hasTabSet()) { |
|
| 121 | - $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
|
| 122 | - } |
|
| 123 | - $form->setHTMLID('Form_EditForm'); |
|
| 124 | - $form->loadDataFrom($config); |
|
| 125 | - $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
|
| 126 | - |
|
| 127 | - // Use <button> to allow full jQuery UI styling |
|
| 128 | - $actions = $actions->dataFields(); |
|
| 129 | - if ($actions) { |
|
| 130 | - /** @var FormAction $action */ |
|
| 131 | - foreach ($actions as $action) { |
|
| 132 | - $action->setUseButtonTag(true); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $this->extend('updateEditForm', $form); |
|
| 137 | - |
|
| 138 | - return $form; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Save the current sites {@link FoxyStripeSetting} into the database. |
|
| 143 | - * |
|
| 144 | - * @param $data |
|
| 145 | - * @param $form |
|
| 146 | - * |
|
| 147 | - * @return \SilverStripe\Control\HTTPResponse |
|
| 148 | - * @throws \SilverStripe\Control\HTTPResponse_Exception |
|
| 149 | - */ |
|
| 150 | - public function save_foxystripe_setting($data, $form) |
|
| 151 | - { |
|
| 152 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 153 | - $form->saveInto($config); |
|
| 154 | - try { |
|
| 155 | - $config->write(); |
|
| 156 | - } catch (ValidationException $ex) { |
|
| 157 | - $form->sessionMessage($ex->getResult()->message(), 'bad'); |
|
| 158 | - |
|
| 159 | - return $this->getResponseNegotiator()->respond($this->request); |
|
| 160 | - } |
|
| 161 | - $this->response->addHeader('X-Status', rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))); |
|
| 162 | - |
|
| 163 | - return $form->forTemplate(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param bool $unlinked |
|
| 168 | - * |
|
| 169 | - * @return ArrayList |
|
| 170 | - */ |
|
| 171 | - public function Breadcrumbs($unlinked = false) |
|
| 172 | - { |
|
| 173 | - return new ArrayList(array( |
|
| 174 | - new ArrayData(array( |
|
| 175 | - 'Title' => static::menu_title(), |
|
| 176 | - 'Link' => $this->Link(), |
|
| 177 | - )), |
|
| 178 | - )); |
|
| 179 | - } |
|
| 21 | + /** |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + private static $url_segment = 'foxystripe'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private static $url_rule = '/$Action/$ID/$OtherID'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + private static $menu_priority = 4; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private static $menu_title = 'FoxyStripe'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private static $menu_icon_class = 'font-icon-cog'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private static $tree_class = FoxyStripeSetting::class; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var array |
|
| 53 | + */ |
|
| 54 | + private static $required_permission_codes = ['EDIT_GLOBAL_PERMISSION']; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Initialises the {@link FoxyStripeSetting} controller. |
|
| 58 | + */ |
|
| 59 | + public function init() |
|
| 60 | + { |
|
| 61 | + parent::init(); |
|
| 62 | + |
|
| 63 | + Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js'); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param null $id |
|
| 68 | + * @param null $fields |
|
| 69 | + * |
|
| 70 | + * @return $this|Form |
|
| 71 | + */ |
|
| 72 | + public function getEditForm($id = null, $fields = null) |
|
| 73 | + { |
|
| 74 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 75 | + $fields = $config->getCMSFields(); |
|
| 76 | + |
|
| 77 | + // Tell the CMS what URL the preview should show |
|
| 78 | + $home = Director::absoluteBaseURL(); |
|
| 79 | + $fields->push(new HiddenField('PreviewURL', 'Preview URL', $home)); |
|
| 80 | + |
|
| 81 | + // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load |
|
| 82 | + $fields->push($navField = new LiteralField( |
|
| 83 | + 'SilverStripeNavigator', |
|
| 84 | + $this->getSilverStripeNavigator() |
|
| 85 | + )); |
|
| 86 | + $navField->setAllowHTML(true); |
|
| 87 | + |
|
| 88 | + // Retrieve validator, if one has been setup (e.g. via data extensions). |
|
| 89 | + if ($config->hasMethod('getCMSValidator')) { |
|
| 90 | + $validator = $config->getCMSValidator(); |
|
| 91 | + } else { |
|
| 92 | + $validator = null; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $actions = $config->getCMSActions(); |
|
| 96 | + $negotiator = $this->getResponseNegotiator(); |
|
| 97 | + $form = Form::create( |
|
| 98 | + $this, |
|
| 99 | + 'EditForm', |
|
| 100 | + $fields, |
|
| 101 | + $actions, |
|
| 102 | + $validator |
|
| 103 | + )->setHTMLID('Form_EditForm'); |
|
| 104 | + $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) { |
|
| 105 | + $request = $this->getRequest(); |
|
| 106 | + if ($request->isAjax() && $negotiator) { |
|
| 107 | + $result = $form->forTemplate(); |
|
| 108 | + |
|
| 109 | + return $negotiator->respond($request, array( |
|
| 110 | + 'CurrentForm' => function () use ($result) { |
|
| 111 | + return $result; |
|
| 112 | + }, |
|
| 113 | + )); |
|
| 114 | + } |
|
| 115 | + return null; |
|
| 116 | + }); |
|
| 117 | + $form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form'); |
|
| 118 | + $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
|
| 119 | + |
|
| 120 | + if ($form->Fields()->hasTabSet()) { |
|
| 121 | + $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
|
| 122 | + } |
|
| 123 | + $form->setHTMLID('Form_EditForm'); |
|
| 124 | + $form->loadDataFrom($config); |
|
| 125 | + $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
|
| 126 | + |
|
| 127 | + // Use <button> to allow full jQuery UI styling |
|
| 128 | + $actions = $actions->dataFields(); |
|
| 129 | + if ($actions) { |
|
| 130 | + /** @var FormAction $action */ |
|
| 131 | + foreach ($actions as $action) { |
|
| 132 | + $action->setUseButtonTag(true); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $this->extend('updateEditForm', $form); |
|
| 137 | + |
|
| 138 | + return $form; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Save the current sites {@link FoxyStripeSetting} into the database. |
|
| 143 | + * |
|
| 144 | + * @param $data |
|
| 145 | + * @param $form |
|
| 146 | + * |
|
| 147 | + * @return \SilverStripe\Control\HTTPResponse |
|
| 148 | + * @throws \SilverStripe\Control\HTTPResponse_Exception |
|
| 149 | + */ |
|
| 150 | + public function save_foxystripe_setting($data, $form) |
|
| 151 | + { |
|
| 152 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 153 | + $form->saveInto($config); |
|
| 154 | + try { |
|
| 155 | + $config->write(); |
|
| 156 | + } catch (ValidationException $ex) { |
|
| 157 | + $form->sessionMessage($ex->getResult()->message(), 'bad'); |
|
| 158 | + |
|
| 159 | + return $this->getResponseNegotiator()->respond($this->request); |
|
| 160 | + } |
|
| 161 | + $this->response->addHeader('X-Status', rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))); |
|
| 162 | + |
|
| 163 | + return $form->forTemplate(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param bool $unlinked |
|
| 168 | + * |
|
| 169 | + * @return ArrayList |
|
| 170 | + */ |
|
| 171 | + public function Breadcrumbs($unlinked = false) |
|
| 172 | + { |
|
| 173 | + return new ArrayList(array( |
|
| 174 | + new ArrayData(array( |
|
| 175 | + 'Title' => static::menu_title(), |
|
| 176 | + 'Link' => $this->Link(), |
|
| 177 | + )), |
|
| 178 | + )); |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -10,49 +10,49 @@ |
||
| 10 | 10 | |
| 11 | 11 | class OrderAdmin extends ModelAdmin |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - private static $managed_models = array( |
|
| 17 | - Order::class, |
|
| 18 | - ); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - private static $url_segment = 'orders'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - private static $menu_title = 'Orders'; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var int |
|
| 32 | - */ |
|
| 33 | - private static $menu_priority = 4; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @param null $id |
|
| 37 | - * @param null $fields |
|
| 38 | - * |
|
| 39 | - * @return \SilverStripe\Forms\Form |
|
| 40 | - */ |
|
| 41 | - public function getEditForm($id = null, $fields = null) |
|
| 42 | - { |
|
| 43 | - $form = parent::getEditForm($id, $fields); |
|
| 44 | - |
|
| 45 | - $gridFieldName = $this->sanitiseClassName($this->modelClass); |
|
| 46 | - /** @var GridField $gridField */ |
|
| 47 | - $gridField = $form->Fields()->fieldByName($gridFieldName); |
|
| 48 | - |
|
| 49 | - // GridField configuration |
|
| 50 | - /** @var GridFieldConfig $config */ |
|
| 51 | - $config = $gridField->getConfig(); |
|
| 52 | - |
|
| 53 | - // remove edit icon |
|
| 54 | - $config->removeComponentsByType(GridFieldEditButton::class); |
|
| 55 | - |
|
| 56 | - return $form; |
|
| 57 | - } |
|
| 13 | + /** |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + private static $managed_models = array( |
|
| 17 | + Order::class, |
|
| 18 | + ); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + private static $url_segment = 'orders'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + private static $menu_title = 'Orders'; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var int |
|
| 32 | + */ |
|
| 33 | + private static $menu_priority = 4; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @param null $id |
|
| 37 | + * @param null $fields |
|
| 38 | + * |
|
| 39 | + * @return \SilverStripe\Forms\Form |
|
| 40 | + */ |
|
| 41 | + public function getEditForm($id = null, $fields = null) |
|
| 42 | + { |
|
| 43 | + $form = parent::getEditForm($id, $fields); |
|
| 44 | + |
|
| 45 | + $gridFieldName = $this->sanitiseClassName($this->modelClass); |
|
| 46 | + /** @var GridField $gridField */ |
|
| 47 | + $gridField = $form->Fields()->fieldByName($gridFieldName); |
|
| 48 | + |
|
| 49 | + // GridField configuration |
|
| 50 | + /** @var GridFieldConfig $config */ |
|
| 51 | + $config = $gridField->getConfig(); |
|
| 52 | + |
|
| 53 | + // remove edit icon |
|
| 54 | + $config->removeComponentsByType(GridFieldEditButton::class); |
|
| 55 | + |
|
| 56 | + return $form; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -35,335 +35,335 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class ProductCategory extends DataObject |
| 37 | 37 | { |
| 38 | - /** |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - private static $db = [ |
|
| 42 | - 'Title' => 'Varchar(255)', |
|
| 43 | - 'Code' => 'Varchar(50)', |
|
| 44 | - 'DeliveryType' => 'Varchar(50)', |
|
| 45 | - 'MaxDownloads' => 'Int', |
|
| 46 | - 'MaxDownloadsTime' => 'Int', |
|
| 47 | - 'DefaultWeight' => 'Float', |
|
| 48 | - 'DefaultWeightUnit' => 'Enum("LBS, KBS", "LBS")', |
|
| 49 | - 'DefaultLengthUnit' => 'Enum("in, cm", "in")', |
|
| 50 | - 'ShippingFlatRate' => 'Currency', |
|
| 51 | - 'ShippingFlatRateType' => 'Varchar(50)', |
|
| 52 | - 'HandlingFeeType' => 'Varchar(50)', |
|
| 53 | - 'HandlingFee' => 'Currency', |
|
| 54 | - 'HandlingFeePercentage' => 'Decimal', |
|
| 55 | - 'HandlingFeeMinimum' => 'Currency', |
|
| 56 | - 'DiscountType' => 'Varchar(50)', |
|
| 57 | - 'DiscountName' => 'Varchar(50)', |
|
| 58 | - 'DiscountDetails' => 'Varchar(200)', |
|
| 59 | - 'CustomsValue' => 'Currency', |
|
| 60 | - ]; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var string |
|
| 64 | - */ |
|
| 65 | - private static $singular_name = 'FoxyCart Category'; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - private static $plural_name = 'FoxyCart Categories'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - private static $description = 'Set the FoxyCart Category on a Product'; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - private static $summary_fields = [ |
|
| 81 | - 'Title' => 'Name', |
|
| 82 | - 'Code' => 'Code', |
|
| 83 | - ]; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @var array |
|
| 87 | - */ |
|
| 88 | - private static $indexes = [ |
|
| 89 | - 'Code' => [ |
|
| 90 | - 'type' => 'unique', |
|
| 91 | - 'columns' => ['Code'], |
|
| 92 | - ], |
|
| 93 | - ]; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var string |
|
| 97 | - */ |
|
| 98 | - private static $table_name = 'ProductCategory'; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @return FieldList |
|
| 102 | - */ |
|
| 103 | - public function getCMSFields() |
|
| 104 | - { |
|
| 105 | - $fields = parent::getCMSFields(); |
|
| 106 | - |
|
| 107 | - if ($this->ID) { |
|
| 108 | - if ($this->Title == 'Default') { |
|
| 109 | - $fields->replaceField( |
|
| 110 | - 'Title', |
|
| 111 | - ReadonlyField::create('Title') |
|
| 112 | - ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $fields->replaceField( |
|
| 116 | - 'Code', |
|
| 117 | - ReadonlyField::create('Code') |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), 'DeliveryType'); |
|
| 122 | - |
|
| 123 | - $fields->replaceField( |
|
| 124 | - 'DeliveryType', |
|
| 125 | - OptionsetField::create('DeliveryType', 'Delivery Type', $this->getShippingOptions()) |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - $fields->dataFieldByName('MaxDownloads') |
|
| 129 | - ->displayIf('DeliveryType')->isEqualTo('downloaded'); |
|
| 130 | - |
|
| 131 | - $fields->dataFieldByName('MaxDownloadsTime') |
|
| 132 | - ->displayIf('DeliveryType')->isEqualTo('downloaded'); |
|
| 133 | - |
|
| 134 | - $fields->dataFieldByName('DefaultWeight') |
|
| 135 | - ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 136 | - |
|
| 137 | - $fields->dataFieldByName('DefaultWeightUnit') |
|
| 138 | - ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 139 | - |
|
| 140 | - $fields->dataFieldByName('DefaultLengthUnit') |
|
| 141 | - ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 142 | - |
|
| 143 | - $fields->dataFieldByName('ShippingFlatRate') |
|
| 144 | - ->displayIf('DeliveryType')->isEqualTo('flat_rate'); |
|
| 145 | - |
|
| 146 | - $fields->replaceField( |
|
| 147 | - 'ShippingFlatRateType', |
|
| 148 | - DropdownField::create('ShippingFlatRateType', 'Flat Rate Type', $this->getShippingFlatRateTypes()) |
|
| 149 | - ->setEmptyString('') |
|
| 150 | - ->displayIf('DeliveryType')->isEqualTo('flat_rate')->end() |
|
| 151 | - ); |
|
| 152 | - |
|
| 153 | - $fields->insertBefore(HeaderField::create('HandlingHD', 'Handling Fees and Discounts', 3), 'HandlingFeeType'); |
|
| 154 | - |
|
| 155 | - $fields->replaceField( |
|
| 156 | - 'HandlingFeeType', |
|
| 157 | - DropdownField::create('HandlingFeeType', 'Handling Fee Type', $this->getHandlingFeeTypes()) |
|
| 158 | - ->setEmptyString('') |
|
| 159 | - ->setDescription('This determines what type of Handling Fee you would like to use.') |
|
| 160 | - ); |
|
| 161 | - |
|
| 162 | - $fields->dataFieldByName('HandlingFee') |
|
| 163 | - ->displayIf('HandlingFeeType')->isNotEqualTo(''); |
|
| 164 | - |
|
| 165 | - $fields->dataFieldByName('HandlingFeeMinimum') |
|
| 166 | - ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum'); |
|
| 167 | - |
|
| 168 | - $fields->dataFieldByName('HandlingFeePercentage') |
|
| 169 | - ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum') |
|
| 170 | - ->orIf('HandlingFeeType')->isEqualTo('flat_percent'); |
|
| 171 | - |
|
| 172 | - $fields->replaceField( |
|
| 173 | - 'DiscountType', |
|
| 174 | - DropdownField::create('DiscountType', 'Discount Type', $this->getDiscountTypes()) |
|
| 175 | - ->setEmptyString('') |
|
| 176 | - ->setDescription('This determines what type of per category discount you would like to use, if any.') |
|
| 177 | - ); |
|
| 178 | - |
|
| 179 | - $fields->dataFieldByName('DiscountName') |
|
| 180 | - ->displayIf('DiscountType')->isNotEqualTo(''); |
|
| 181 | - |
|
| 182 | - $fields->dataFieldByName('DiscountDetails') |
|
| 183 | - ->displayIf('DiscountType')->isNotEqualTo(''); |
|
| 184 | - |
|
| 185 | - $fields->dataFieldByName('CustomsValue') |
|
| 186 | - ->setDescription('Enter a dollar amount here for the declared customs value for international |
|
| 38 | + /** |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + private static $db = [ |
|
| 42 | + 'Title' => 'Varchar(255)', |
|
| 43 | + 'Code' => 'Varchar(50)', |
|
| 44 | + 'DeliveryType' => 'Varchar(50)', |
|
| 45 | + 'MaxDownloads' => 'Int', |
|
| 46 | + 'MaxDownloadsTime' => 'Int', |
|
| 47 | + 'DefaultWeight' => 'Float', |
|
| 48 | + 'DefaultWeightUnit' => 'Enum("LBS, KBS", "LBS")', |
|
| 49 | + 'DefaultLengthUnit' => 'Enum("in, cm", "in")', |
|
| 50 | + 'ShippingFlatRate' => 'Currency', |
|
| 51 | + 'ShippingFlatRateType' => 'Varchar(50)', |
|
| 52 | + 'HandlingFeeType' => 'Varchar(50)', |
|
| 53 | + 'HandlingFee' => 'Currency', |
|
| 54 | + 'HandlingFeePercentage' => 'Decimal', |
|
| 55 | + 'HandlingFeeMinimum' => 'Currency', |
|
| 56 | + 'DiscountType' => 'Varchar(50)', |
|
| 57 | + 'DiscountName' => 'Varchar(50)', |
|
| 58 | + 'DiscountDetails' => 'Varchar(200)', |
|
| 59 | + 'CustomsValue' => 'Currency', |
|
| 60 | + ]; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var string |
|
| 64 | + */ |
|
| 65 | + private static $singular_name = 'FoxyCart Category'; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + private static $plural_name = 'FoxyCart Categories'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + private static $description = 'Set the FoxyCart Category on a Product'; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + private static $summary_fields = [ |
|
| 81 | + 'Title' => 'Name', |
|
| 82 | + 'Code' => 'Code', |
|
| 83 | + ]; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @var array |
|
| 87 | + */ |
|
| 88 | + private static $indexes = [ |
|
| 89 | + 'Code' => [ |
|
| 90 | + 'type' => 'unique', |
|
| 91 | + 'columns' => ['Code'], |
|
| 92 | + ], |
|
| 93 | + ]; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var string |
|
| 97 | + */ |
|
| 98 | + private static $table_name = 'ProductCategory'; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @return FieldList |
|
| 102 | + */ |
|
| 103 | + public function getCMSFields() |
|
| 104 | + { |
|
| 105 | + $fields = parent::getCMSFields(); |
|
| 106 | + |
|
| 107 | + if ($this->ID) { |
|
| 108 | + if ($this->Title == 'Default') { |
|
| 109 | + $fields->replaceField( |
|
| 110 | + 'Title', |
|
| 111 | + ReadonlyField::create('Title') |
|
| 112 | + ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $fields->replaceField( |
|
| 116 | + 'Code', |
|
| 117 | + ReadonlyField::create('Code') |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), 'DeliveryType'); |
|
| 122 | + |
|
| 123 | + $fields->replaceField( |
|
| 124 | + 'DeliveryType', |
|
| 125 | + OptionsetField::create('DeliveryType', 'Delivery Type', $this->getShippingOptions()) |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + $fields->dataFieldByName('MaxDownloads') |
|
| 129 | + ->displayIf('DeliveryType')->isEqualTo('downloaded'); |
|
| 130 | + |
|
| 131 | + $fields->dataFieldByName('MaxDownloadsTime') |
|
| 132 | + ->displayIf('DeliveryType')->isEqualTo('downloaded'); |
|
| 133 | + |
|
| 134 | + $fields->dataFieldByName('DefaultWeight') |
|
| 135 | + ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 136 | + |
|
| 137 | + $fields->dataFieldByName('DefaultWeightUnit') |
|
| 138 | + ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 139 | + |
|
| 140 | + $fields->dataFieldByName('DefaultLengthUnit') |
|
| 141 | + ->displayIf('DeliveryType')->isEqualTo('shipped'); |
|
| 142 | + |
|
| 143 | + $fields->dataFieldByName('ShippingFlatRate') |
|
| 144 | + ->displayIf('DeliveryType')->isEqualTo('flat_rate'); |
|
| 145 | + |
|
| 146 | + $fields->replaceField( |
|
| 147 | + 'ShippingFlatRateType', |
|
| 148 | + DropdownField::create('ShippingFlatRateType', 'Flat Rate Type', $this->getShippingFlatRateTypes()) |
|
| 149 | + ->setEmptyString('') |
|
| 150 | + ->displayIf('DeliveryType')->isEqualTo('flat_rate')->end() |
|
| 151 | + ); |
|
| 152 | + |
|
| 153 | + $fields->insertBefore(HeaderField::create('HandlingHD', 'Handling Fees and Discounts', 3), 'HandlingFeeType'); |
|
| 154 | + |
|
| 155 | + $fields->replaceField( |
|
| 156 | + 'HandlingFeeType', |
|
| 157 | + DropdownField::create('HandlingFeeType', 'Handling Fee Type', $this->getHandlingFeeTypes()) |
|
| 158 | + ->setEmptyString('') |
|
| 159 | + ->setDescription('This determines what type of Handling Fee you would like to use.') |
|
| 160 | + ); |
|
| 161 | + |
|
| 162 | + $fields->dataFieldByName('HandlingFee') |
|
| 163 | + ->displayIf('HandlingFeeType')->isNotEqualTo(''); |
|
| 164 | + |
|
| 165 | + $fields->dataFieldByName('HandlingFeeMinimum') |
|
| 166 | + ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum'); |
|
| 167 | + |
|
| 168 | + $fields->dataFieldByName('HandlingFeePercentage') |
|
| 169 | + ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum') |
|
| 170 | + ->orIf('HandlingFeeType')->isEqualTo('flat_percent'); |
|
| 171 | + |
|
| 172 | + $fields->replaceField( |
|
| 173 | + 'DiscountType', |
|
| 174 | + DropdownField::create('DiscountType', 'Discount Type', $this->getDiscountTypes()) |
|
| 175 | + ->setEmptyString('') |
|
| 176 | + ->setDescription('This determines what type of per category discount you would like to use, if any.') |
|
| 177 | + ); |
|
| 178 | + |
|
| 179 | + $fields->dataFieldByName('DiscountName') |
|
| 180 | + ->displayIf('DiscountType')->isNotEqualTo(''); |
|
| 181 | + |
|
| 182 | + $fields->dataFieldByName('DiscountDetails') |
|
| 183 | + ->displayIf('DiscountType')->isNotEqualTo(''); |
|
| 184 | + |
|
| 185 | + $fields->dataFieldByName('CustomsValue') |
|
| 186 | + ->setDescription('Enter a dollar amount here for the declared customs value for international |
|
| 187 | 187 | shipments. If you leave this blank, the sale price of the item will be used.'); |
| 188 | 188 | |
| 189 | - return $fields; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @return \SilverStripe\ORM\ValidationResult |
|
| 194 | - */ |
|
| 195 | - public function validate() |
|
| 196 | - { |
|
| 197 | - $result = parent::validate(); |
|
| 198 | - |
|
| 199 | - if (ProductCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) { |
|
| 200 | - $result->addError('Code must be unique for each category.'); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $result; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 208 | - */ |
|
| 209 | - public function requireDefaultRecords() |
|
| 210 | - { |
|
| 211 | - parent::requireDefaultRecords(); |
|
| 212 | - $allCats = self::get(); |
|
| 213 | - if (!$allCats->count()) { |
|
| 214 | - $cat = new self(); |
|
| 215 | - $cat->Title = 'Default'; |
|
| 216 | - $cat->Code = 'DEFAULT'; |
|
| 217 | - $cat->write(); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param bool $member |
|
| 223 | - * |
|
| 224 | - * @return bool |
|
| 225 | - */ |
|
| 226 | - public function canView($member = false) |
|
| 227 | - { |
|
| 228 | - return true; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @param null $member |
|
| 233 | - * |
|
| 234 | - * @return bool|int |
|
| 235 | - */ |
|
| 236 | - public function canEdit($member = null) |
|
| 237 | - { |
|
| 238 | - return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @param null $member |
|
| 243 | - * |
|
| 244 | - * @return bool|int |
|
| 245 | - */ |
|
| 246 | - public function canDelete($member = null) |
|
| 247 | - { |
|
| 248 | - |
|
| 249 | - //don't allow deletion of DEFAULT category |
|
| 250 | - return ($this->Code == 'DEFAULT') ? false : Permission::check('Product_CANCRUD', 'any', $member); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * @param null $member |
|
| 255 | - * |
|
| 256 | - * @param array $context |
|
| 257 | - * |
|
| 258 | - * @return bool|int |
|
| 259 | - */ |
|
| 260 | - public function canCreate($member = null, $context = []) |
|
| 261 | - { |
|
| 262 | - return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @return array |
|
| 267 | - */ |
|
| 268 | - public function getShippingOptions() |
|
| 269 | - { |
|
| 270 | - return [ |
|
| 271 | - 'shipped' => 'Shipped using live shipping rates', |
|
| 272 | - 'downloaded' => 'Downloaded by the customer', |
|
| 273 | - 'flat_rate' => 'Shipped using a flat rate fee', |
|
| 274 | - 'pickup' => 'Picked up by the customer', |
|
| 275 | - 'notshipped' => 'No Shipping', |
|
| 276 | - ]; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @return array |
|
| 281 | - */ |
|
| 282 | - public function getShippingFlatRateTypes() |
|
| 283 | - { |
|
| 284 | - return [ |
|
| 285 | - 'per_order' => 'Charge per order', |
|
| 286 | - 'per_item' => 'Charge per item', |
|
| 287 | - ]; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @return array |
|
| 292 | - */ |
|
| 293 | - public function getHandlingFeeTypes() |
|
| 294 | - { |
|
| 295 | - return [ |
|
| 296 | - 'flat_per_order' => 'Flat fee per order with products in this category', |
|
| 297 | - 'flat_per_item' => 'Flat fee per product in this category', |
|
| 298 | - 'flat_percent' => 'Flat fee per shipment + % of price for products in this category', |
|
| 299 | - 'flat_percent_with_minimum' => 'Flat fee per shipment OR % of order total with products in this category. |
|
| 189 | + return $fields; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @return \SilverStripe\ORM\ValidationResult |
|
| 194 | + */ |
|
| 195 | + public function validate() |
|
| 196 | + { |
|
| 197 | + $result = parent::validate(); |
|
| 198 | + |
|
| 199 | + if (ProductCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) { |
|
| 200 | + $result->addError('Code must be unique for each category.'); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $result; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 208 | + */ |
|
| 209 | + public function requireDefaultRecords() |
|
| 210 | + { |
|
| 211 | + parent::requireDefaultRecords(); |
|
| 212 | + $allCats = self::get(); |
|
| 213 | + if (!$allCats->count()) { |
|
| 214 | + $cat = new self(); |
|
| 215 | + $cat->Title = 'Default'; |
|
| 216 | + $cat->Code = 'DEFAULT'; |
|
| 217 | + $cat->write(); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param bool $member |
|
| 223 | + * |
|
| 224 | + * @return bool |
|
| 225 | + */ |
|
| 226 | + public function canView($member = false) |
|
| 227 | + { |
|
| 228 | + return true; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @param null $member |
|
| 233 | + * |
|
| 234 | + * @return bool|int |
|
| 235 | + */ |
|
| 236 | + public function canEdit($member = null) |
|
| 237 | + { |
|
| 238 | + return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @param null $member |
|
| 243 | + * |
|
| 244 | + * @return bool|int |
|
| 245 | + */ |
|
| 246 | + public function canDelete($member = null) |
|
| 247 | + { |
|
| 248 | + |
|
| 249 | + //don't allow deletion of DEFAULT category |
|
| 250 | + return ($this->Code == 'DEFAULT') ? false : Permission::check('Product_CANCRUD', 'any', $member); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * @param null $member |
|
| 255 | + * |
|
| 256 | + * @param array $context |
|
| 257 | + * |
|
| 258 | + * @return bool|int |
|
| 259 | + */ |
|
| 260 | + public function canCreate($member = null, $context = []) |
|
| 261 | + { |
|
| 262 | + return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @return array |
|
| 267 | + */ |
|
| 268 | + public function getShippingOptions() |
|
| 269 | + { |
|
| 270 | + return [ |
|
| 271 | + 'shipped' => 'Shipped using live shipping rates', |
|
| 272 | + 'downloaded' => 'Downloaded by the customer', |
|
| 273 | + 'flat_rate' => 'Shipped using a flat rate fee', |
|
| 274 | + 'pickup' => 'Picked up by the customer', |
|
| 275 | + 'notshipped' => 'No Shipping', |
|
| 276 | + ]; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @return array |
|
| 281 | + */ |
|
| 282 | + public function getShippingFlatRateTypes() |
|
| 283 | + { |
|
| 284 | + return [ |
|
| 285 | + 'per_order' => 'Charge per order', |
|
| 286 | + 'per_item' => 'Charge per item', |
|
| 287 | + ]; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @return array |
|
| 292 | + */ |
|
| 293 | + public function getHandlingFeeTypes() |
|
| 294 | + { |
|
| 295 | + return [ |
|
| 296 | + 'flat_per_order' => 'Flat fee per order with products in this category', |
|
| 297 | + 'flat_per_item' => 'Flat fee per product in this category', |
|
| 298 | + 'flat_percent' => 'Flat fee per shipment + % of price for products in this category', |
|
| 299 | + 'flat_percent_with_minimum' => 'Flat fee per shipment OR % of order total with products in this category. |
|
| 300 | 300 | Whichever is greater.', |
| 301 | - ]; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @return array |
|
| 306 | - */ |
|
| 307 | - public function getDiscountTypes() |
|
| 308 | - { |
|
| 309 | - return [ |
|
| 310 | - 'quantity_amount' => 'Discount by an amount based on the quantity', |
|
| 311 | - 'quantity_percentage' => 'Discount by a percentage based on the quantity', |
|
| 312 | - 'price_amount' => 'Discount by an amount based on the price in this category', |
|
| 313 | - 'price_percentage' => 'Discount by a percentage based on the price in this category', |
|
| 314 | - ]; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @return array |
|
| 319 | - */ |
|
| 320 | - public function getDataMap() |
|
| 321 | - { |
|
| 322 | - return [ |
|
| 323 | - 'name' => $this->Title, |
|
| 324 | - 'code' => $this->Code, |
|
| 325 | - 'item_delivery_type' => $this->DeliveryType, |
|
| 326 | - 'max_downloads_per_customer' => $this->MaxDownloads, |
|
| 327 | - 'max_downloads_time_period' => $this->MaxDownloadsTime, |
|
| 328 | - 'customs_value' => $this->CustomsValue, |
|
| 329 | - 'default_weight' => $this->DefaultWeight, |
|
| 330 | - 'default_weight_unit' => $this->DefaultWeightUnit, |
|
| 331 | - 'default_length_unit' => $this->DefautlLengthUnit, |
|
| 332 | - 'shipping_flat_rate' => $this->ShippingFlatRate, |
|
| 333 | - 'shipping_flat_rate_type' => $this->ShippingFlatRateType, |
|
| 334 | - 'handling_fee_type' => $this->HandlingFeeType, |
|
| 335 | - 'handling_fee' => $this->HandlingFee, |
|
| 336 | - 'handling_fee_minimum' => $this->HandlingFeeMinimum, |
|
| 337 | - 'handling_fee_percentage' => $this->HandlingFeePercentage, |
|
| 338 | - 'discount_type' => $this->DiscountType, |
|
| 339 | - 'discount_name' => $this->DiscountName, |
|
| 340 | - 'discount_details' => $this->DiscountDetails, |
|
| 341 | - ]; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 346 | - */ |
|
| 347 | - public function onAfterWrite() |
|
| 348 | - { |
|
| 349 | - parent::onAfterWrite(); |
|
| 350 | - |
|
| 351 | - if ($this->isChanged()) { |
|
| 352 | - if ($fc = new FoxyStripeClient()) { |
|
| 353 | - $fc->putCategory($this->getDataMap()); |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 360 | - */ |
|
| 361 | - public function onAfterDelete() |
|
| 362 | - { |
|
| 363 | - parent::onAfterDelete(); |
|
| 364 | - |
|
| 365 | - if ($fc = new FoxyStripeClient()) { |
|
| 366 | - $fc->deleteCategory($this->getDataMap()); |
|
| 367 | - } |
|
| 368 | - } |
|
| 301 | + ]; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @return array |
|
| 306 | + */ |
|
| 307 | + public function getDiscountTypes() |
|
| 308 | + { |
|
| 309 | + return [ |
|
| 310 | + 'quantity_amount' => 'Discount by an amount based on the quantity', |
|
| 311 | + 'quantity_percentage' => 'Discount by a percentage based on the quantity', |
|
| 312 | + 'price_amount' => 'Discount by an amount based on the price in this category', |
|
| 313 | + 'price_percentage' => 'Discount by a percentage based on the price in this category', |
|
| 314 | + ]; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @return array |
|
| 319 | + */ |
|
| 320 | + public function getDataMap() |
|
| 321 | + { |
|
| 322 | + return [ |
|
| 323 | + 'name' => $this->Title, |
|
| 324 | + 'code' => $this->Code, |
|
| 325 | + 'item_delivery_type' => $this->DeliveryType, |
|
| 326 | + 'max_downloads_per_customer' => $this->MaxDownloads, |
|
| 327 | + 'max_downloads_time_period' => $this->MaxDownloadsTime, |
|
| 328 | + 'customs_value' => $this->CustomsValue, |
|
| 329 | + 'default_weight' => $this->DefaultWeight, |
|
| 330 | + 'default_weight_unit' => $this->DefaultWeightUnit, |
|
| 331 | + 'default_length_unit' => $this->DefautlLengthUnit, |
|
| 332 | + 'shipping_flat_rate' => $this->ShippingFlatRate, |
|
| 333 | + 'shipping_flat_rate_type' => $this->ShippingFlatRateType, |
|
| 334 | + 'handling_fee_type' => $this->HandlingFeeType, |
|
| 335 | + 'handling_fee' => $this->HandlingFee, |
|
| 336 | + 'handling_fee_minimum' => $this->HandlingFeeMinimum, |
|
| 337 | + 'handling_fee_percentage' => $this->HandlingFeePercentage, |
|
| 338 | + 'discount_type' => $this->DiscountType, |
|
| 339 | + 'discount_name' => $this->DiscountName, |
|
| 340 | + 'discount_details' => $this->DiscountDetails, |
|
| 341 | + ]; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 346 | + */ |
|
| 347 | + public function onAfterWrite() |
|
| 348 | + { |
|
| 349 | + parent::onAfterWrite(); |
|
| 350 | + |
|
| 351 | + if ($this->isChanged()) { |
|
| 352 | + if ($fc = new FoxyStripeClient()) { |
|
| 353 | + $fc->putCategory($this->getDataMap()); |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 360 | + */ |
|
| 361 | + public function onAfterDelete() |
|
| 362 | + { |
|
| 363 | + parent::onAfterDelete(); |
|
| 364 | + |
|
| 365 | + if ($fc = new FoxyStripeClient()) { |
|
| 366 | + $fc->deleteCategory($this->getDataMap()); |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | 369 | } |