dynamic /
foxystripe
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Dynamic\FoxyStripe\ORM; |
||
| 4 | |||
| 5 | use Dynamic\FoxyStripe\Model\FoxyCart; |
||
| 6 | use Dynamic\FoxyStripe\Model\Order; |
||
| 7 | use SilverStripe\ORM\DataExtension; |
||
| 8 | use SilverStripe\Security\Member; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class CustomerExtension |
||
| 12 | * @package Dynamic\FoxyStripe\ORM |
||
| 13 | * |
||
| 14 | * @property Member $owner |
||
| 15 | * @property \SilverStripe\ORM\FieldType\DBInt Customer_ID |
||
| 16 | */ |
||
| 17 | class CustomerExtension extends DataExtension |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private static $db = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 23 | 'Customer_ID' => 'Int', |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $has_many = [ |
||
|
0 ignored issues
–
show
|
|||
| 30 | 'Orders' => Order::class, |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private static $indexes = [ |
||
|
0 ignored issues
–
show
|
|||
| 37 | 'Customer_ID' => true, // make unique |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 42 | */ |
||
| 43 | 49 | public function onBeforeWrite() |
|
| 44 | { |
||
| 45 | 49 | parent::onBeforeWrite(); |
|
| 46 | |||
| 47 | // if Member data was imported from FoxyCart, PasswordEncryption will be set to 'none'. |
||
| 48 | // Change to sh1_v2.4 to ensure SilverStripe is using the same hash as FoxyCart API 1.1 |
||
| 49 | 49 | if (!$this->owner->PasswordEncryption && ( |
|
| 50 | 49 | $this->owner->PasswordEncryption == null || $this->owner->PasswordEncryption == 'none' |
|
| 51 | )) { |
||
| 52 | $this->owner->PasswordEncryption = 'sha1_v2.4'; |
||
| 53 | } |
||
| 54 | |||
| 55 | // Send updated customer data to Foxy Cart via API |
||
| 56 | 49 | $response = FoxyCart::putCustomer($this->owner); |
|
| 57 | |||
| 58 | // Grab customer_id record from FoxyCart response, store in Member |
||
| 59 | 49 | if ($response) { |
|
| 60 | $foxyResponse = new \SimpleXMLElement($response); |
||
| 61 | $this->owner->Customer_ID = (int)$foxyResponse->customer_id; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 |