Passed
Pull Request — master (#373)
by Nic
07:47 queued 03:58
created

FoxyStripeController::orderDetailFromProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Dynamic\FoxyStripe\Controller;
4
5
use Dynamic\FoxyStripe\Model\FoxyCart;
6
use Dynamic\FoxyStripe\Model\FoxyStripeClient;
7
use Dynamic\FoxyStripe\Model\FoxyStripeSetting;
8
use Dynamic\FoxyStripe\Model\OptionItem;
9
use Dynamic\FoxyStripe\Model\Order;
10
use Dynamic\FoxyStripe\Model\OrderDetail;
11
use Dynamic\FoxyStripe\Page\ProductPage;
12
use SilverStripe\Control\HTTPRequest;
13
use SilverStripe\ORM\Queries\SQLUpdate;
14
use SilverStripe\Security\Member;
15
use SilverStripe\Security\Security;
16
17
class FoxyStripeController extends \PageController
18
{
19
    /**
20
     *
21
     */
22
    const URLSEGMENT = 'foxystripe';
23
24
    /**
25
     * @var array
26
     */
27
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
28
        'index',
29
        'sso',
30
    ];
31
32
    /**
33
     * @return string
34
     */
35
    public function getURLSegment()
36
    {
37
        return self::URLSEGMENT;
38
    }
39
40
    /**
41
     * @return string
42
     *
43
     * @throws \SilverStripe\ORM\ValidationException
44
     */
45
    public function index()
46
    {
47
        $request = $this->getRequest();
48
49
        if ($request->postVar('FoxyData') || $request->postVar('FoxySubscriptionData')) {
50
            $this->processFoxyRequest($request);
51
52
            return 'foxy';
53
        }
54
55
        return 'No FoxyData or FoxySubscriptionData received.';
56
    }
57
58
    /**
59
     * Process a request after a transaction is completed via Foxy
60
     *
61
     * @param HTTPRequest $request
62
     */
63
    protected function processFoxyRequest(HTTPRequest $request)
64
    {
65
        $encryptedData = $request->postVar('FoxyData') ? urldecode($request->postVar('FoxyData')) : urldecode($request->postVar('FoxySubscriptionData'));
66
        $decryptedData = $this->decryptFeedData($encryptedData);
67
68
        $this->parseFeedData($encryptedData, $decryptedData);
69
70
        $this->extend('addIntegrations', $encryptedData);
71
    }
72
73
    /**
74
     * Decrypt the XML data feed from Foxy
75
     *
76
     * @param $data
77
     * @return string
78
     * @throws \SilverStripe\ORM\ValidationException
79
     */
80
    private function decryptFeedData($data)
81
    {
82
        return \rc4crypt::decrypt(FoxyCart::getStoreKey(), $data);
0 ignored issues
show
Bug introduced by
It seems like Dynamic\FoxyStripe\Model\FoxyCart::getStoreKey() can also be of type false; however, parameter $pwd of rc4crypt::decrypt() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        return \rc4crypt::decrypt(/** @scrutinizer ignore-type */ FoxyCart::getStoreKey(), $data);
Loading history...
83
    }
84
85
    /**
86
     * Parse the XML data feed from Foxy to a SimpleXMLElement object
87
     *
88
     * @param $encrypted
89
     * @param $decrypted
90
     *
91
     * @throws \SilverStripe\ORM\ValidationException
92
     */
93
    private function parseFeedData($encryptedData, $decryptedData)
94
    {
95
        $orders = new \SimpleXMLElement($decryptedData);
96
97
        // loop over each transaction to find FoxyCart Order ID
98
        foreach ($orders->transactions->transaction as $transaction) {
99
            $this->processTransaction($transaction, $encryptedData);
100
        }
101
    }
102
103
    /**
104
     * @param $transaction
105
     * @return bool
106
     * @throws \SilverStripe\ORM\ValidationException
107
     */
108
    private function processTransaction($transaction, $encryptedData)
109
    {
110
        if (!isset($transaction->id)) {
111
            return false;
112
        }
113
114
        if (!$order = Order::get()->filter('Order_ID', (int)$transaction->id)->first()) {
115
            $order = Order::create();
116
            $order->Order_ID = (int)$transaction->id;
117
            $order->Response = urlencode($encryptedData);
118
        }
119
120
        $order->write();
121
    }
122
123
    /**
124
     * Single Sign on integration with FoxyCart.
125
     */
126
    public function sso()
127
    {
128
        // GET variables from FoxyCart Request
129
        $fcsid = $this->request->getVar('fcsid');
130
        $timestampNew = strtotime('+30 days');
131
132
        // get current member if logged in. If not, create a 'fake' user with Customer_ID = 0
133
        // fake user will redirect to FC checkout, ask customer to log in
134
        // to do: consider a login/registration form here if not logged in
135
        if (!$Member = Security::getCurrentUser()) {
136
            $Member = new Member();
137
            $Member->Customer_ID = 0;
138
        }
139
140
        $auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . FoxyCart::getStoreKey());
0 ignored issues
show
Bug introduced by
Are you sure Dynamic\FoxyStripe\Model\FoxyCart::getStoreKey() of type SilverStripe\ORM\FieldType\DBVarchar|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
        $auth_token = sha1($Member->Customer_ID . '|' . $timestampNew . '|' . /** @scrutinizer ignore-type */ FoxyCart::getStoreKey());
Loading history...
141
142
        $config = FoxyStripeSetting::current_foxystripe_setting();
143
        if ($config->CustomSSL) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomSSL does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
144
            $link = FoxyCart::getFoxyCartStoreName();
145
        } else {
146
            $link = FoxyCart::getFoxyCartStoreName() . '.foxycart.com';
147
        }
148
149
        $params = [
150
            'fc_auth_token' => $auth_token,
151
            'fcsid' => $fcsid,
152
            'fc_customer_id' => $Member->Customer_ID,
153
            'timestamp' => $timestampNew,
154
        ];
155
156
        $httpQuery = http_build_query($params);
157
158
        $this->redirect("https://{$link}/checkout?$httpQuery");
159
    }
160
}
161