247119f646a9429826c2d9ee9eda603e - Version 1.0.1

Version Notes

Magento 1.9.x - Masterpass Plugin

Release Version : 1.0.1

Download this release

Release Info

Developer Kanchan
Extension 247119f646a9429826c2d9ee9eda603e
Version 1.0.1
Comparing to
See all releases


Version 1.0.1

Files changed (53) hide show
  1. app/code/local/MasterCard/Countriesmapping/etc/config.xml +37 -0
  2. app/code/local/MasterCard/Countriesmapping/sql/mastercard_countriesmapping_setup/directory_country_region.csv +55 -0
  3. app/code/local/MasterCard/Countriesmapping/sql/mastercard_countriesmapping_setup/mysql4-install-0.1.0.php +52 -0
  4. app/code/local/MasterCard/Masterpass/Block/Button.php +53 -0
  5. app/code/local/MasterCard/Masterpass/Block/Checkout/Checkout.php +47 -0
  6. app/code/local/MasterCard/Masterpass/Block/Checkout/Precheckout.php +308 -0
  7. app/code/local/MasterCard/Masterpass/Block/Checkout/Review/Details.php +30 -0
  8. app/code/local/MasterCard/Masterpass/Block/Checkout/Shipping.php +13 -0
  9. app/code/local/MasterCard/Masterpass/Block/Customer/Link.php +20 -0
  10. app/code/local/MasterCard/Masterpass/Block/Customer/Wrapper.php +31 -0
  11. app/code/local/MasterCard/Masterpass/Block/Onepage/Link.php +20 -0
  12. app/code/local/MasterCard/Masterpass/Block/Onepage/Links.php +19 -0
  13. app/code/local/MasterCard/Masterpass/Block/Onepage/Sidebar.php +22 -0
  14. app/code/local/MasterCard/Masterpass/Helper/Data.php +653 -0
  15. app/code/local/MasterCard/Masterpass/Model/Masterpass.php +175 -0
  16. app/code/local/MasterCard/Masterpass/Model/Mpservice/Connector.php +438 -0
  17. app/code/local/MasterCard/Masterpass/Model/Mpservice/Masterpassservice.php +357 -0
  18. app/code/local/MasterCard/Masterpass/Model/Observer.php +183 -0
  19. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Acceptancemarksize.php +67 -0
  20. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Acceptedcards.php +60 -0
  21. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Buttonsize.php +51 -0
  22. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Environment.php +40 -0
  23. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/PaymentGateways.php +49 -0
  24. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Transactionurl.php +38 -0
  25. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Trueorfalse.php +40 -0
  26. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Userflows.php +45 -0
  27. app/code/local/MasterCard/Masterpass/Model/System/Config/Source/Xmlversion.php +36 -0
  28. app/code/local/MasterCard/Masterpass/controllers/IndexController.php +376 -0
  29. app/code/local/MasterCard/Masterpass/controllers/MasterpasspairingController.php +663 -0
  30. app/code/local/MasterCard/Masterpass/etc/adminhtml.xml +26 -0
  31. app/code/local/MasterCard/Masterpass/etc/config.xml +133 -0
  32. app/code/local/MasterCard/Masterpass/etc/system.xml +492 -0
  33. app/code/local/MasterCard/Masterpass/sql/mastercard_masterpass_setup/mysql4-install-0.1.0.php +32 -0
  34. app/design/frontend/rwd/default/layout/masterpass/masterpass.xml +211 -0
  35. app/design/frontend/rwd/default/template/masterpass/ajax/details.phtml +49 -0
  36. app/design/frontend/rwd/default/template/masterpass/ajax/shipping_method.phtml +82 -0
  37. app/design/frontend/rwd/default/template/masterpass/cart/button.phtml +12 -0
  38. app/design/frontend/rwd/default/template/masterpass/cart/buttonbottom.phtml +75 -0
  39. app/design/frontend/rwd/default/template/masterpass/customer/pair_after_register.phtml +65 -0
  40. app/design/frontend/rwd/default/template/masterpass/customer/wrapper.phtml +71 -0
  41. app/design/frontend/rwd/default/template/masterpass/onepage/checkout.phtml +96 -0
  42. app/design/frontend/rwd/default/template/masterpass/onepage/login.phtml +163 -0
  43. app/design/frontend/rwd/default/template/masterpass/onepage/shipping_form.phtml +140 -0
  44. app/design/frontend/rwd/default/template/masterpass/onepage/success.phtml +63 -0
  45. app/design/frontend/rwd/default/template/masterpass/precheckout/pairWallet.phtml +108 -0
  46. app/design/frontend/rwd/default/template/masterpass/precheckout/precheckout.phtml +514 -0
  47. app/design/frontend/rwd/default/template/masterpass/precheckout/shipping.phtml +127 -0
  48. app/design/frontend/rwd/default/template/masterpass/precheckout/shipping_method.phtml +106 -0
  49. app/design/frontend/rwd/default/template/masterpass/review/details.phtml +49 -0
  50. app/etc/modules/MasterCard_Countriesmapping.xml +9 -0
  51. app/etc/modules/MasterCard_Masterpass.xml +9 -0
  52. package.xml +47 -0
  53. skin/frontend/rwd/default/css/masterpass/masterpassclient.css +178 -0
app/code/local/MasterCard/Countriesmapping/etc/config.xml ADDED
@@ -0,0 +1,37 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ To change this license header, choose License Headers in Project Properties.
4
+ To change this template file, choose Tools | Templates
5
+ and open the template in the editor.
6
+ -->
7
+ <config>
8
+ <modules>
9
+ <MasterCard_Countriesmapping>
10
+ <version>0.1.0</version>
11
+ </MasterCard_Countriesmapping>
12
+ </modules>
13
+ <global>
14
+ <resources>
15
+ <mastercard_countriesmapping_setup>
16
+ <setup>
17
+ <module>MasterCard_Countriesmapping</module>
18
+ <class>Mage_Customer_Model_Entity_Setup</class>
19
+ </setup>
20
+ <connection>
21
+ <use>core_setup</use>
22
+ </connection>
23
+ </mastercard_countriesmapping_setup>
24
+ <mastercard_countriesmapping_write>
25
+ <connection>
26
+ <use>core_write</use>
27
+ </connection>
28
+ </mastercard_countriesmapping_write>
29
+ <mastercard_countriesmapping_read>
30
+ <connection>
31
+ <use>core_read</use>
32
+ </connection>
33
+ </mastercard_countriesmapping_read>
34
+ </resources>
35
+ </global>
36
+
37
+ </config>
app/code/local/MasterCard/Countriesmapping/sql/mastercard_countriesmapping_setup/directory_country_region.csv ADDED
@@ -0,0 +1,55 @@
1
+ AU,ACT,Australia Capital Territory
2
+ AU,NSW,New South Wales
3
+ AU,NT,Northern Territory
4
+ AU,QLD,Queensland
5
+ AU,SA,South Australia
6
+ AU,TAS,Tasmania
7
+ AU,VIC,Victoria
8
+ AU,WA,Western Australia
9
+ JP,23,Aichi
10
+ JP,5,Akita
11
+ JP,2,Aomori
12
+ JP,12,Chiba
13
+ JP,38,Ehime
14
+ JP,18,Fukui
15
+ JP,40,Fukuoka
16
+ JP,7,Fukushima
17
+ JP,21,Gifu
18
+ JP,10,Gunma
19
+ JP,34,Hiroshima
20
+ JP,1,Hokkaido
21
+ JP,28,Hyogo
22
+ JP,8,Ibaraki
23
+ JP,17,Ishikawa
24
+ JP,3,Iwate
25
+ JP,37,Kagawa
26
+ JP,46,Kagoshima
27
+ JP,14,Kanagawa
28
+ JP,39,Kochi
29
+ JP,43,Kumamoto
30
+ JP,26,Kyoto
31
+ JP,24,Mie
32
+ JP,4,Miyagi
33
+ JP,45,Miyazaki
34
+ JP,20,Nagano
35
+ JP,42,Nagasaki
36
+ JP,29,Nara
37
+ JP,15,Niigata
38
+ JP,44,Oita
39
+ JP,33,Okayama
40
+ JP,47,Okinawa
41
+ JP,27,Osaka
42
+ JP,41,Saga
43
+ JP,11,Saitama
44
+ JP,25,Shiga
45
+ JP,32,Shimane
46
+ JP,22,Shizuoka
47
+ JP,9,Tochigi
48
+ JP,36,Tokushima
49
+ JP,13,Tokyo
50
+ JP,31,Tottori
51
+ JP,16,Toyama
52
+ JP,30,Wakayama
53
+ JP,6,Yamagata
54
+ JP,35,Yamaguchi
55
+ JP,19,Yamanashi
app/code/local/MasterCard/Countriesmapping/sql/mastercard_countriesmapping_setup/mysql4-install-0.1.0.php ADDED
@@ -0,0 +1,52 @@
1
+ <?php
2
+
3
+ /*
4
+ * To change this license header, choose License Headers in Project Properties.
5
+ * To change this template file, choose Tools | Templates
6
+ * and open the template in the editor.
7
+ */
8
+ $installer = $this;
9
+
10
+ $installer->startSetup();
11
+
12
+ $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
13
+ $installer->run("
14
+ DELETE FROM `{$this->getTable('directory_country_region')}` WHERE country_id = 'AU';"); // remove Australia's states
15
+ $installer->run("
16
+ DELETE FROM `{$this->getTable('directory_country_region')}` WHERE country_id = 'JP';"); // remove Japan's states
17
+
18
+
19
+ $regionfile = dirname(__FILE__) . '/directory_country_region.csv';
20
+
21
+
22
+ $arr=array();
23
+ $row = -1;
24
+ $i=0;
25
+ if (($handle = fopen($regionfile, "r")) !== FALSE) {
26
+ while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
27
+ $num = count($data);
28
+
29
+ $row++;
30
+ for ($c = 0; $c < $num; $c++) {
31
+ $arr[$row][$c]= $data[$c];
32
+
33
+ }
34
+
35
+ $i++;
36
+ }
37
+ fclose($handle);
38
+
39
+ foreach ($arr as $data){
40
+ if(is_numeric($data[1]) && strlen($data[1])<2){
41
+ $code = '0'.$data[1];
42
+ }else{
43
+ $code = $data[1];
44
+ }
45
+ $regionValue = "('".$data[0]."','".$code."','".$data[2]."')";
46
+
47
+ $installer->run("INSERT INTO {$this->getTable('directory_country_region')} (country_id, code, default_name) VALUES ". $regionValue . ";");
48
+ }
49
+ }
50
+
51
+
52
+ $installer->endSetup();
app/code/local/MasterCard/Masterpass/Block/Button.php ADDED
@@ -0,0 +1,53 @@
1
+ <?php
2
+
3
+ /*
4
+ * Developer: Pavith Lovan
5
+ */
6
+
7
+ class MasterCard_Masterpass_Block_Button extends Mage_Core_Block_Template {
8
+
9
+ //checkout type
10
+ const EXPRESS_CHECKOUT = '3';
11
+ const CONNECTED_CHECKOUT = '2';
12
+ const STANDARD_CHECKOUT = '1';
13
+
14
+ protected function _construct() {
15
+
16
+ parent::_construct();
17
+ }
18
+
19
+ // checkout if user flow is set to express
20
+ public function checkUserFlows() {
21
+ if (Mage::helper('masterpass')->isMasterpassEnabled()) {
22
+ if (Mage::getSingleton('customer/session')->isLoggedIn()) {
23
+ if (Mage::getStoreConfig('masterpass/config/userflows') == self::CONNECTED_CHECKOUT || Mage::getStoreConfig('masterpass/config/userflows') == self::EXPRESS_CHECKOUT) {
24
+ return true;
25
+ } elseif (Mage::getStoreConfig('masterpass/config/userflows') == self::STANDARD_CHECKOUT) {
26
+ return false;
27
+ }
28
+ } else {
29
+ if ($this->getQuote()->isAllowedGuestCheckout()) {
30
+ return false;
31
+ } else {
32
+ return true;
33
+ }
34
+ }
35
+ } else {
36
+ return false;
37
+ }
38
+ }
39
+
40
+ public function walletIsPaired() {
41
+ return Mage::helper('masterpass')->checkWallet();
42
+ }
43
+
44
+ public function getQuote() {
45
+ return Mage::getSingleton('checkout/session')->getQuote();
46
+ }
47
+
48
+ //if digital product
49
+ public function digitalItem() {
50
+ return $this->getQuote()->isVirtual();
51
+ }
52
+
53
+ }
app/code/local/MasterCard/Masterpass/Block/Checkout/Checkout.php ADDED
@@ -0,0 +1,47 @@
1
+ <?php
2
+
3
+ /*
4
+ * Developer: Pavith Lovan
5
+ */
6
+ /*
7
+ * To change this template, choose Tools | Templates
8
+ * and open the template in the editor.
9
+ */
10
+
11
+ class MasterCard_Masterpass_Block_Checkout_Checkout extends Mage_Checkout_Block_Onepage_Abstract {
12
+
13
+ public function _construct() {
14
+
15
+ $this->setMasterpasscheckoutUrl(Mage::getUrl('', array('_secure' => true)) . "masterpass/index/placeorder/");
16
+
17
+ parent::_construct();
18
+ }
19
+
20
+ public function getBilling() {
21
+ return $this->getQuote()->getBillingAddress();
22
+ }
23
+
24
+ public function getShipping() {
25
+ return $this->getQuote()->getShippingAddress();
26
+ }
27
+
28
+ public function getPaymentHtml() {
29
+ return $this->getChildHtml('payment_info');
30
+ }
31
+ public function getAddress() {
32
+ if (is_null($this->_address)) {
33
+ if ($this->isCustomerLoggedIn()) {
34
+ $this->_address = $this->getQuote()->getShippingAddress();
35
+ } else {
36
+ $ship = $this->getQuote()->getShippingAddress();
37
+ $ship_country = $ship->getCountryId();
38
+ if (!empty($ship_country))
39
+ $this->_address = $ship;
40
+ else
41
+ $this->_address = Mage::getModel('sales/quote_address');
42
+ }
43
+ }
44
+
45
+ return $this->_address;
46
+ }
47
+ }
app/code/local/MasterCard/Masterpass/Block/Checkout/Precheckout.php ADDED
@@ -0,0 +1,308 @@
1
+ <?php
2
+
3
+ /*
4
+ * Developer: Pavith Lovan
5
+ */
6
+ /*
7
+ * To change this template, choose Tools | Templates
8
+ * and open the template in the editor.
9
+ */
10
+
11
+ class MasterCard_Masterpass_Block_Checkout_Precheckout extends Mage_Checkout_Block_Cart_Totals {
12
+
13
+ public $cards;
14
+ public $address;
15
+ public $reward;
16
+ public $profile;
17
+ public $longaccesstoken;
18
+ public $walletname;
19
+ public $precheckoutTransactionId;
20
+ public $consumerWalletId;
21
+ public $masterpassImage;
22
+ public $partnerLogo;
23
+
24
+ /**
25
+ * @var Mage_Sales_Model_Quote
26
+ */
27
+ protected $_quote = null;
28
+
29
+ /**
30
+ * @var Mage_Sales_Model_Quote_Address
31
+ */
32
+ protected $_shippingAddress = null;
33
+
34
+ /**
35
+ * Currently selected shipping rate.
36
+ *
37
+ * @var Mage_Sales_Model_Quote_Address_Rate
38
+ */
39
+ protected $_currentShippingRate = null;
40
+
41
+ public function _construct() {
42
+ if (Mage::getStoreConfig('masterpass/config/userflows') == '2') {
43
+ $this->setPostexpresscheckout("");
44
+ } elseif (Mage::getStoreConfig('masterpass/config/userflows') == '3') {
45
+ $this->setPostexpresscheckout(Mage::getUrl('', array('_secure' => true)) . "masterpass/masterpasspairing/placeorder");
46
+ }
47
+ parent::_construct();
48
+ }
49
+
50
+ public function getPrecheckoutdata() {
51
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
52
+ $lonAccessToken = $customer->getData('longtoken');
53
+ $result = false;
54
+ if ($lonAccessToken) {
55
+ try {
56
+
57
+ $preCheckoutData = Mage::helper('masterpass')->postPreCheckoutData($lonAccessToken);
58
+
59
+ if (!empty($preCheckoutData)) {
60
+
61
+ if (Mage::getStoreConfig('masterpass/config/cfcard')) {
62
+ $this->cards = $preCheckoutData->PrecheckoutData->Cards;
63
+ if (!$this->cards) {
64
+ //save long access token
65
+ Mage::helper('masterpass')->removeMpLongaccesstoken();
66
+ }
67
+ }
68
+ if (Mage::getStoreConfig('masterpass/config/cfprofile')) {
69
+ $this->profile = $preCheckoutData->PrecheckoutData->Contact;
70
+ }
71
+ if (Mage::getStoreConfig('masterpass/config/cfaddress')) {
72
+ $this->address = $preCheckoutData->PrecheckoutData->ShippingAddresses;
73
+ }
74
+ if (Mage::getStoreConfig('masterpass/config/exreward')) {
75
+ $this->reward = $preCheckoutData->PrecheckoutData->RewardPrograms;
76
+ }
77
+ $this->masterpassImage = $preCheckoutData->MasterpassLogoUrl;
78
+ $this->partnerLogo = $preCheckoutData->WalletPartnerLogoUrl;
79
+ // store partner logo for order confirmation page
80
+ Mage::getSingleton('core/session')->setPartnerLogo(htmlentities($preCheckoutData->WalletPartnerLogoUrl));
81
+
82
+ $this->longaccesstoken = $preCheckoutData->LongAccessToken;
83
+ $this->walletname = $preCheckoutData->PrecheckoutData->WalletName;
84
+ $this->precheckoutTransactionId = $preCheckoutData->PrecheckoutData->PrecheckoutTransactionId;
85
+ $this->consumerWalletId = $preCheckoutData->PrecheckoutData->ConsumerWalletId;
86
+
87
+ $result = true;
88
+ } else {
89
+ return false;
90
+ }
91
+ } catch (Exception $e) {
92
+ Mage::log($e->getMessage());
93
+ //remove longaccesstoken
94
+ Mage::helper('masterpass')->removeMpLongaccesstoken();
95
+
96
+ Mage::getSingleton('core/session')->addError(Mage::getStoreConfig('masterpass/config/checkouterrormessage'));
97
+ $domain = Mage::getUrl('', array(
98
+ '_secure' => true
99
+ ));
100
+ $url = $domain . "checkout/onepage/";
101
+ Mage::app()->getFrontController()->getResponse()->setRedirect($url);
102
+ }
103
+ }
104
+ return $result;
105
+ }
106
+
107
+ public function getLongToken() {
108
+ return $this->longaccesstoken;
109
+ }
110
+
111
+ public function getPreCheckoutTranId() {
112
+ return $this->precheckoutTransactionId;
113
+ }
114
+
115
+ public function getConWalletId() {
116
+ return $this->consumerWalletId;
117
+ }
118
+
119
+ public function partnerLogo() {
120
+ return $this->partnerLogo;
121
+ }
122
+
123
+ public function masterPassLogo() {
124
+ return $this->masterpassImage;
125
+ }
126
+
127
+ public function getWalletName() {
128
+ return $this->walletname;
129
+ }
130
+
131
+ public function connectedCallBackUrl() {
132
+ return Mage::getUrl('', array('_secure' => true)) . "masterpass/masterpasspairing/connectedcheckout";
133
+ }
134
+
135
+ public function getPreCards() {
136
+ if (!empty($this->cards)) {
137
+
138
+ $cards = json_decode(json_encode((array) $this->cards), 1);
139
+ if (is_array($cards) && !empty($cards['Card'][0])) {
140
+ return $cards['Card'];
141
+ } else {
142
+ return $cards;
143
+ }
144
+ } else {
145
+ return null;
146
+ }
147
+ }
148
+
149
+ //get shipping address list
150
+ public function getPreAddress() {
151
+ //check if it's a virtual product
152
+ $quote = $this->getQuote();
153
+
154
+ if (!$quote->getIsVirtual()) {
155
+
156
+ if (!empty($this->address)) {
157
+ $address = json_decode(json_encode((array) $this->address), 1);
158
+ Mage::getSingleton('customer/session')->setPreaddress($address);
159
+ if (is_array($address) && !empty($address['ShippingAddress'][0])) {
160
+ return $address['ShippingAddress'];
161
+ } else {
162
+ return $address;
163
+ }
164
+ } else {
165
+ return null;
166
+ }
167
+ } else {
168
+ return null;
169
+ }
170
+ }
171
+
172
+ //set default shippping address
173
+ public function mpShippingAddress() {
174
+ $shipping = array();
175
+ if (!empty($this->address)) {
176
+ $address = json_decode(json_encode((array) $this->address), 1);
177
+
178
+ if (is_array($address) && !empty($address['ShippingAddress'][0])) {
179
+ $shipping = $address['ShippingAddress'][0];
180
+ } else {
181
+ $shipping = $address['ShippingAddress'];
182
+ }
183
+ }
184
+
185
+ $shippingAdd = Mage::getSingleton('checkout/type_onepage');
186
+
187
+ $name = explode(' ', $shipping['RecipientName']);
188
+ if ($name[1] == '') {
189
+ $name[1] = $name[0];
190
+ }
191
+ //check address line2
192
+ if (!is_array($shipping['Line2'])) {
193
+ $lin2 = $shipping['Line2'];
194
+ } else {
195
+ $lin2 = '';
196
+ }
197
+ $mpData = Mage::getModel('masterpass/masterpass');
198
+
199
+ // get shipping address
200
+
201
+ $regionIdS = $this->checkMasterpassData($mpData->statesMapping($shipping['CountrySubdivision']));
202
+ $regionS = $this->checkMasterpassData($mpData->getMasterpassRegion($shipping['CountrySubdivision']));
203
+ $shippingPhone = $mpData->getRemoveDash($this->checkMasterpassData($shipping['RecipientPhoneNumber']));
204
+ $data = array(
205
+ 'firstname' => $name[0],
206
+ 'lastname' => $name[1],
207
+ 'street' => array(0 => $shipping['Line1'], 1 => $lin2),
208
+ 'city' => $shipping['City'],
209
+ 'region_id' => $regionIdS,
210
+ 'region' => $regionS,
211
+ 'postcode' => $shipping['PostalCode'],
212
+ 'country_id' => $shipping['Country'],
213
+ 'telephone' => $shippingPhone
214
+ );
215
+
216
+ $shippingAdd->saveShipping($data, null);
217
+ }
218
+
219
+ public function getPreProfile() {
220
+ if (!empty($this->profile)) {
221
+ $profile = json_decode(json_encode((array) $this->profile), 1);
222
+ if (is_array($profile)) {
223
+ return $profile;
224
+ } else {
225
+ return $profile;
226
+ }
227
+ } else {
228
+ return null;
229
+ }
230
+ }
231
+
232
+ public function getPreReward() {
233
+ if (!empty($this->reward)) {
234
+ $reward = json_decode(json_encode((array) $this->reward), 1);
235
+ if (is_array($reward) && !empty($reward['RewardProgram'][0])) {
236
+ return $reward['RewardProgram'];
237
+ } else {
238
+ return $reward;
239
+ }
240
+ } else {
241
+ return null;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Get checkout quote object.
247
+ *
248
+ * @return Mage_Sales_Model_Quote
249
+ */
250
+ public function getQuote() {
251
+ if ($this->_quote === null) {
252
+ $this->_quote = Mage::getSingleton("checkout/session")->getQuote();
253
+ }
254
+ return $this->_quote;
255
+ }
256
+
257
+ /**
258
+ * Get shipping address from quote.
259
+ * Returns null if the quote only contains virtual products.
260
+ *
261
+ * @return Mage_Sales_Model_Quote_Address
262
+ */
263
+ public function canbeShipped() {
264
+ if ($this->getQuote()->isVirtual()) {
265
+ return false;
266
+ } else {
267
+ return true;
268
+ }
269
+ // return $this->getQuote()->getShippingAddress();
270
+ }
271
+
272
+ public function acceptanceMark() {
273
+ return '<img src="https://www.mastercard.com/mc_us/wallet/img/en/US/mp_mc_acc_' . Mage::getStoreConfig('masterpass/config/acceptancemark') . '_gif.gif" alt=" MasterPass Checkout"/>';
274
+ }
275
+
276
+ protected function _beforeToHtml() {
277
+
278
+
279
+ $quote = $this->getQuote();
280
+
281
+ if ($quote->getIsVirtual()) {
282
+ $this->setShippingRateRequired(false);
283
+ } else {
284
+ $this->setShippingRateRequired(true);
285
+
286
+ // misc shipping parameters
287
+ $this->setShippingMethodSubmitUrl($this->getUrl("masterpass/masterpasspairing/saveShippingMethod"))
288
+ ->setCanEditShippingAddress(false) // MasterPass doesn't let the customer return to edit details at present.
289
+ ->setCanEditShippingMethod(true); // MasterPass doesn't support handling shipping methods at present, so this must always be true.
290
+ }
291
+
292
+ $this->setPlaceOrderUrl($this->getUrl("masterpass/masterpasspairing/placeOrder"));
293
+
294
+ return parent::_beforeToHtml();
295
+ }
296
+
297
+ public function checkMasterpassData($mpData) {
298
+ if (!is_array($mpData)) {
299
+ $mpAfterCheck = $mpData;
300
+ } else {
301
+ $mpAfterCheck = '';
302
+ }
303
+ return $mpAfterCheck;
304
+ }
305
+
306
+ }
307
+
308
+ ?>
app/code/local/MasterCard/Masterpass/Block/Checkout/Review/Details.php ADDED
@@ -0,0 +1,30 @@
1
+ <?php
2
+
3
+
4
+ class MasterCard_Masterpass_Block_Checkout_Review_Details extends Mage_Checkout_Block_Cart_Totals
5
+ {
6
+ protected $_address = null;
7
+
8
+ /**
9
+ * Get shipping address from quote.
10
+ *
11
+ * @return Mage_Sales_Model_Order_Address
12
+ */
13
+ public function getAddress()
14
+ {
15
+ if ($this->_address === null) {
16
+ $this->_address = $this->getQuote()->getShippingAddress();
17
+ }
18
+ return $this->_address;
19
+ }
20
+
21
+ /**
22
+ * Return review quote totals.
23
+ *
24
+ * @return array
25
+ */
26
+ public function getTotals()
27
+ {
28
+ return $this->getQuote()->getTotals();
29
+ }
30
+ }
app/code/local/MasterCard/Masterpass/Block/Checkout/Shipping.php ADDED
@@ -0,0 +1,13 @@
1
+ <?php
2
+
3
+ class MasterCard_Masterpass_Block_Checkout_Shipping extends Mage_Checkout_Block_Onepage_Shipping {
4
+
5
+ public function getAddress() {
6
+ if (is_null($this->_address)) {
7
+ $this->_address = Mage::helper('masterpass')-> returnAddress();
8
+ }
9
+
10
+ return $this->_address;
11
+ }
12
+
13
+ }
app/code/local/MasterCard/Masterpass/Block/Customer/Link.php ADDED
@@ -0,0 +1,20 @@
1
+ <?php
2
+
3
+ /*
4
+ Pavith Lovan
5
+ */
6
+
7
+ class MasterCard_Masterpass_Block_Customer_Link extends Mage_Core_Block_Template {
8
+
9
+ public function addProfileLink() {
10
+ $private_key = Mage::helper('masterpass')->checkPrivateKey();
11
+ if ($private_key) {
12
+ if (Mage::helper('masterpass')->isMasterpassEnabled() && Mage::helper('masterpass')->isExpressEnabled()) {
13
+ $navigation = $this->getParentBlock();
14
+
15
+ $navigation->addLink('masterpass', 'masterpass/masterpasspairing', $this->__("Connect With MasterPass"), array('_secure' => true));
16
+ }
17
+ }
18
+ }
19
+
20
+ }
app/code/local/MasterCard/Masterpass/Block/Customer/Wrapper.php ADDED
@@ -0,0 +1,31 @@
1
+ <?php
2
+
3
+ /*
4
+ // Pavith Lovan
5
+ */
6
+
7
+ class MasterCard_Masterpass_Block_Customer_Wrapper extends Mage_Core_Block_Template {
8
+
9
+ public function getPairingButton() {
10
+ if (Mage::getStoreConfig('masterpass/config/enabled')) {
11
+ return true;
12
+ } else {
13
+ return false;
14
+ }
15
+ }
16
+
17
+ public function pairingcallbackUrl() {
18
+ return Mage::getUrl('', array('_secure' => true)) . "masterpass/masterpasspairing/pairingcallback";
19
+ }
20
+
21
+ public function isWalletPaired() {
22
+ return Mage::helper('masterpass')->checkWallet();
23
+ }
24
+
25
+ public function pairingData() {
26
+ $pairingData = Mage::helper('masterpass')->pairingDataTypes();
27
+ $pairingData = explode(',', $pairingData);
28
+ return $pairingData;
29
+ }
30
+
31
+ }
app/code/local/MasterCard/Masterpass/Block/Onepage/Link.php ADDED
@@ -0,0 +1,20 @@
1
+ <?php
2
+
3
+ class MasterCard_Masterpass_Block_Onepage_Link extends Mage_Checkout_Block_Onepage_Link {
4
+
5
+ public function getCheckoutUrl() {
6
+ //check if customer already paired wallet with masterpass
7
+
8
+ if (!$this->helper('masterpass')->isMasterpassEnabled()) {
9
+ return parent::getCheckoutUrl();
10
+ }
11
+ $masterpass_CheckoutUrl = Mage::helper('masterpass')->getMasterpassCheckoutUrl();
12
+
13
+ if ($masterpass_CheckoutUrl!=null) {
14
+ return $this->getUrl($masterpass_CheckoutUrl, array('_secure' => true));
15
+ }else{
16
+ return parent::getCheckoutUrl();
17
+ }
18
+ }
19
+
20
+ }
app/code/local/MasterCard/Masterpass/Block/Onepage/Links.php ADDED
@@ -0,0 +1,19 @@
1
+ <?php
2
+
3
+ class MasterCard_Masterpass_Block_Onepage_Links extends Mage_Checkout_Block_Onepage_Link {
4
+
5
+ public function getCheckoutUrl() {
6
+ return $this->getUrl('checkout/onepage', array('_secure' => true));
7
+ }
8
+
9
+ /**
10
+ * Add link on checkout page to parent block
11
+ *
12
+ * @return Mage_Checkout_Block_Links
13
+ */
14
+ public function addCheckoutLink() {
15
+
16
+ return $this;
17
+ }
18
+
19
+ }
app/code/local/MasterCard/Masterpass/Block/Onepage/Sidebar.php ADDED
@@ -0,0 +1,22 @@
1
+ <?php
2
+ class MasterCard_Masterpass_Block_Onepage_Sidebar extends Mage_Checkout_Block_Cart_Sidebar
3
+ {
4
+
5
+ public function getCheckoutUrl()
6
+ {
7
+
8
+ //check if customer already paired wallet with masterpass
9
+
10
+
11
+ if (!$this->helper('masterpass')->isMasterpassEnabled()) {
12
+ return parent::getCheckoutUrl();
13
+ }
14
+ $masterpass_CheckoutUrl = Mage::helper('masterpass')->getMasterpassCheckoutUrl();
15
+
16
+ if ($masterpass_CheckoutUrl!=null) {
17
+ return $this->getUrl($masterpass_CheckoutUrl, array('_secure' => true));
18
+ }else{
19
+ return parent::getCheckoutUrl();
20
+ }
21
+ }
22
+ }
app/code/local/MasterCard/Masterpass/Helper/Data.php ADDED
@@ -0,0 +1,653 @@
1
+ <?php
2
+
3
+ /*
4
+ * Developer: Pavith Lovan
5
+ */
6
+
7
+ class MasterCard_Masterpass_Helper_Data extends Mage_Core_Helper_Abstract {
8
+
9
+ public $service;
10
+ public $appData;
11
+ public $xmlVersion;
12
+
13
+ //checkout type
14
+ const EXPRESS_CHECKOUT = '3';
15
+ const CONNECTED_CHECKOUT = '2';
16
+ //card types
17
+ const DINERS = 'diners';
18
+ const MASTER = 'master';
19
+ const VISA = 'visa';
20
+ const MAESTRO = 'maestro';
21
+ const DISCOVER = 'discover';
22
+ const AMEX = 'amex';
23
+ const OTHER = 'OT'; // other type can be use for private label card
24
+
25
+ public function __construct() {
26
+
27
+ // masterpass api version
28
+ $this->xmlVersion = Mage::getStoreConfig('masterpass/config/masterpassxml');
29
+
30
+ $this->service = Mage::getModel('masterpass/mpservice_masterpassservice');
31
+ }
32
+
33
+ // check if MasterPass is enabled
34
+ public function isMasterpassEnabled() {
35
+ $checkPv_key = $this->checkPrivateKey();
36
+
37
+ if (Mage::getStoreConfig('masterpass/config/enabled') && $checkPv_key) {
38
+ return true;
39
+ } else {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ public function isExpressEnabled() {
45
+ if (Mage::getStoreConfig('masterpass/config/userflows') == self::CONNECTED_CHECKOUT || Mage::getStoreConfig('masterpass/config/userflows') == self::EXPRESS_CHECKOUT) {
46
+ return true;
47
+ } else {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ //get recipien name
53
+ public function getRecipienName($reecipientName) {
54
+ $name = explode(' ', $reecipientName);
55
+ if ($name[1] == '') {
56
+ $name[1] = $name[0];
57
+ }
58
+ return $name;
59
+ }
60
+
61
+ public function checkIfSensitiveRequest(Mage_Core_Controller_Request_Http $request) {
62
+
63
+ if ($request->getRequestedRouteName() == "masterpass") {
64
+ return false;
65
+ } else {
66
+ return true;
67
+ }
68
+ }
69
+ //check masterpass wallet status
70
+ public function checkWallet(){
71
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
72
+ $customerData = Mage::getModel('customer/customer')->load($customer->getId());
73
+ $longtoken = $customerData->getData('longtoken');
74
+
75
+ // check if wallet is unpaired
76
+ if ($longtoken) {
77
+ $preCheckoutData = $this->postPreCheckoutData($longtoken);
78
+
79
+ if (empty($preCheckoutData)) {
80
+ $this->removeMpLongaccesstoken();
81
+ $longtoken = null;
82
+ }
83
+ }
84
+ if (!empty($longtoken)) {
85
+ return true;
86
+ } else {
87
+ return false;
88
+ }
89
+ }
90
+ public function checkMasterpassData($mpData) {
91
+ if (!is_array($mpData)) {
92
+ $mpAfterCheck = $mpData;
93
+ } else {
94
+ $mpAfterCheck = '';
95
+ }
96
+ return $mpAfterCheck;
97
+ }
98
+ //return address
99
+ public function returnAddress() {
100
+ $_address = null;
101
+ if ($this->isCustomerLoggedIn()) {
102
+ $_address = $this->getQuote()->getShippingAddress();
103
+ } else {
104
+ $ship = $this->getQuote()->getShippingAddress();
105
+ $ship_country = $ship->getCountryId();
106
+ if (!empty($ship_country))
107
+ $_address = $ship;
108
+ else
109
+ $_address = Mage::getModel('sales/quote_address');
110
+ }
111
+ Mage::log("return address " . $_address);
112
+
113
+ return $_address;
114
+ }
115
+
116
+ // make sure all session is unset
117
+ public function resetMasterData() {
118
+
119
+ Mage::getSingleton('core/session')->unsProfileName();
120
+ Mage::getSingleton('core/session')->unsPrecheckout();
121
+ Mage::getSingleton('checkout/session')->unsMasterPassCard();
122
+ Mage::getSingleton('checkout/session')->unsPostBackData();
123
+ Mage::getSingleton('checkout/session')->unsCardMask();
124
+ Mage::getSingleton('customer/session')->unsPreaddress();
125
+ }
126
+
127
+ //validate privatekey password
128
+ public function checkPrivateKey() {
129
+ $keystore = Mage::getModel('masterpass/mpservice_connector')->merchantPrivateKey();
130
+
131
+ if ($keystore['pkey']) {
132
+ return true;
133
+ } else {
134
+ Mage::log('Invalid private key password');
135
+ return false;
136
+ }
137
+ }
138
+
139
+ //remove existing long eccesstokem
140
+ public function removeMpLongaccesstoken() {
141
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
142
+ $customerData = Mage::getModel('customer/customer')->load($customer->getId());
143
+ $longAccessToken = null;
144
+ $customerData->setData('longtoken', $longAccessToken);
145
+
146
+ $customerData->save();
147
+ return $this;
148
+ }
149
+
150
+ // request masterpass token
151
+
152
+ public function getMasterpassToken() {
153
+ $module_path = Mage::getModuleDir('', 'MasterCard_Masterpass');
154
+
155
+ $prefix = 'https://';
156
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
157
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
158
+ $consumerKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbconsumerkey'));
159
+ } else {
160
+ $consumerKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/prconsumerkey'));
161
+ }
162
+
163
+ $requestUrl = $prefix . "api.mastercard.com/oauth/consumer/v1/request_token";
164
+ $callbackUrl = Mage::getUrl('', array('_secure' => true)) . "masterpass/index/callback";
165
+ try {
166
+ $requestToken = $this->service->GetRequestToken($requestUrl, $callbackUrl);
167
+
168
+ return $requestToken;
169
+ } catch (Exception $e) {
170
+ Mage::log($e->getMessage());
171
+
172
+ return $requestToken = null;
173
+ }
174
+ }
175
+
176
+ /* get magento shopping */
177
+
178
+ public function getShoppingCart() {
179
+
180
+ $cart = Mage::getSingleton('checkout/session')->getQuote();
181
+ // Number of items
182
+ $numberOfItems = $cart->getItemsCount();
183
+ $requestToken = null;
184
+
185
+ $xml_cart = '';
186
+ if ($numberOfItems > 0 && isset($this->getMasterpassToken()->requestToken)) {
187
+ $requestToken = $this->getMasterpassToken()->requestToken;
188
+
189
+ $xml_cart = "<?xml version=\"1.0\"?><ShoppingCartRequest><OAuthToken>" . $requestToken . "</OAuthToken><ShoppingCart>";
190
+
191
+ $currencyCode = $cart->getGlobalCurrencyCode();
192
+
193
+ $xml_cart .= "<CurrencyCode>" . $currencyCode . "</CurrencyCode>";
194
+ $subTotal = $cart->getGrandTotal();
195
+ $subTotal = $subTotal * 100;
196
+ $xml_cart .= "<Subtotal>" . $subTotal . "</Subtotal>";
197
+
198
+ $all_items = $cart->getAllVisibleItems();
199
+ $generic_error_message = Mage::getStoreConfig('masterpass/config/checkouterrormessage');
200
+
201
+ foreach ($all_items as $item) {
202
+ $xml_cart .= "<ShoppingCartItem>";
203
+ $productId = $item->getProductId();
204
+ $productPrice = $item->getPrice();
205
+ $description = htmlentities($item->getName());
206
+ $value = $item->getPrice();
207
+
208
+ $quantity = $item->getQty();
209
+ $value = $value * $quantity;
210
+ $value = $value * 100;
211
+ $imageUrl = htmlentities(Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail'));
212
+ $xml_cart .= "<Description>" . $description . "</Description><Quantity>" . $quantity . "</Quantity><Value>" . $value . "</Value><ImageURL>" . $imageUrl . "</ImageURL>";
213
+ $xml_cart .= "</ShoppingCartItem>";
214
+ }
215
+ // if coupon
216
+ $discount = $cart->getTotals();
217
+ if (isset($discount['discount'])) {
218
+
219
+ $discountAmount = $discount['discount']->getValue() * 100;
220
+ $discountAmount = $discountAmount; //*-1;
221
+ if (isset($discountAmount)) {
222
+ $xml_cart .= '<ShoppingCartItem><Description>Discount</Description><Quantity>1</Quantity><Value>' . $discountAmount . '</Value><ImageURL></ImageURL></ShoppingCartItem>';
223
+ }
224
+ }
225
+ $xml_cart .= "</ShoppingCart><OriginUrl>" . Mage::getBaseUrl() . "</OriginUrl></ShoppingCartRequest>";
226
+
227
+ return $xml_cart;
228
+ } else {
229
+ return $requestToken;
230
+ }
231
+ }
232
+
233
+ // post shopping cart to MasterPass
234
+ public function postShoppingCart() {
235
+
236
+ $shoppingCartRequest = $this->getShoppingCart();
237
+
238
+ if (!empty($shoppingCartRequest)) {
239
+ $prefix = 'https://';
240
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
241
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
242
+ $consumerKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbconsumerkey'));
243
+ }
244
+ $shoppingCartUrl = $prefix . "api.mastercard.com/masterpass/" . $this->xmlVersion . "/shopping-cart";
245
+
246
+ try {
247
+ $service = Mage::getModel('masterpass/mpservice_masterpassservice');
248
+ $shoppingCartResponse = $service->postShoppingCartData($shoppingCartUrl, $shoppingCartRequest);
249
+ $data = simplexml_load_string($shoppingCartResponse);
250
+ $data = json_decode(json_encode((array) $data), 1);
251
+ return $data['OAuthToken'];
252
+ } catch (Exception $e) {
253
+ Mage::log($e->getMessage());
254
+ return null;
255
+ }
256
+ } else {
257
+ return null;
258
+ }
259
+ }
260
+
261
+ public function acceptedCards() {
262
+ $acceptedCards = Mage::getStoreConfig('masterpass/config/acceptedcards');
263
+ $privateLabel = Mage::getStoreConfig('masterpass/config/privatelabel');
264
+ if ($privateLabel) {
265
+ $acceptedCards = $acceptedCards . ',' . $privateLabel;
266
+ }
267
+ return $acceptedCards;
268
+ }
269
+
270
+ public function getMerchantSettings() {
271
+ if (Mage::getStoreConfig('masterpass/config/requestbasiccheckout') == 'true') {
272
+ $basicchk = '1';
273
+ } else {
274
+ $basicchk = '';
275
+ }
276
+ $data = array('checkout_id' => '',
277
+ 'suppressShipping' => '',
278
+ 'loyaltyEnabled' => Mage::getStoreConfig('masterpass/config/rewards'),
279
+ 'loyaltyProgram' => Mage::getStoreConfig('masterpass/config/loyaltyprogram'),
280
+ 'requestBasicCheckout' => $basicchk,
281
+ 'version' => Mage::getStoreConfig('masterpass/config/masterpassxml'));
282
+
283
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
284
+ $data['checkout_id'] = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbcheckoutidentifier'));
285
+ } else {
286
+ $data['checkout_id'] = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/prcheckoutidentifier'));
287
+ }
288
+ if ($this->getQuote()->isVirtual()) {
289
+ $data['suppressShipping'] = 'true';
290
+ } else {
291
+ if (Mage::getStoreConfig('masterpass/config/shippingsuppression') == 'true') {
292
+ $data['suppressShipping'] = 'true';
293
+ } else {
294
+ $data['suppressShipping'] = 'false';
295
+ }
296
+ }
297
+ $data['loyaltyEnabled'] = Mage::getStoreConfig('masterpass/config/rewards');
298
+ return $data;
299
+ }
300
+
301
+ public function getQuote() {
302
+ return Mage::getSingleton('checkout/session')->getQuote();
303
+ }
304
+
305
+ // get access token from masterpass
306
+ public function getAccessToken($requestToken, $requestVerifier) {
307
+
308
+
309
+ $prefix = 'https://';
310
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
311
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
312
+ }
313
+ $accessUrl = $prefix . "api.mastercard.com/oauth/consumer/v1/access_token";
314
+
315
+ try {
316
+ $getAccessToken = $this->service->GetAccessToken($accessUrl, $requestToken, $requestVerifier);
317
+ return $getAccessToken;
318
+ } catch (Exception $e) {
319
+ Mage::log($e->getMessage());
320
+ return null;
321
+ }
322
+ }
323
+
324
+ public function getCheckoutData($checkoutResourceUrl, $accessToken) {
325
+ $checkoutObject = $this->service->GetPaymentShippingResource($checkoutResourceUrl, $accessToken);
326
+
327
+ return $checkoutObject;
328
+ }
329
+
330
+ /**
331
+ *
332
+ * Used to format the Errors XML for display
333
+ *
334
+ * @return formatted error message
335
+ */
336
+ // Used to format the Error XML for display
337
+ public function formatError($errorMessage) {
338
+ if (preg_match(Connector::ERRORS_TAG, $errorMessage) > 0) {
339
+ $errorMessage = $this->formatXML($errorMessage);
340
+ }
341
+ return $errorMessage;
342
+ }
343
+
344
+ // formate xml
345
+ public function formatXML($resources) {
346
+ if ($resources != null) {
347
+ $dom = new DOMDocument;
348
+ $dom->preserveWhiteSpace = FALSE;
349
+ $dom->loadXML($resources);
350
+ $dom->formatOutput = TRUE;
351
+ $resources = $dom->saveXml();
352
+
353
+ $resources = htmlentities($resources);
354
+ }
355
+ return $resources;
356
+ }
357
+
358
+ //******************************EXPRESS CHECKOUT ********************************************
359
+ // express call back url
360
+ public function expresscallBackurl() {
361
+ return Mage::getUrl('', array('_secure' => true)) . "masterpass/index/expresscallback";
362
+ }
363
+
364
+ public function postMerchantInitData() {
365
+ $pairingToken = $this->getMasterpassToken()->requestToken;
366
+ if ($pairingToken) {
367
+ $merchantInitRequest = $this->parseMerchantInitXML($pairingToken);
368
+
369
+ $prefix = 'https://';
370
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
371
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
372
+ }
373
+ $merchantInitUrl = $prefix . "api.mastercard.com/masterpass/" . $this->xmlVersion . "/merchant-initialization";
374
+
375
+ $merchantInitResponse = $this->service->postMerchantInitData($merchantInitUrl, $merchantInitRequest);
376
+ $merchantInitResponse = simplexml_load_string($merchantInitResponse);
377
+ $merchantInitResponse = json_decode(json_encode((array) $merchantInitResponse), 1);
378
+ return $merchantInitResponse['OAuthToken'];
379
+ } else {
380
+ return false;
381
+ }
382
+ }
383
+
384
+ public function parseMerchantInitXML($pairingToken) {
385
+
386
+ $originUrl = Mage::getUrl('', array('_secure' => true));
387
+ $merchantInitData = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><MerchantInitializationRequest><OAuthToken>' . $pairingToken . '</OAuthToken><PreCheckoutTransactionId/><OriginUrl>' . $originUrl . '</OriginUrl></MerchantInitializationRequest>';
388
+
389
+ return $merchantInitData;
390
+ }
391
+
392
+ //masterpass checkout url
393
+ public function getMasterpassCheckoutUrl() {
394
+ //check if customer already paired wallet with masterpass
395
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
396
+ $lonAccessToken = $customer->getData('longtoken');
397
+
398
+ $url = null;
399
+ if (!empty($lonAccessToken) && Mage::getStoreConfig('masterpass/config/userflows') == self::CONNECTED_CHECKOUT) {
400
+ $url = 'masterpass/masterpasspairing/precheckout';
401
+ } elseif (!empty($lonAccessToken) && Mage::getStoreConfig('masterpass/config/userflows') == self::EXPRESS_CHECKOUT) {
402
+ $url = 'masterpass/masterpasspairing/precheckout';
403
+ }
404
+ return $url;
405
+ }
406
+
407
+ public function pairingDataTypes() {
408
+ $pairingDataType = ''; //["CARD,PROFILE,ADDRESS,REWARD_PROGRAM"],
409
+ if (Mage::getStoreConfig('masterpass/config/cfcard')) {
410
+ $pairingDataType .= 'CARD,';
411
+ }
412
+ if (Mage::getStoreConfig('masterpass/config/cfprofile')) {
413
+ $pairingDataType .= 'PROFILE,';
414
+ }
415
+ if (Mage::getStoreConfig('masterpass/config/cfaddress')) {
416
+ $pairingDataType .= 'ADDRESS,';
417
+ }
418
+ if (Mage::getStoreConfig('masterpass/config/exreward')) {
419
+ $pairingDataType .= 'REWARD_PROGRAM,';
420
+ }
421
+ $pairingDataType = substr($pairingDataType, 0, -1);
422
+ return $pairingDataType;
423
+ }
424
+
425
+ public function requestExpressCheckout() {
426
+ $xpr = 'false';
427
+ if (Mage::getStoreConfig('masterpass/config/userflows') == self::EXPRESS_CHECKOUT) {
428
+ $xpr = 'true';
429
+ }
430
+ return $xpr;
431
+ }
432
+
433
+ public function postPreCheckoutData($longAccessToken) {
434
+ $preCheckoutRequest = $this->parsePrecheckoutXml();
435
+ $prefix = 'https://';
436
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
437
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
438
+ }
439
+ $preCheckoutUrl = $prefix . "api.mastercard.com/masterpass/" . $this->xmlVersion . "/precheckout";
440
+
441
+ try {
442
+ $preCheckoutResponse = $this->service->getPreCheckoutData($preCheckoutUrl, $preCheckoutRequest, $longAccessToken);
443
+
444
+ // Special syntax for working with SimpleXMLElement objects
445
+ $preCheckoutResponse = simplexml_load_string($preCheckoutResponse);
446
+
447
+ if(Mage::getStoreConfig ( 'masterpass/config/userflows' ) == self::EXPRESS_CHECKOUT)
448
+ {
449
+ $this->requestToken = $this->postMerchantInitData();
450
+ Mage::getSingleton('core/session')->setExpToken($this->requestToken);
451
+ }
452
+ if ($preCheckoutResponse != null) {
453
+ $this->saveLongToken ( $preCheckoutResponse->LongAccessToken );
454
+ return $preCheckoutResponse;
455
+ }
456
+
457
+ } catch (Exception $e) {
458
+ Mage::log($e->getMessage());
459
+
460
+ }
461
+ }
462
+
463
+ protected function saveLongToken($longToken) {
464
+ //save long access token
465
+ $customer = Mage::getSingleton('customer/session')->getCustomer();
466
+ $customerData = Mage::getModel('customer/customer')->load($customer->getId());
467
+ $customerData->setData('longtoken', $longToken);
468
+ try {
469
+ $customerData->save();
470
+ } catch (Exception $e) {
471
+ Mage::log($e->getMessage());
472
+ }
473
+ }
474
+
475
+ public function postExpressCheckoutData($post) {
476
+
477
+ Mage::getSingleton ( 'customer/session' )->setExprsFormData($post);
478
+ $response = null;
479
+ $longAccessToken = $post['longaccesstoken'];
480
+
481
+ $prefix = 'https://';
482
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
483
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
484
+ $checkout_id = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbcheckoutidentifier'));
485
+ } else {
486
+ $checkout_id = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/prcheckoutidentifier'));
487
+ }
488
+ $expressCheckoutUrl = $prefix . "api.mastercard.com/masterpass/" . $this->xmlVersion . "/expresscheckout";
489
+
490
+ $cart = Mage::getSingleton('checkout/session')->getQuote();
491
+ $subTotal = $cart->getGrandTotal();
492
+ $subTotal = $subTotal * 100;
493
+
494
+ //check if noshipping required
495
+ $noshipping = false;
496
+ $quote = Mage::getSingleton("checkout/session")->getQuote();
497
+ if ($quote->isVirtual()) {
498
+ $noshipping = true;
499
+ } elseif (!$quote->isVirtual() && !Mage::getStoreConfig('masterpass/config/cfaddress')) {
500
+ $noshipping = true;
501
+ }
502
+ $expressCheckoutRequest = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
503
+ <ExpressCheckoutRequest>
504
+ <MerchantCheckoutId>' . $checkout_id . '</MerchantCheckoutId>
505
+ <PrecheckoutTransactionId>' . $post['precheckoutid'] . '</PrecheckoutTransactionId>
506
+ <CurrencyCode>' . Mage::app()->getStore($storeID)->getCurrentCurrencyCode() . '</CurrencyCode>
507
+ <OrderAmount>' . $subTotal . '</OrderAmount>
508
+ <CardId>' . $post['cards'] . '</CardId>';
509
+ if ($noshipping) {
510
+ $expressCheckoutRequest .= '<DigitalGoods>' . $noshipping . '</DigitalGoods>';
511
+ } else {
512
+ $expressCheckoutRequest .= '<ShippingAddressId>' . $post['address'] . '</ShippingAddressId>';
513
+ }
514
+ if (isset($post['rewardprogram'])) {
515
+ $expressCheckoutRequest .= '<RewardProgramId>' . $post['rewardprogram'] . '</RewardProgramId>';
516
+ }
517
+ $expressCheckoutRequest .= '<WalletId>' . $post['walletid'] . '</WalletId>
518
+ <AdvancedCheckoutOverride>false</AdvancedCheckoutOverride>
519
+ <OriginUrl>' . Mage::getBaseUrl() . '</OriginUrl>
520
+ </ExpressCheckoutRequest>';
521
+
522
+ Mage::getSingleton ( "core/session" )->setpreTransId ( $post ['precheckoutid'] );
523
+ Mage::getSingleton("customer/session")->setData("expressData", $post);
524
+
525
+ try {
526
+ $expressCheckoutResponse = $this->service->getExpressCheckoutData($expressCheckoutUrl, $expressCheckoutRequest, $longAccessToken);
527
+ $response = simplexml_load_string($expressCheckoutResponse);
528
+
529
+ if (isset ( $response->Errors->Error )) {
530
+ if ($response->Errors->Error->Source == "3DS Needed") {
531
+ Mage::getSingleton ( 'core/session' )->setExpSecure ( true );
532
+ }
533
+ }else {
534
+ Mage::getSingleton( 'core/session' )->setExpSecure ( false );
535
+ }
536
+ $this->saveLongToken($response->LongAccessToken);
537
+ return $response;
538
+ } catch (Exception $e) {
539
+ Mage::log($e->getMessage());
540
+ // return;
541
+ }
542
+ }
543
+
544
+ public function parsePrecheckoutXml() {
545
+ $typesXml = "";
546
+ $pairingData = $this->pairingDataTypes();
547
+ $pairingData = explode(',', $pairingData);
548
+
549
+ foreach ($pairingData as $dataType) {
550
+ $typesXml = $typesXml . sprintf("<PairingDataType><Type>%s</Type></PairingDataType>", $dataType);
551
+ }
552
+ $preCheckoutRequest = simplexml_load_string(sprintf("<PrecheckoutDataRequest><PairingDataTypes>%s</PairingDataTypes></PrecheckoutDataRequest>", $typesXml));
553
+
554
+ return $preCheckoutRequest->asXML();
555
+ }
556
+
557
+ // post transaction back to masterpass
558
+ public function postTransaction($post, $TransactionStatus, $ApprovalCode) {
559
+
560
+
561
+ $prefix = 'https://';
562
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
563
+ $prefix .= Mage::getStoreConfig('masterpass/config/environment') . ".";
564
+ }
565
+ $postbackUrl = $prefix . "api.mastercard.com/masterpass/" . $this->xmlVersion . "/transaction";
566
+
567
+ $sentXml = '';
568
+ try {
569
+ $consumerKey = $this->service->getConsumerKey();
570
+ $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
571
+ $nowString = date(DATE_ATOM);
572
+
573
+ $orderReference = Mage::getSingleton('checkout/session')->getLastRealOrderId();
574
+ $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
575
+ $order = Mage::getSingleton('sales/order');
576
+ $order->load($lastOrderId);
577
+ $_totalData = $order->getData();
578
+ $subTotal = $_totalData['grand_total'];
579
+
580
+ $subTotal = str_replace(',', '', $subTotal);
581
+ $orderAmount = $subTotal * 100;
582
+
583
+ //if expree checkout
584
+ if (Mage::getStoreConfig('masterpass/config/userflows') == self::EXPRESS_CHECKOUT) {
585
+ $expresschk = "true";
586
+ } else {
587
+ $expresschk = "false";
588
+ }
589
+
590
+ $sentXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><MerchantTransactions><MerchantTransactions><TransactionId>' . $post['transaction_id'] . '</TransactionId><ConsumerKey>' . $consumerKey . '</ConsumerKey><Currency>' . $currency . '</Currency><OrderAmount>' . $orderAmount . '</OrderAmount><PurchaseDate>' . $nowString . '</PurchaseDate><TransactionStatus>' . $TransactionStatus . '</TransactionStatus><ApprovalCode>' . $ApprovalCode . '</ApprovalCode><PreCheckoutTransactionId>' . $post['precheckout_id'] . '</PreCheckoutTransactionId>
591
+ <ExpressCheckoutIndicator>' . $expresschk . '</ExpressCheckoutIndicator></MerchantTransactions></MerchantTransactions>';
592
+ $resources = $this->service->PostCheckoutTransaction($postbackUrl, $sentXml);
593
+
594
+ Mage::getSingleton('checkout/session')->setPostBackMp(true);
595
+ } catch (Exception $e) {
596
+
597
+ Mage::log($e->getMessage());
598
+ Mage::getSingleton('checkout/session')->setPostBackMp(false);
599
+ return;
600
+ }
601
+
602
+ return $this->postback;
603
+ }
604
+
605
+ public function ifPostback() {
606
+ return Mage::getSingleton('checkout/session')->getPostBackMp();
607
+ }
608
+
609
+ public function getCardData($brandId) {
610
+
611
+
612
+ $cardType = null;
613
+ $cdata = null;
614
+ if ($brandId == null) {
615
+ $preData = Mage::getSingleton('checkout/session')->getMasterPassCard();
616
+ } else {
617
+ $preData = $brandId;
618
+ }
619
+ if ($preData['BrandId'] == self::DINERS) {
620
+ $cardType = 'OT';
621
+ } elseif ($preData['BrandId'] == self::MASTER) {
622
+ $cardType = 'MC';
623
+ } elseif ($preData['BrandId'] == self::VISA) {
624
+ $cardType = 'VI';
625
+ } elseif ($preData['BrandId'] == self::MAESTRO) {
626
+ $cardType = 'SM';
627
+ } elseif ($preData['BrandId'] == self::DISCOVER) {
628
+ $cardType = 'DI';
629
+ } elseif ($preData['BrandId'] == self::AMEX) {
630
+ $cardType = 'AE';
631
+ } else {
632
+ $cardType = 'OT';
633
+ }
634
+
635
+ $nameOnCard = $preData['CardHolderName'];
636
+ $ccNum = $preData['AccountNumber'];
637
+ $expMonth = $preData['ExpiryMonth'];
638
+ $expYear = $preData['ExpiryYear'];
639
+
640
+ $cdata = array(
641
+ 'method' => Mage::getStoreConfig('masterpass/config/paymentgateway'),
642
+ 'cc_owner' => $nameOnCard,
643
+ 'cc_type' => $cardType,
644
+ 'cc_number' => $ccNum,
645
+ 'cc_number_enc' => Mage::getSingleton('payment/info')->encrypt($ccNum),
646
+ 'cc_last4' => substr($ccNum, -4),
647
+ 'cc_exp_month' => $expMonth,
648
+ 'cc_exp_year' => $expYear
649
+ );
650
+ return $cdata;
651
+ }
652
+
653
+ }
app/code/local/MasterCard/Masterpass/Model/Masterpass.php ADDED
@@ -0,0 +1,175 @@
1
+ <?php
2
+
3
+ /*
4
+ * Developer: Pavith Lovan
5
+ */
6
+
7
+ class MasterCard_Masterpass_Model_Masterpass extends Mage_Core_Model_Abstract {
8
+
9
+ public function __construct() {
10
+ $this->_init('masterpass/masterpass');
11
+ parent::__construct();
12
+ }
13
+
14
+ public function getCustomerWallet() {
15
+ $masterPassData = Mage::getSingleton('checkout/session')->getMasterPassData();
16
+
17
+ return $masterPassData;
18
+ }
19
+
20
+ public function getCreditCardDetail() {
21
+ $arrayCard = array(
22
+ '0' => '2323'
23
+ );
24
+ $xmlCard = $this->getCustomerWallet();
25
+ $arrayCard = array(
26
+ 'BrandName' => (string) $xmlCard->Card->BrandName,
27
+ 'AccountNumber' => (string) $xmlCard->Card->AccountNumber,
28
+ 'CardHolderName' => (string) $xmlCard->Card->CardHolderName,
29
+ 'ExpiryMonth' => (string) $xmlCard->Card->ExpiryMonth,
30
+ 'ExpiryYear' => (string) $xmlCard->Card->ExpiryYear
31
+ );
32
+ $this->_masterPass = setBrandName((string) $xmlCard->Card->BrandName);
33
+ $this->_masterPass = setAccountNumber((string) $xmlCard->Card->AccountNumber);
34
+ $this->_masterPass = setCardHolderName((string) $xmlCard->Card->CardHolderName);
35
+ $this->_masterPass = setExpiryMonth((string) $xmlCard->Card->ExpiryMonth);
36
+ $this->_masterPass = setExpiryYear((string) $xmlCard->Card->ExpiryYear);
37
+ return $this->_masterPass;
38
+ }
39
+
40
+ public function getBrandName() {
41
+ $_Card = $this->getCustomerWallet();
42
+ try {
43
+ return (string) $_Card ['Card'] ['BrandName'];
44
+ } catch (Exception $e) {
45
+ return null;
46
+ }
47
+ }
48
+
49
+ public function getAccountNumber() {
50
+ $_Card = $this->getCustomerWallet();
51
+ try {
52
+ return (string) $_Card ['Card'] ['AccountNumber'];
53
+ } catch (Exception $e) {
54
+ return null;
55
+ }
56
+ }
57
+
58
+ public function getCardHolderName() {
59
+ $_Card = $this->getCustomerWallet();
60
+ try {
61
+ return (string) $_Card ['Card'] ['CardHolderName'];
62
+ } catch (Exception $e) {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ public function noExpirydate() {
68
+ $_Card = $this->getCustomerWallet();
69
+ if ($_Card ['Card'] ['ExpiryMonth'] == '') {
70
+ return false;
71
+ } else {
72
+ return true;
73
+ }
74
+ }
75
+
76
+ public function getExpiryMonth() {
77
+ $_Card = $this->getCustomerWallet();
78
+
79
+ try {
80
+ if ($_Card ['Card'] ['ExpiryMonth'] == '') {
81
+ $expiryMonth = '04';
82
+ } else {
83
+ $expiryMonth = $_Card ['Card'] ['ExpiryMonth'];
84
+ }
85
+ return (string) $expiryMonth;
86
+ } catch (Exception $e) {
87
+ return null;
88
+ }
89
+ }
90
+
91
+ public function getExpiryYear() {
92
+ $_Card = $this->getCustomerWallet();
93
+ try {
94
+ if ($_Card ['Card'] ['ExpiryYear'] == '') {
95
+ $expiryYear = date('Y') + 4;
96
+ } else {
97
+ $expiryYear = $_Card ['Card'] ['ExpiryYear'];
98
+ }
99
+ return (string) $expiryYear;
100
+ } catch (Exception $e) {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ public function getAutho() {
106
+ $autho = Mage::getSingleton('core/session');
107
+
108
+ return $autho->getAutho();
109
+ }
110
+
111
+ public function getRemoveDash($string) {
112
+ if (strstr($string, '-')) {
113
+ $master = explode('-', $string);
114
+ return $master [1];
115
+ } else {
116
+ return $string;
117
+ }
118
+ }
119
+
120
+ public function statesMapping($stateCode) {
121
+ $regionId = null;
122
+ if (preg_match('/-/', $stateCode)) {
123
+ $countryCode = explode('-', $stateCode);
124
+ $stateCode = $this->getRemoveDash($stateCode);
125
+ if ($countryCode [0]) {
126
+ $regionCollection = Mage::getModel('directory/region_api')->items($countryCode [0]);
127
+ foreach ($regionCollection as $region) {
128
+ if ($stateCode == $region ['code'] || $region['name'] == ucwords($stateCode)) {
129
+ $regionId = $region ['region_id'];
130
+ }
131
+ }
132
+ }
133
+ } else {
134
+ $regionId = '';
135
+ }
136
+
137
+ return $regionId;
138
+ }
139
+
140
+ //if not code try code
141
+ public function getMasterpassRegion($stateCode) {
142
+ $region = null;
143
+ if (preg_match('/-/', $stateCode)) {
144
+ $region = '';
145
+ } else {
146
+ $region = $stateCode;
147
+ }
148
+
149
+ return $region;
150
+ }
151
+
152
+ /**
153
+ * Update the shipping method set on the customer's quote.
154
+ *
155
+ * @param Mage_Sales_Model_Quote $quote
156
+ * @param string $methodCode
157
+ */
158
+ public function updateShippingMethod(Mage_Sales_Model_Quote $quote, $methodCode) {
159
+ Mage::dispatchEvent("mastercard_masterpass_update_shipping_method_before", array("quote" => $quote, "shipping_method" => $methodCode));
160
+
161
+ $changed = false;
162
+ if (!$quote->getIsVirtual() && $shippingAddress = $quote->getShippingAddress()) {
163
+ if ($methodCode != $shippingAddress->getShippingMethod()) {
164
+ $shippingAddress->setShippingMethod($methodCode)->setCollectShippingRates(true);
165
+ $quote->collectTotals()->save();
166
+ $changed = true;
167
+ }
168
+ }
169
+
170
+ Mage::dispatchEvent("mastsercard_masterpass_update_shipping_method_after", array("quote" => $quote, "shipping_method" => $methodCode, "changed" => $changed));
171
+ }
172
+
173
+ }
174
+
175
+ ?>
app/code/local/MasterCard/Masterpass/Model/Mpservice/Connector.php ADDED
@@ -0,0 +1,438 @@
1
+ <?php
2
+
3
+ class MasterCard_Masterpass_Model_Mpservice_Connector extends Mage_Core_Model_Abstract {
4
+
5
+ const AMP = "&";
6
+ const QUESTION = "?";
7
+ const EMPTY_STRING = "";
8
+ const EQUALS = "=";
9
+ const DOUBLE_QUOTE = '"';
10
+ const COMMA = ',';
11
+ const ENCODED_TILDE = '%7E';
12
+ const TILDE = '~';
13
+ const COLON = ':';
14
+ const SPACE = ' ';
15
+ const UTF_8 = 'UTF-8';
16
+ const V1 = 'v1';
17
+ const OAUTH_START_STRING = 'OAuth ';
18
+ const REALM = 'realm';
19
+ const ACCEPT = 'Accept';
20
+ const CONTENT_TYPE = 'Content-Type';
21
+ const SSL_CA_CER_PATH_LOCATION = '';
22
+ const POST = "POST";
23
+ const PUT = "PUT";
24
+ const GET = "GET";
25
+ const PKEY = 'pkey';
26
+ const STRNATCMP = "strnatcmp";
27
+ const SHA1 = "SHA1";
28
+ const APPLICATION_XML = "application/xml";
29
+ const AUTHORIZATION = "Authorization";
30
+ const OAUTH_BODY_HASH = "oauth_body_hash";
31
+ const BODY = "body";
32
+ const MESSAGE = 'Message';
33
+ // Signature Base String
34
+ const OAUTH_SIGNATURE = "oauth_signature";
35
+ const OAUTH_CONSUMER_KEY = 'oauth_consumer_key';
36
+ const OAUTH_NONCE = 'oauth_nonce';
37
+ const SIGNATURE_METHOD = 'oauth_signature_method';
38
+ const OAUTH_TIMESTAMP = 'oauth_timestamp';
39
+ const OAUTH_CALLBACK = "oauth_callback";
40
+ const OAUTH_SIGNATURE_METHOD = 'oauth_signature_method';
41
+ const OAUTH_VERSION = 'oauth_version';
42
+ // Srings to detect errors in the service calls
43
+ const ERRORS_TAG = "<Errors>";
44
+ const HTML_TAG = "<html>";
45
+ const HTML_BODY_OPEN = '<body>';
46
+ const HTML_BODY_CLOSE = '</body>';
47
+ // Error Messages
48
+ const EMPTY_REQUEST_TOKEN_ERROR_MESSAGE = 'Invalid Request Token';
49
+ const INVALID_AUTH_URL = 'Invalid Auth Url';
50
+ const POSTBACK_ERROR_MESSAGE = 'Postback Transaction Call was unsuccessful';
51
+ //Connection Strings
52
+ const CONTENT_TYPE_APPLICATION_XML = 'Content-Type: application/xml';
53
+ const SSL_ERROR_MESSAGE = "SSL Error Code: %s %sSSL Error Message: %s";
54
+
55
+ public $signatureBaseString;
56
+ public $authHeader;
57
+ protected $consumerKey;
58
+ private $privateKey;
59
+ public $keystorePath;
60
+ public $keystorePassword;
61
+ private $version = '1.0';
62
+ private $signatureMethod = 'RSA-SHA1';
63
+ public $realm = "eWallet"; // This value is static
64
+
65
+ /**
66
+ * Constructor for Connector
67
+ * @param string $consumerKey
68
+ * @param string $privateKey
69
+ */
70
+
71
+ public function __construct() {
72
+ $this->consumerKey = $this->getConsumerKey();
73
+ $this->privateKey = $this->getPrivateKey();
74
+ }
75
+
76
+ private function getPrivateKey() {
77
+ $keystore = $this->merchantPrivateKey();
78
+ if ($keystore['pkey']) {
79
+ return $keystore['pkey'];
80
+ } else {
81
+ Mage::log('Invalid private key password');
82
+ return null;
83
+ }
84
+ }
85
+ public function merchantPrivateKey() {
86
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
87
+ $thispath = Mage::getBaseDir() . "/shell/certs/" . Mage::getStoreConfig('masterpass/config/sbkeystorepath');
88
+ $keystorePassword = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbkeystorepassword'));
89
+ } else {
90
+ $thispath = Mage::getBaseDir() . "/shell/certs/" . Mage::getStoreConfig('masterpass/config/prkeystorepath');
91
+ $keystorePassword = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/prkeystorepassword'));
92
+ }
93
+ try {
94
+ if (strstr($keystorePassword, '.sh')) {
95
+ $key_pw = exec('sh ' . escapeshellarg($keystorePassword));
96
+ } elseif (strstr($keystorePassword, '.bat')) {
97
+ $key_pw = exec(escapeshellarg($keystorePassword));
98
+ }
99
+ $keystore = array();
100
+
101
+ $path = realpath($thispath);
102
+ //error_log("PATH IS " . $path);
103
+ $pkcs12 = file_get_contents($path);
104
+ trim(openssl_pkcs12_read($pkcs12, $keystore, $key_pw));
105
+ //error_log("GOT TO HERE - " . $keystore['pkey']);
106
+ } catch (Exception $e) {
107
+ Mage::log('Invalid private key password');
108
+ }
109
+ return $keystore;
110
+ }
111
+ public function getConsumerKey() {
112
+
113
+ if (Mage::getStoreConfig('masterpass/config/environment') === 'sandbox') {
114
+ $consumerKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/sbconsumerkey'));
115
+ } else {
116
+ $consumerKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('masterpass/config/prconsumerkey'));
117
+ }
118
+ return $consumerKey;
119
+ }
120
+
121
+ /**
122
+ * Method to convert strings 'true' and 'false' to a boolean value
123
+ * If parameter string is not 'true' (case insensitive), then false will be returned
124
+ *
125
+ * @param String : $str
126
+ *
127
+ * @return boolean
128
+ */
129
+ public static function str_to_bool($str) {
130
+ return (strcasecmp($str, TRUE) == 0) ? true : false;
131
+ }
132
+
133
+ public static function formatXML($resources) {
134
+
135
+ if ($resources != null) {
136
+ $resources = Mage::helper('masterpass')->formatXML($resources);
137
+ }
138
+ return $resources;
139
+ }
140
+
141
+ protected function doSimpleRequest($url, $requestMethod, $body = null) {
142
+ return self::doRequest(array(), $url, $requestMethod, $body);
143
+ }
144
+
145
+ /* * ************* Private Methods **************************************************************************************************************************** */
146
+
147
+ /**
148
+ * Method used for all Http connections
149
+ *
150
+ * @param $params
151
+ * @param $url
152
+ * @param $requestMethod
153
+ * @param $body
154
+ *
155
+ * @throws Exception - When connection error
156
+ *
157
+ * @return mixed - Raw data returned from the HTTP connection
158
+ */
159
+ public function doRequest($params, $url, $requestMethod, $body = null) {
160
+
161
+ if ($body != null) {
162
+ $params[self::OAUTH_BODY_HASH] = $this->generateBodyHash($body);
163
+ }
164
+
165
+ try {
166
+ return $this->connect($params, $this->realm, $url, $requestMethod, $body);
167
+ } catch (Exception $e) {
168
+ throw $this->checkForErrors($e);
169
+ }
170
+ }
171
+
172
+ /**
173
+ * SDK:
174
+ * Method to generate the body hash
175
+ *
176
+ * @param $body
177
+ *
178
+ * @return string
179
+ */
180
+ protected function generateBodyHash($body) {
181
+ $sha1Hash = sha1($body, true);
182
+ return base64_encode($sha1Hash);
183
+ }
184
+
185
+ /**
186
+ * This method generates and returns a unique nonce value to be used in
187
+ * Wallet API OAuth calls.
188
+ *
189
+ * @param $length
190
+ *
191
+ * @return string
192
+ */
193
+ private function generateNonce($length) {
194
+ if (function_exists('com_create_guid') === true) {
195
+ return trim(com_create_guid(), '{}');
196
+ } else {
197
+ $u = md5(uniqid('nonce_', true));
198
+ return substr($u, 0, $length);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Builds a Auth Header used in connection to MasterPass services
204
+ *
205
+ * @param $params
206
+ * @param $realm
207
+ * @param $url
208
+ * @param $requestMethod
209
+ * @param $body
210
+ *
211
+ * @return string - Auth header
212
+ */
213
+ private function buildAuthHeaderString($params, $realm, $url, $requestMethod, $body) {
214
+
215
+ $params = array_merge($this->OAuthParametersFactory(), $params);
216
+
217
+ $signature = $this->generateAndSignSignature($params, $url, $requestMethod, $this->privateKey, $body);
218
+ $params[self::OAUTH_SIGNATURE] = $signature;
219
+
220
+ $startString = self::OAUTH_START_STRING;
221
+ if (!empty($realm)) {
222
+ $startString = $startString . self::REALM . self::EQUALS . self::DOUBLE_QUOTE . $realm . self::DOUBLE_QUOTE . self::COMMA;
223
+ }
224
+
225
+ foreach ($params as $key => $value) {
226
+ $startString = $startString . $key . self::EQUALS . self::DOUBLE_QUOTE . $this->RFC3986urlencode($value) . self::DOUBLE_QUOTE . self::COMMA;
227
+ }
228
+
229
+ $this->authHeader = substr($startString, 0, strlen($startString) - 1);
230
+
231
+ return $this->authHeader;
232
+ }
233
+
234
+ /**
235
+ * Method to generate base string and generate the signature
236
+ *
237
+ * @param $params
238
+ * @param $url
239
+ * @param $requestMethod
240
+ * @param $privateKey
241
+ * @param $body
242
+ *
243
+ * @return string
244
+ */
245
+ private function generateAndSignSignature($params, $url, $requestMethod, $privateKey, $body) {
246
+ $baseString = $this->generateBaseString($params, $url, $requestMethod);
247
+ $this->signatureBaseString = $baseString;
248
+
249
+ $signature = $this->sign($baseString, $privateKey);
250
+
251
+ return $signature;
252
+ }
253
+
254
+ /**
255
+ * Method to sign string
256
+ *
257
+ * @param $string
258
+ * @param $privateKey
259
+ *
260
+ * @return string
261
+ */
262
+ private function sign($string, $privateKey) {
263
+ $privatekeyid = openssl_get_privatekey($privateKey);
264
+ openssl_sign($string, $signature, $privatekeyid, OPENSSL_ALGO_SHA1);
265
+
266
+ return base64_encode($signature);
267
+ }
268
+
269
+ /**
270
+ * Method to generate the signature base string
271
+ *
272
+ * @param $params
273
+ * @param $url
274
+ * @param $requestMethod
275
+ *
276
+ * @return string
277
+ */
278
+ private function generateBaseString($params, $url, $requestMethod) {
279
+ $urlMap = parse_url($url);
280
+
281
+ $url = $this->formatUrl($url, $params);
282
+
283
+ $params = $this->parseUrlParameters($urlMap, $params);
284
+
285
+
286
+ $baseString = strtoupper($requestMethod) . self::AMP . $this->RFC3986urlencode($url) . self::AMP;
287
+ ksort($params);
288
+
289
+ $parameters = self::EMPTY_STRING;
290
+ foreach ($params as $key => $value) {
291
+ $parameters = $parameters . $key . self::EQUALS . $this->RFC3986urlencode($value) . self::AMP;
292
+ }
293
+ $parameters = $this->RFC3986urlencode(substr($parameters, 0, strlen($parameters) - 1));
294
+ return $baseString . $parameters;
295
+ }
296
+
297
+ /**
298
+ * Method to extract the URL parameters and add them to the params array
299
+ *
300
+ * @param string $urlMap
301
+ * @param string $params
302
+ *
303
+ * @return string|multitype:
304
+ */
305
+ function parseUrlParameters($urlMap, $params) {
306
+ if (empty($urlMap['query'])) {
307
+ return $params;
308
+ } else {
309
+ $str = $urlMap['query'];
310
+ parse_str($str, $urlParamsArray);
311
+ foreach ($urlParamsArray as $key => $value) {
312
+ $urlParamsArray[$key] = $this->RFC3986urlencode($value);
313
+ }
314
+ return array_merge($params, $urlParamsArray);
315
+ }
316
+ }
317
+
318
+ /**
319
+ * Method to format the URL that is included in the signature base string
320
+ *
321
+ * @param string $url
322
+ * @param string $params
323
+ *
324
+ * @return string|string
325
+ */
326
+ function formatUrl($url, $params) {
327
+ if (!parse_url($url)) {
328
+ return $url;
329
+ }
330
+ $urlMap = parse_url($url);
331
+ return $urlMap['scheme'] . '://' . $urlMap['host'] . $urlMap['path'];
332
+ }
333
+
334
+ /**
335
+ * URLEncoder that conforms to the RFC3986 spec.
336
+ * PHP's internal function, rawurlencode, does not conform to RFC3986 for PHP 5.2
337
+ *
338
+ * @param unknown $string
339
+ *
340
+ * @return unknown|mixed
341
+ */
342
+ function RFC3986urlencode($string) {
343
+ if ($string === false) {
344
+ return $string;
345
+ } else {
346
+ return str_replace(self::ENCODED_TILDE, self::TILDE, rawurlencode($string));
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Method to create all default parameters used in the base string and auth header
352
+ *
353
+ * @return array