Source of file Transaction.php
Size: 8,306 Bytes - Last Modified: 2021-12-24T06:46:12+00:00
/var/www/docs.ssmods.com/process/src/src/Foxy/Transaction.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 | <?php namespace Dynamic\Foxy\Parser\Foxy; use Dynamic\Foxy\Model\FoxyHelper; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ValidationException; use SilverStripe\View\ArrayData; /** * Class Transaction * @package Dynamic\Foxy\Parser\Foxy */ class Transaction { use Injectable; use Configurable; /** * @var string Encrypted response from Foxy */ private $encrypted_data; /** * @var "Foxy.io transaction xml record" */ private $transaction; /** * @var ArrayData */ private $transaction_data; /** * @var ArrayList */ private $discount_data; /** * @var ArrayList */ private $product_data; /** * @var ArrayList */ private $custom_fields_data; /** * Transaction constructor. * @param $data string encrypted foxy response data from the xml data feed * @throws ValidationException */ public function __construct($data) { $this->setEncryptedData($data); $this->setTransaction($data); } /** * @param $data * @return $this */ public function setEncryptedData($data) { $this->encrypted_data = $data; return $this; } /** * @return string */ public function getEncryptedData() { return $this->encrypted_data; } /** * Set the decrypted transaction data to use. * * @param $data * @return $this * @throws ValidationException */ public function setTransaction($data) { $decryptedData = $this->getDecryptedData($data); foreach ($decryptedData->transactions->transaction as $transaction) { if ($this->hasID($transaction)) { $this->transaction = $transaction; break; } } if (!$this->transaction) { $this->transaction = false; } return $this; } /** * Check if there is a transaction ID for the given decrypted transaction data. * * @param $transaction * @return bool */ protected function hasID($transaction) { return (int)$transaction->id > 0; } /** * Return the decrypted transaction xml data. * * @return mixed */ protected function getTransaction() { return $this->transaction; } /** * Return an ArrayData containing transaction, products and discounts data. * * @return ArrayData */ public function getParsedTransactionData() { /** * */ return ArrayData::create([ 'transaction' => $this->getTransactionData(), 'products' => $this->getProductData(), 'discounts' => $this->getDiscountData(), 'custom_fields' => $this->getCustomFieldsData(), ]); } /** * Set the base transaction data in an ArrayData object. * * @return $this */ private function setTransactionData() { $this->transaction_data = $this->getObject( $this->getTransaction(), $this->config()->get('transaction_mapping') ); return $this; } /** * Return base transaction data. * * @return ArrayData */ protected function getTransactionData() { if (!$this->transaction_data instanceof ArrayData) { $this->setTransactionData(); } return $this->transaction_data; } /** * Return discounts data from Foxy.io xml data feed if any. * * @return ArrayList */ protected function getDiscountData() { if (!$this->discount_data instanceof ArrayList) { $this->setDiscountData(); } return $this->discount_data; } /** * Set an ArrayList with possible discount data as ArrayData objects. * * @return $this */ protected function setDiscountData() { $discounts = $this->getTransaction()->discounts; $discountsList = ArrayList::create(); foreach ($discounts as $discount) { $discountsList->push($this->getObject($discount->discount, $this->config()->get('discounts_mapping'))); } $this->discount_data = $discountsList; return $this; } /** * Return transaction_details data from Foxy.io xml data feed if any. * * @return ArrayList */ protected function getProductData() { if (!$this->product_data instanceof ArrayList) { $this->setProductData(); } return $this->product_data; } /** * Set an ArrayList with possible transaction_detail data as ArrayData objects. * * @return $this */ protected function setProductData() { $details = $this->getTransaction()->transaction_details->transaction_detail; $detailsList = ArrayList::create(); foreach ($details as $detail) { $product = $this->getObject($detail, $this->config()->get('transaction_detail_mapping')); $product->transaction_detail_options = $this->getProductOptions($detail->transaction_detail_options->transaction_detail_option); $detailsList->push($product); } $this->product_data = $detailsList; return $this; } /** * Returns an ArrayList containing possible transaction_detail_option data as ArrayData objects. * * @param $data * @return ArrayList */ protected function getProductOptions($data) { $options = ArrayList::create(); foreach ($data as $option) { $options->push($this->getObject($option, $this->config()->get('transaction_detail_option_mapping'))); } return $options; } /** * @return ArrayList */ protected function getCustomFieldsData() { if (!$this->custom_fields_data instanceof ArrayList) { $this->setCustomFieldsData(); } return $this->custom_fields_data; } /** * @return $this */ protected function setCustomFieldsData() { $customFields = $this->getTransaction()->custom_fields; $customFieldsList = ArrayList::create(); foreach ($customFields as $customFieldArray) { foreach ($customFieldArray as $customField) { $customFieldsList->push($this->getObject( $customField, $this->config()->get('custom_fields_mapping') )); } } $this->custom_fields_data = $customFieldsList; return $this; } /** * Returns an ArrayData object based on the given iterable data and a key/val config array. Used * to type hint data from the Foxy.io xml data feed. * * @param $data * @param array $config * @return ArrayData */ protected function getObject($data, $config = []) { $dataArray = []; foreach ($config as $name => $type) { switch ($type) { case 'int': $dataArray[$name] = (int)$data->{$name}; break; case 'float': $dataArray[$name] = (float)$data->{$name}; break; case 'bool': case 'boolean': $dataArray[$name] = (bool)$data->{$name}; break; case 'string': default: $dataArray[$name] = (string)$data->{$name}; break; } } return ArrayData::create($dataArray); } /** * @return bool */ public function exists() { return $this->getTransaction() != false && !empty($this->getTransaction()); } /** * @param $data * @return \SimpleXMLElement * @throws \SilverStripe\ORM\ValidationException */ private function getDecryptedData($data) { $helper = new FoxyHelper(); return new \SimpleXMLElement(\rc4crypt::decrypt($helper->config()->get('secret'), $data)); } } |