|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyCart; |
|
6
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
|
7
|
|
|
use SilverStripe\Security\Member; |
|
8
|
|
|
|
|
9
|
|
|
class FoxyStripeController extends \PageController |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
const URLSEGMENT = 'foxystripe'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
public function getURLSegment() |
|
20
|
|
|
{ |
|
21
|
|
|
return self::URLSEGMENT; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
28
|
|
|
'index', |
|
29
|
|
|
'sso', |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return string |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function index() |
|
38
|
|
|
{ |
|
39
|
|
|
// handle POST from FoxyCart API transaction |
|
40
|
|
|
if ((isset($_POST['FoxyData']) or isset($_POST['FoxySubscriptionData']))) { |
|
|
|
|
|
|
41
|
|
|
$FoxyData_encrypted = (isset($_POST['FoxyData'])) ? |
|
42
|
|
|
urldecode($_POST['FoxyData']) : |
|
43
|
|
|
urldecode($_POST['FoxySubscriptionData']); |
|
44
|
|
|
$FoxyData_decrypted = \rc4crypt::decrypt(FoxyCart::getStoreKey(), $FoxyData_encrypted); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
// parse the response and save the order |
|
47
|
|
|
self::handleDataFeed($FoxyData_encrypted, $FoxyData_decrypted); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
// extend to allow for additional integrations with Datafeed |
|
50
|
|
|
$this->extend('addIntegrations', $FoxyData_encrypted); |
|
51
|
|
|
|
|
52
|
|
|
return 'foxy'; |
|
53
|
|
|
} else { |
|
54
|
|
|
return 'No FoxyData or FoxySubscriptionData received.'; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $encrypted |
|
60
|
|
|
* @param $decrypted |
|
61
|
|
|
* |
|
62
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handleDataFeed($encrypted, $decrypted) |
|
65
|
|
|
{ |
|
66
|
|
|
$orders = new \SimpleXMLElement($decrypted); |
|
67
|
|
|
|
|
68
|
|
|
// loop over each transaction to find FoxyCart Order ID |
|
69
|
|
|
foreach ($orders->transactions->transaction as $transaction) { |
|
70
|
|
|
// if FoxyCart order id, then parse order |
|
71
|
|
|
if (isset($transaction->id)) { |
|
72
|
|
|
($order = Order::get()->filter('Order_ID', (int) $transaction->id)->First()) ? |
|
|
|
|
|
|
73
|
|
|
$order = Order::get()->filter('Order_ID', (int) $transaction->id)->First() : |
|
74
|
|
|
$order = Order::create(); |
|
75
|
|
|
|
|
76
|
|
|
// save base order info |
|
77
|
|
|
$order->Order_ID = (int) $transaction->id; |
|
78
|
|
|
$order->Response = urlencode($encrypted); |
|
79
|
|
|
$order->write(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Single Sign on integration with FoxyCart. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function sso() |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
// GET variables from FoxyCart Request |
|
91
|
|
|
$fcsid = $this->request->getVar('fcsid'); |
|
92
|
|
|
$timestampNew = strtotime('+30 days'); |
|
93
|
|
|
|
|
94
|
|
|
// get current member if logged in. If not, create a 'fake' user with Customer_ID = 0 |
|
95
|
|
|
// fake user will redirect to FC checkout, ask customer to log in |
|
96
|
|
|
// to do: consider a login/registration form here if not logged in |
|
97
|
|
|
if ($Member = Member::currentUser()) { |
|
|
|
|
|
|
98
|
|
|
$Member = Member::currentUser(); |
|
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
$Member = new Member(); |
|
101
|
|
|
$Member->Customer_ID = 0; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$auth_token = sha1($Member->Customer_ID.'|'.$timestampNew.'|'.FoxyCart::getStoreKey()); |
|
105
|
|
|
|
|
106
|
|
|
$redirect_complete = 'https://'.FoxyCart::getFoxyCartStoreName().'.foxycart.com/checkout?fc_auth_token='. |
|
107
|
|
|
$auth_token.'&fcsid='.$fcsid.'&fc_customer_id='.$Member->Customer_ID.'×tamp='.$timestampNew; |
|
108
|
|
|
|
|
109
|
|
|
$this->redirect($redirect_complete); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|