Completed
Pull Request — master (#4)
by Jason
05:20 queued 03:17
created

FoxyController::sso()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Controller;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\Order;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\ORM\ValidationException;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Security\Security;
12
13
class FoxyController extends Controller
14
{
15
    /**
16
     *
17
     */
18
    const URLSEGMENT = 'foxy';
19
20
    /**
21
     * @var array
22
     */
23
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'index',
25
    ];
26
27
    /**
28
     * @return string
29
     */
30
    public function getURLSegment()
31
    {
32
        return self::URLSEGMENT;
33
    }
34
35
    /**
36
     * @return string
37
     * @throws ValidationException
38
     */
39
    public function index()
40
    {
41
        $request = $this->getRequest();
42
        if ($request->postVar('FoxyData') || $request->postVar('FoxySubscriptionData')) {
43
            $this->processFoxyRequest($request);
44
            return 'foxy';
45
        }
46
        return 'No FoxyData or FoxySubscriptionData received.';
47
    }
48
49
    /**
50
     * Process a request after a transaction is completed via Foxy
51
     *
52
     * @param HTTPRequest $request
53
     * @throws ValidationException
54
     */
55
    protected function processFoxyRequest(HTTPRequest $request)
56
    {
57
        $encryptedData = $request->postVar('FoxyData')
58
            ? urldecode($request->postVar('FoxyData'))
59
            : urldecode($request->postVar('FoxySubscriptionData'));
60
        $decryptedData = $this->decryptFeedData($encryptedData);
61
        $this->parseFeedData($encryptedData, $decryptedData);
62
63
        $this->extend('addIntegrations', $encryptedData);
64
    }
65
66
    /**
67
     * Decrypt the XML data feed from Foxy
68
     *
69
     * @param $data
70
     * @return string
71
     * @throws \SilverStripe\ORM\ValidationException
72
     */
73
    private function decryptFeedData($data)
74
    {
75
        $helper = FoxyHelper::create();
76
        return \rc4crypt::decrypt($helper->config()->get('secret'), $data);
77
    }
78
79
    /**
80
     * Parse the XML data feed from Foxy to a SimpleXMLElement object
81
     *
82
     * @param $encryptedData
83
     * @param $decryptedData
84
     * @throws ValidationException
85
     */
86
    private function parseFeedData($encryptedData, $decryptedData)
87
    {
88
        $orders = new \SimpleXMLElement($decryptedData);
89
        // loop over each transaction to find FoxyCart Order ID
90
        foreach ($orders->transactions->transaction as $transaction) {
91
            $this->processTransaction($transaction, $encryptedData);
92
        }
93
    }
94
95
    /**
96
     * @param $transaction
97
     * @return bool
98
     * @throws ValidationException
99
     */
100
    private function processTransaction($transaction, $encryptedData)
101
    {
102
        if (!isset($transaction->id)) {
103
            return false;
104
        }
105
        if (!$order = Order::get()->filter('OrderID', (int)$transaction->id)->first()) {
106
            $order = Order::create();
107
            $order->OrderID = (int)$transaction->id;
108
            $order->Response = urlencode($encryptedData);
109
        }
110
        $order->write();
111
    }
112
}
113