AMG_SecurePay - Version 0.1.0

Version Notes

AMG_SecurePay

Download this release

Release Info

Developer AMG
Extension AMG_SecurePay
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

Files changed (26) hide show
  1. app/code/community/AMG/SecurePay/Block/Customer/Storedcard.php +30 -0
  2. app/code/community/AMG/SecurePay/Block/Directpost/Customervault/Form.php +20 -0
  3. app/code/community/AMG/SecurePay/Block/Directpost/Form.php +14 -0
  4. app/code/community/AMG/SecurePay/Helper/Data.php +162 -0
  5. app/code/community/AMG/SecurePay/Model/Api.php +371 -0
  6. app/code/community/AMG/SecurePay/Model/Customervault.php +62 -0
  7. app/code/community/AMG/SecurePay/Model/Directpost.php +163 -0
  8. app/code/community/AMG/SecurePay/Model/Directpost/Customervault.php +112 -0
  9. app/code/community/AMG/SecurePay/Model/Directpost/Customervault/Source/PaymentAction.php +25 -0
  10. app/code/community/AMG/SecurePay/Model/Directpost/Source/PaymentAction.php +25 -0
  11. app/code/community/AMG/SecurePay/Model/Mysql4/Customervault.php +52 -0
  12. app/code/community/AMG/SecurePay/Model/Mysql4/Customervault/Collection.php +9 -0
  13. app/code/community/AMG/SecurePay/Model/Observer.php +104 -0
  14. app/code/community/AMG/SecurePay/controllers/StoredcardController.php +40 -0
  15. app/code/community/AMG/SecurePay/etc/config.xml +131 -0
  16. app/code/community/AMG/SecurePay/etc/system.xml +246 -0
  17. app/code/community/AMG/SecurePay/sql/securepay_setup/mysql4-install-0.1.0.php +32 -0
  18. app/design/adminhtml/default/default/template/AMG/securepay/directpost/customervault/form.phtml +19 -0
  19. app/design/adminhtml/default/default/template/AMG/securepay/directpost/form.phtml +67 -0
  20. app/design/frontend/base/default/layout/AMG/securepay.xml +19 -0
  21. app/design/frontend/base/default/template/AMG/securepay/customer/storedcard.phtml +39 -0
  22. app/design/frontend/base/default/template/AMG/securepay/directpost/customervault/form.phtml +19 -0
  23. app/design/frontend/base/default/template/AMG/securepay/directpost/form.phtml +98 -0
  24. app/etc/modules/AMG_SecurePay.xml +9 -0
  25. app/locale/en_US/AMG/SecurePay.csv +1 -0
  26. package.xml +18 -0
app/code/community/AMG/SecurePay/Block/Customer/Storedcard.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Block_Customer_Storedcard extends Mage_Core_Block_Template
3
+ {
4
+ protected function _prepareLayout()
5
+ {
6
+ parent::_prepareLayout();
7
+ if($head = $this->getLayout()->getBlock('head')){
8
+ $head->setTitle($this->__('My Credit Cards'));
9
+ }
10
+ }
11
+
12
+ public function getStoredCards()
13
+ {
14
+ if(Mage::getSingleton('customer/session')->isLoggedIn()){
15
+ $loggedInCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
16
+ if($loggedInCustomerId>0){
17
+ $collection = Mage::getModel('securepay/customervault')->getCollection()
18
+ ->addFieldToFilter('customer_id',$loggedInCustomerId);
19
+ if(count($collection)>0)
20
+ return $collection;
21
+ }
22
+ }
23
+ return array();
24
+ }
25
+
26
+ public function getDeleteUrl($storedCard)
27
+ {
28
+ return $this->getUrl('*/*/delete', array('c'=>$storedCard->getCode()));
29
+ }
30
+ }
app/code/community/AMG/SecurePay/Block/Directpost/Customervault/Form.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Block_Directpost_Customervault_Form extends Mage_Payment_Block_Form_Cc
3
+ {
4
+ protected function _construct()
5
+ {
6
+ parent::_construct();
7
+ $this->setTemplate('AMG/securepay/directpost/customervault/form.phtml');
8
+
9
+ }
10
+
11
+ public function getCustomerVaultsCollection()
12
+ {
13
+ $customerId = Mage::helper('securepay')->getCurrentQuoteCustomerId();
14
+ if($customerId>0){
15
+ return Mage::getModel('securepay/customervault')->getCollection()
16
+ ->addFieldToFilter('customer_id',$customerId);
17
+ }
18
+ return array();
19
+ }
20
+ }
app/code/community/AMG/SecurePay/Block/Directpost/Form.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Block_Directpost_Form extends Mage_Payment_Block_Form_Cc
3
+ {
4
+ protected function _construct()
5
+ {
6
+ parent::_construct();
7
+ $this->setTemplate('AMG/securepay/directpost/form.phtml');
8
+ }
9
+
10
+ public function isCcSaveAllowed()
11
+ {
12
+ return Mage::helper('securepay')->isCcSaveAllowed();
13
+ }
14
+ }
app/code/community/AMG/SecurePay/Helper/Data.php ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+ const TYPE_SALE = 'sale';
5
+ const TYPE_AUTHORIZATION = 'auth';
6
+ const TYPE_CAPTURE = 'capture';
7
+ const TYPE_REFUND = 'refund';
8
+ const TYPE_ADD_CUSTOMER_VAULT = 'add_customer';
9
+ const TYPE_UPDATE_CUSTOMER_VAULT = 'update_customer';
10
+
11
+ const DEFAULT_DUP_SECONDS = 0;
12
+ const DEFAULT_AMG_PAYMENT_TYPE = 'creditcard';
13
+
14
+ protected $_ccTypes = array();
15
+
16
+ public function isPaymentMethodAvailable()
17
+ {
18
+ $apiId = trim($this->getAmgApiId());
19
+ $transKey = trim($this->getAmgTransKey());
20
+ $gatewayUrl = trim($this->getAmgGatewayUrl());
21
+
22
+ if($apiId!='' && $transKey!='' && $gatewayUrl!='')
23
+ return true;
24
+
25
+ return false;
26
+ }
27
+
28
+ public function getAmgApiId()
29
+ {
30
+ return Mage::getStoreConfig('payment/securepay_directpost/api_id');
31
+ }
32
+
33
+ public function getAmgTransKey()
34
+ {
35
+ return Mage::getStoreConfig('payment/securepay_directpost/trans_key');
36
+ }
37
+
38
+ public function getAmgGatewayUrl()
39
+ {
40
+ return Mage::getStoreConfig('payment/securepay_directpost/gateway_url');
41
+ }
42
+
43
+ public function getUseCcv()
44
+ {
45
+ return Mage::getStoreConfigFlag('payment/securepay_directpost/useccv');
46
+ }
47
+
48
+ public function isLogEnabled()
49
+ {
50
+ return Mage::getStoreConfigFlag('payment/securepay_directpost/log');
51
+ }
52
+
53
+ public function getDuplicateTransSeconds()
54
+ {
55
+ if(Mage::getStoreConfig('payment/securepay_directpost/dup_seconds'))
56
+ return Mage::getStoreConfig('payment/securepay_directpost/dup_seconds');
57
+ return self::DEFAULT_DUP_SECONDS;
58
+ }
59
+
60
+ public function getDuplicateTransSecondsFeatureEnabled()
61
+ {
62
+ return Mage::getStoreConfigFlag('payment/securepay_directpost/check_dup_seconds');
63
+ }
64
+
65
+ public function getApiPaymentType()
66
+ {
67
+ return self::DEFAULT_AMG_PAYMENT_TYPE;
68
+ }
69
+
70
+ public function getApiAuthorizationType()
71
+ {
72
+ return self::TYPE_AUTHORIZATION;
73
+ }
74
+
75
+ public function getAddCustomerVaultType()
76
+ {
77
+ return self::TYPE_ADD_CUSTOMER_VAULT;
78
+ }
79
+
80
+ public function getUpdateCustomerVaultType()
81
+ {
82
+ return self::TYPE_UPDATE_CUSTOMER_VAULT;
83
+ }
84
+
85
+ public function getApiCaptureType()
86
+ {
87
+ return self::TYPE_CAPTURE;
88
+ }
89
+
90
+ public function getApiRefundType()
91
+ {
92
+ return self::TYPE_REFUND;
93
+ }
94
+
95
+ public function getApiSaleType()
96
+ {
97
+ return self::TYPE_SALE;
98
+ }
99
+
100
+ public function getPaymentMethodAdditionalInfoFields()
101
+ {
102
+ return array('cc_save_future_directpost','customervault_id','amg_customer_vault_id');
103
+ }
104
+
105
+ public function getLogDir()
106
+ {
107
+ return Mage::getBaseDir().DS.'var'.DS.'log'.DS.'AMG'.DS.'SecurePay';
108
+ }
109
+
110
+ protected function _getCcTypes()
111
+ {
112
+ if(empty($this->_ccTypes)){
113
+ $this->_ccTypes = Mage::getSingleton('payment/config')->getCcTypes();
114
+ }
115
+
116
+ return $this->_ccTypes;
117
+ }
118
+
119
+ public function translateCcType($ccTypeShort)
120
+ {
121
+ $ccTypes = $this->_getCcTypes();
122
+ if ( !empty($ccTypes) && $ccTypeShort && isset($ccTypes[$ccTypeShort]) ) {
123
+ return $ccTypes[$ccTypeShort];
124
+ }
125
+
126
+ return '';
127
+ }
128
+
129
+ public function getCurrentQuoteCustomerId()
130
+ {
131
+ if(Mage::getSingleton('admin/session')->isLoggedIn()){
132
+ $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();
133
+ if($quote){
134
+ if($quote->getId()){
135
+ if($quote->getCustomerId()){
136
+ return $quote->getCustomerId();
137
+ }
138
+ }
139
+ }
140
+ }
141
+ else{
142
+ if(Mage::getSingleton('customer/session')->isLoggedIn()){
143
+ $loggedInCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
144
+ if($loggedInCustomerId>0)
145
+ return $loggedInCustomerId;
146
+ }
147
+ }
148
+
149
+ return 0;
150
+ }
151
+
152
+ public function isCcSaveAllowed()
153
+ {
154
+ if(Mage::getStoreConfigFlag('payment/securepay_directpost/allowsavecc')){
155
+ $customerId = $this->getCurrentQuoteCustomerId();
156
+ if($customerId>0)
157
+ return true;
158
+ }
159
+ return false;
160
+ }
161
+ }
162
+
app/code/community/AMG/SecurePay/Model/Api.php ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Api extends Mage_Core_Model_Abstract
3
+ {
4
+ protected $_helper;
5
+ protected $_payment;
6
+ protected $_order;
7
+ protected $_amount;
8
+
9
+ public function _construct()
10
+ {
11
+ parent::_construct();
12
+ $this->_helper = Mage::helper('securepay');
13
+ }
14
+
15
+ public function isPaymentMethodAvailable()
16
+ {
17
+ return $this->_helper->isPaymentMethodAvailable();
18
+ }
19
+
20
+ public function setPayment(Varien_Object $payment)
21
+ {
22
+ if($payment){
23
+ $this->_payment = $payment;
24
+ $order = $payment->getOrder();
25
+ if($order && $order->getId()){
26
+ $this->_order = $order;
27
+ }
28
+ }
29
+ }
30
+
31
+ public function setAmount($amount)
32
+ {
33
+ $this->_amount = $amount;
34
+ }
35
+
36
+ protected function getBillingAddress()
37
+ {
38
+ $billingAddressArr['firstname'] = '';
39
+ $billingAddressArr['lastname'] = '';
40
+ $billingAddressArr['company'] = '';
41
+ $billingAddressArr['address1'] = '';
42
+ $billingAddressArr['address2'] = '';
43
+ $billingAddressArr['city'] = '';
44
+ $billingAddressArr['state'] = '';
45
+ $billingAddressArr['zip'] = '';
46
+ $billingAddressArr['country'] = '';
47
+ $billingAddressArr['phone'] = '';
48
+ $billingAddressArr['fax'] = '';
49
+ $billingAddressArr['email'] = '';
50
+
51
+ if($this->_order){
52
+ $billingAddress = $this->_order->getBillingAddress();
53
+ if($billingAddress && $billingAddress->getId()){
54
+ $billingAddressArr['firstname'] = $billingAddress->getData('firstname');
55
+ $billingAddressArr['lastname'] = $billingAddress->getData('lastname');
56
+ $billingAddressArr['company'] = $billingAddress->getData('company');
57
+ $billingAddressArr['address1'] = $billingAddress->getStreet1();
58
+ $billingAddressArr['address2'] = $billingAddress->getStreet2();
59
+ $billingAddressArr['city'] = $billingAddress->getData('city');
60
+ $billingAddressArr['state'] = $billingAddress->getData('region');
61
+ $billingAddressArr['zip'] = $billingAddress->getData('postcode');
62
+ $billingAddressArr['country'] = $billingAddress->getData('country_id');
63
+ $billingAddressArr['phone'] = $billingAddress->getData('telephone');
64
+ $billingAddressArr['fax'] = $billingAddress->getData('fax');
65
+ $billingAddressArr['email'] = $billingAddress->getData('email');
66
+ }
67
+ }
68
+
69
+ return $billingAddressArr;
70
+ }
71
+
72
+ protected function getShippingAddress()
73
+ {
74
+ $shippingAddressArr['shipping_firstname'] = '';
75
+ $shippingAddressArr['shipping_lastname'] = '';
76
+ $shippingAddressArr['shipping_company'] = '';
77
+ $shippingAddressArr['shipping_address1'] = '';
78
+ $shippingAddressArr['shipping_address2'] = '';
79
+ $shippingAddressArr['shipping_city'] = '';
80
+ $shippingAddressArr['shipping_state'] = '';
81
+ $shippingAddressArr['shipping_zip'] = '';
82
+ $shippingAddressArr['shipping_country'] = '';
83
+ $shippingAddressArr['shipping_phone'] = '';
84
+ $shippingAddressArr['shipping_fax'] = '';
85
+ $shippingAddressArr['shipping_email'] = '';
86
+
87
+ if($this->_order){
88
+ if(!$this->_order->getIsVirtual()){
89
+ $shippingAddress = $this->_order->getShippingAddress();
90
+ }
91
+ else{
92
+ $shippingAddress = $this->_order->getBillingAddress();
93
+ }
94
+
95
+ if($shippingAddress && $shippingAddress->getId()){
96
+ $shippingAddressArr['shipping_firstname'] = $shippingAddress->getData('firstname');
97
+ $shippingAddressArr['shipping_lastname'] = $shippingAddress->getData('lastname');
98
+ $shippingAddressArr['shipping_company'] = $shippingAddress->getData('company');
99
+ $shippingAddressArr['shipping_address1'] = $shippingAddress->getStreet1();
100
+ $shippingAddressArr['shipping_address2'] = $shippingAddress->getStreet2();
101
+ $shippingAddressArr['shipping_city'] = $shippingAddress->getData('city');
102
+ $shippingAddressArr['shipping_state'] = $shippingAddress->getData('region');
103
+ $shippingAddressArr['shipping_zip'] = $shippingAddress->getData('postcode');
104
+ $shippingAddressArr['shipping_country'] = $shippingAddress->getData('country_id');
105
+ $shippingAddressArr['shipping_phone'] = $shippingAddress->getData('telephone');
106
+ $shippingAddressArr['shipping_fax'] = $shippingAddress->getData('fax');
107
+ $shippingAddressArr['shipping_email'] = $shippingAddress->getData('email');
108
+ }
109
+ }
110
+
111
+ return $shippingAddressArr;
112
+ }
113
+
114
+ protected function getCreditCardData()
115
+ {
116
+ $creditCardArr['ccnumber'] = '';
117
+ $creditCardArr['ccexp'] = '';
118
+ $creditCardArr['cvv'] = '';
119
+
120
+ if($this->_payment){
121
+ $creditCardArr['ccnumber'] = $this->_payment->getCcNumber();
122
+ $creditCardArr['ccexp'] = $this->_payment->getCcExpMonth().'-'.$this->_payment->getCcExpYear();
123
+ if($this->_helper->getUseCcv())
124
+ $creditCardArr['cvv'] = $this->_payment->getCcCid();
125
+ }
126
+
127
+ return $creditCardArr;
128
+ }
129
+
130
+ protected function getApiCredentials()
131
+ {
132
+ $apiCredentialsArr['username'] = $this->_helper->getAmgApiId();
133
+ $apiCredentialsArr['password'] = $this->_helper->getAmgTransKey();
134
+
135
+ return $apiCredentialsArr;
136
+ }
137
+
138
+ protected function getOrderAmounts()
139
+ {
140
+ $orderAmounts['shipping'] = 0;
141
+ $orderAmounts['discount_amount'] = 0;
142
+ $orderAmounts['tax'] = 0;
143
+
144
+ if($this->_payment && $this->_order){
145
+ $orderAmounts['shipping'] = $this->_order->getShippingAmount();
146
+ $orderAmounts['discount_amount'] = $this->_order->getDiscountAmount();
147
+ $orderAmounts['tax'] = $this->_order->getTaxAmount();
148
+ }
149
+
150
+ return $orderAmounts;
151
+ }
152
+
153
+ protected function getApiCommonFields()
154
+ {
155
+ $commonFieldsArr['amount'] = '';
156
+ $commonFieldsArr['ipaddress'] = '';
157
+ $commonFieldsArr['orderid'] = '';
158
+ $commonFieldsArr['merchant_ref_number'] = '';
159
+ $commonFieldsArr['currency'] = '';
160
+ $commonFieldsArr['payment'] = $this->_helper->getApiPaymentType();
161
+
162
+ if($this->_helper->getDuplicateTransSecondsFeatureEnabled())
163
+ $commonFieldsArr['dup_seconds'] = $this->_helper->getDuplicateTransSeconds();
164
+
165
+ if($this->_payment && $this->_order){
166
+ $commonFieldsArr['amount'] = $this->_amount;
167
+ $commonFieldsArr['ipaddress'] = $_SERVER['REMOTE_ADDR'];
168
+ $commonFieldsArr['orderid'] = $this->_order->getIncrementId();
169
+ $commonFieldsArr['merchant_ref_number'] = $this->_order->getIncrementId();
170
+ $commonFieldsArr['currency'] = $this->_order->getBaseCurrencyCode();
171
+ }
172
+
173
+ return $commonFieldsArr;
174
+ }
175
+
176
+ protected function getApiCustomerValutFields()
177
+ {
178
+
179
+ $customerVault = $this->getCustomerVaultIfApplicable();
180
+ if($customerVault){
181
+ $customerValutFieldsArr['customer_vault'] = $this->_helper->getAddCustomerVaultType();
182
+ if($customerVault->getAmgCustomerVaultId()){
183
+ $customerValutFieldsArr['customer_vault'] = $this->_helper->getUpdateCustomerVaultType();
184
+ $customerValutFieldsArr['customer_vault_id'] = $customerVault->getAmgCustomerVaultId();
185
+ $customerValutFieldsArr['customer_id'] = $customerVault->getAmgCustomerVaultId();
186
+ }
187
+ return $customerValutFieldsArr;
188
+ }
189
+ return array();
190
+ }
191
+
192
+ public function getAuthorizationRequest()
193
+ {
194
+ $billingAddressArr = $this->getBillingAddress();
195
+ $shippingAddressArr = $this->getShippingAddress();
196
+ $creditCardArr = $this->getCreditCardData();
197
+ $apiCredentialsArr = $this->getApiCredentials();
198
+ $orderAmounts = $this->getOrderAmounts();
199
+ $commonFieldsArr = $this->getApiCommonFields();
200
+ $customerValutFieldsArr = $this->getApiCustomerValutFields();
201
+
202
+ $authorizeRequest = array_merge($billingAddressArr,$shippingAddressArr,$creditCardArr,$apiCredentialsArr,$orderAmounts,$commonFieldsArr,$customerValutFieldsArr);
203
+
204
+ $authorizeRequest['type'] = $this->_helper->getApiAuthorizationType();
205
+
206
+ return $authorizeRequest;
207
+ }
208
+
209
+ public function getSaleRequest()
210
+ {
211
+ $saleRequest = $this->getAuthorizationRequest();
212
+
213
+ $saleRequest['type'] = $this->_helper->getApiSaleType();
214
+
215
+ return $saleRequest;
216
+ }
217
+
218
+
219
+ public function getCustomerVaultAuthorizationRequest($amgCustomerVaultId)
220
+ {
221
+ $apiCredentialsArr = $this->getApiCredentials();
222
+
223
+ $commonFieldsArr['amount'] = $this->_amount;
224
+ $commonFieldsArr['customer_vault_id'] = $amgCustomerVaultId;
225
+ $commonFieldsArr['orderid'] = $this->_order->getIncrementId();
226
+ $commonFieldsArr['merchant_ref_number'] = $this->_order->getIncrementId();
227
+
228
+ $authorizeRequest = array_merge($apiCredentialsArr,$commonFieldsArr);
229
+
230
+ $authorizeRequest['type'] = $this->_helper->getApiAuthorizationType();
231
+
232
+ return $authorizeRequest;
233
+ }
234
+
235
+ public function getCustomerVaultSaleRequest($amgCustomerVaultId)
236
+ {
237
+ $saleRequest = $this->getCustomerVaultAuthorizationRequest($amgCustomerVaultId);
238
+
239
+ $saleRequest['type'] = $this->_helper->getApiSaleType();
240
+
241
+ return $saleRequest;
242
+ }
243
+
244
+ public function getCaptureRequest($transactionId)
245
+ {
246
+ $apiCredentialsArr = $this->getApiCredentials();
247
+
248
+ $commonFieldsArr['amount'] = $this->_amount;
249
+ $commonFieldsArr['transactionid'] = $transactionId;
250
+ $commonFieldsArr['orderid'] = $this->_order->getIncrementId();
251
+
252
+ $captureRequest = array_merge($apiCredentialsArr,$commonFieldsArr);
253
+
254
+ $captureRequest['type'] = $this->_helper->getApiCaptureType();
255
+
256
+ return $captureRequest;
257
+ }
258
+
259
+ public function getRefundRequest()
260
+ {
261
+ $apiCredentialsArr = $this->getApiCredentials();
262
+
263
+ $commonFieldsArr['amount'] = $this->_amount;
264
+ $commonFieldsArr['transactionid'] = $this->_payment->getLastTransId();
265
+
266
+ $refundRequest = array_merge($apiCredentialsArr,$commonFieldsArr);
267
+
268
+ $refundRequest['type'] = $this->_helper->getApiRefundType();
269
+
270
+ return $refundRequest;
271
+ }
272
+
273
+ protected function getCustomerVaultIfApplicable()
274
+ {
275
+ $paymentMethodCode = $this->_payment->getMethod();
276
+ $directPostPaymentMethodCode = Mage::getModel('securepay/directpost')->getCode();
277
+ if($paymentMethodCode==$directPostPaymentMethodCode){
278
+ if(Mage::helper('securepay')->isCcSaveAllowed()){
279
+ if($this->_payment->hasAdditionalInformation('cc_save_future_directpost') && ($this->_payment->getAdditionalInformation('cc_save_future_directpost') == 'Y')) {
280
+ $customerVaultModel = Mage::getModel('securepay/customervault');
281
+ $customerVaultModel->loadByCreditCard(
282
+ $this->_payment->getOrder()->getData('customer_id'),
283
+ $this->_payment->getData('cc_type'),
284
+ $this->_payment->getData('cc_last4'),
285
+ $this->_payment->getData('cc_exp_month'),
286
+ $this->_payment->getData('cc_exp_year')
287
+ );
288
+ return $customerVaultModel;
289
+ }
290
+ }
291
+ }
292
+ return false;
293
+ }
294
+
295
+ public function _postApiRequest($requestArr)
296
+ {
297
+ $this->log($requestArr);
298
+
299
+ $requestString = "";
300
+ foreach($requestArr as $key=>$value) {
301
+ $requestString .= $key.'='. urlencode($value).'&';
302
+ }
303
+ $requestString = substr($requestString,0,-1);
304
+
305
+ $gatewayUrl = $this->_helper->getAmgGatewayUrl();
306
+
307
+ $ch = curl_init();
308
+ curl_setopt($ch, CURLOPT_URL,$gatewayUrl);
309
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
310
+ curl_setopt($ch, CURLOPT_TIMEOUT, 30);
311
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
312
+ curl_setopt($ch, CURLOPT_HEADER, 0);
313
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
314
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $requestString);
315
+ curl_setopt($ch, CURLOPT_POST, 1);
316
+ if (!($data = curl_exec($ch))) {
317
+ Mage::throwException(Mage::helper('securepay')->__('Payment Gateway Request Timeout.'));
318
+ }
319
+ curl_close($ch);
320
+ unset($ch);
321
+
322
+ $responseArr = array();
323
+ $data = explode("&",$data);
324
+ for($i=0;$i<count($data);$i++) {
325
+ $rdata = explode("=",$data[$i]);
326
+ $responseArr[$rdata[0]] = $rdata[1];
327
+ }
328
+
329
+ $this->log($responseArr);
330
+
331
+ return $responseArr;
332
+ }
333
+
334
+ protected function log($message)
335
+ {
336
+ if($this->_helper->isLogEnabled()){
337
+ $orderNumber = $this->_order->getIncrementId();
338
+ $file = $orderNumber.'.log';
339
+
340
+ if($message && $orderNumber){
341
+ $logDir = $this->_helper->getLogDir();
342
+ $logFile = $logDir . DS . $file;
343
+ $level = Zend_Log::DEBUG;
344
+ if(!is_dir($logDir)){
345
+ mkdir($logDir);
346
+ chmod($logDir, 0777);
347
+ }
348
+ if(!file_exists($logFile)){
349
+ file_put_contents($logFile, '');
350
+ chmod($logFile, 0777);
351
+ }
352
+ $currentTime = Mage::getModel('core/date')->date('Y-m-d H:i:s');
353
+ $format = "$currentTime %priorityName% (%priority%) : %message%" . PHP_EOL;
354
+ $formatter = new Zend_Log_Formatter_Simple($format);
355
+ $writerModel = (string)Mage::getConfig()->getNode('global/log/core/writer_model');
356
+ if(!$writerModel){
357
+ $writer = new Zend_Log_Writer_Stream($logFile);
358
+ }
359
+ else{
360
+ $writer = new $writerModel($logFile);
361
+ }
362
+ $writer->setFormatter($formatter);
363
+ $logger = new Zend_Log($writer);
364
+ if(is_array($message) || is_object($message)){
365
+ $message = print_r($message, true);
366
+ }
367
+ $logger->log($message, $level);
368
+ }
369
+ }
370
+ }
371
+ }
app/code/community/AMG/SecurePay/Model/Customervault.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Customervault extends Mage_Core_Model_Abstract
3
+ {
4
+ public function __construct()
5
+ {
6
+ $this->_init('securepay/customervault');
7
+ }
8
+
9
+ public function loadByCreditCard($customerId, $ccType, $ccLast4, $ccExpMonth, $ccExpYear)
10
+ {
11
+ if($customerId && $ccType && $ccLast4 && $ccExpMonth && $ccExpYear){
12
+ $customerVaultId = $this->_getResource()->getCustomerVaultIdByCreditCard($customerId, $ccType, $ccLast4, $ccExpMonth, $ccExpYear);
13
+ if($customerVaultId>0){
14
+ $this->load($customerVaultId);
15
+ }
16
+ }
17
+ return $this;
18
+ }
19
+
20
+ public function loadByCode($code)
21
+ {
22
+ if($code){
23
+ $customerVaultId = $this->_getResource()->getCustomerVaultIdByCode($code);
24
+ if($customerVaultId>0){
25
+ $this->load($customerVaultId);
26
+ }
27
+ }
28
+ return $this;
29
+ }
30
+
31
+ public function generateUniqueCode()
32
+ {
33
+ return $this->_getResource()->generateUniqueCode();
34
+ }
35
+
36
+ protected function _beforeSave()
37
+ {
38
+ parent::_beforeSave();
39
+ if(!$this->getId()){
40
+ $this->_dataSaveAllowed = false;
41
+
42
+ $customerId = $this->getData('customer_id');
43
+ $code = $this->getData('code');
44
+ $ccType = $this->getData('cc_type');
45
+ $ccLast4 = $this->getData('cc_last4');
46
+ $ccExpMonth = $this->getData('cc_exp_month');
47
+ $ccExpYear = $this->getData('cc_exp_year');
48
+ $amgCustomerVaultId = $this->getData('amg_customer_vault_id');
49
+
50
+ if($customerId && $code && $ccType && $ccLast4 && $ccExpMonth && $ccExpYear && $amgCustomerVaultId){
51
+ $customerVaultId = $this->_getResource()->getCustomerVaultIdByCreditCard($customerId, $ccType, $ccLast4, $ccExpMonth, $ccExpYear);
52
+ if($customerVaultId==0){
53
+ $customerVaultId = $this->_getResource()->getCustomerVaultIdByCode($code);
54
+ if($customerVaultId==0)
55
+ $this->_dataSaveAllowed = true;
56
+ }
57
+ }
58
+
59
+ }
60
+ return $this;
61
+ }
62
+ }
app/code/community/AMG/SecurePay/Model/Directpost.php ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Directpost extends Mage_Payment_Model_Method_Cc
3
+ {
4
+ protected $_code = 'securepay_directpost';
5
+ protected $_formBlockType = 'securepay/directpost_form';
6
+
7
+ protected $_canAuthorize = true;
8
+ protected $_canCapture = true;
9
+ protected $_canRefund = true;
10
+
11
+ protected $_apiModel;
12
+
13
+ public function __construct()
14
+ {
15
+ $this->_apiModel = Mage::getModel('securepay/api');
16
+ }
17
+
18
+ public function isAvailable($quote = null)
19
+ {
20
+ $isVailable = parent::isAvailable($quote);
21
+ if($isVailable){
22
+ return $this->_apiModel->isPaymentMethodAvailable();
23
+ }
24
+ return false;
25
+ }
26
+
27
+ public function getConfigData($field, $storeId = null)
28
+ {
29
+ if($field!='payment_action')
30
+ return parent::getConfigData($field, $storeId);
31
+
32
+ $path = 'payment/'.$this->getCode().'/'.$field;
33
+ $value = Mage::getStoreConfig($path, $storeId);
34
+ if($value==AMG_SecurePay_Model_Directpost_Source_PaymentAction::SALE){
35
+ $value = AMG_SecurePay_Model_Directpost_Source_PaymentAction::AUTHORIZE_CAPTURE;
36
+ }
37
+ return $value;
38
+ }
39
+
40
+ protected function initializeApiModel(Varien_Object $payment,$amount)
41
+ {
42
+ $this->_apiModel->setPayment($payment);
43
+ $this->_apiModel->setAmount($amount);
44
+ }
45
+
46
+ protected function _shouldDoSale()
47
+ {
48
+ if(Mage::getStoreConfig('payment/'.$this->getCode().'/payment_action')==AMG_SecurePay_Model_Directpost_Source_PaymentAction::SALE){
49
+ return true;
50
+ }
51
+ return false;
52
+ }
53
+
54
+ protected function sale(Varien_Object $payment, $amount)
55
+ {
56
+ if($amount <= 0){
57
+ Mage::throwException(Mage::helper('securepay')->__('Invalid amount.'));
58
+ }
59
+
60
+ $this->initializeApiModel($payment,$amount);
61
+
62
+ $saleRequest = $this->_apiModel->getSaleRequest();
63
+
64
+ $saleResponse = $this->_apiModel->_postApiRequest($saleRequest);
65
+
66
+ if($saleResponse['response'] == 1){
67
+ $this->updatePaymentWithApiResponse($payment,$saleResponse);
68
+ }
69
+ else{
70
+ Mage::throwException($saleResponse['responsetext']);
71
+ }
72
+ }
73
+
74
+ public function authorize(Varien_Object $payment, $amount)
75
+ {
76
+ if($amount <= 0){
77
+ Mage::throwException(Mage::helper('securepay')->__('Invalid amount to authorize.'));
78
+ }
79
+
80
+ $this->initializeApiModel($payment,$amount);
81
+
82
+ $authorizeRequest = $this->_apiModel->getAuthorizationRequest();
83
+
84
+ $authorizationResponse = $this->_apiModel->_postApiRequest($authorizeRequest);
85
+
86
+ if($authorizationResponse['response'] == 1){
87
+ $this->updatePaymentWithApiResponse($payment,$authorizationResponse);
88
+ }
89
+ else{
90
+ Mage::throwException($authorizationResponse['responsetext']);
91
+ }
92
+ return $authorizationResponse;
93
+ }
94
+
95
+ public function capture(Varien_Object $payment, $amount)
96
+ {
97
+ if($this->_shouldDoSale()){
98
+ $this->sale($payment, $amount);
99
+ return;
100
+ }
101
+
102
+ $this->initializeApiModel($payment,$amount);
103
+
104
+ $transactionId = '';
105
+
106
+ $authorizationTransaction = $payment->getAuthorizationTransaction();
107
+ if($authorizationTransaction){
108
+ if($authorizationTransaction->getTxnId()){
109
+ $transactionId = $authorizationTransaction->getTxnId();
110
+ }
111
+ }
112
+ if($transactionId==''){
113
+ $authorizationResponse = $this->authorize($payment, $amount);
114
+ if($authorizationResponse['response'] == 1 && $authorizationResponse['transactionid']){
115
+ $transactionId = $authorizationResponse['transactionid'];
116
+ }
117
+ }
118
+ if($transactionId!=''){
119
+ $captureRequest = $this->_apiModel->getCaptureRequest($transactionId);
120
+ $captureResponse = $this->_apiModel->_postApiRequest($captureRequest);
121
+ if($captureResponse['response'] == 1){
122
+ $this->updatePaymentWithApiResponse($payment,$captureResponse);
123
+ }
124
+ else{
125
+ Mage::throwException($captureResponse['responsetext']);
126
+ }
127
+ }
128
+ else{
129
+ Mage::throwException(Mage::helper('securepay')->__('Amount is not authorized.'));
130
+ }
131
+ }
132
+
133
+ protected function updatePaymentWithApiResponse(Varien_Object $payment,$response, $transactionClosed = 0)
134
+ {
135
+ $payment->setTransactionId($response['transactionid']);
136
+ if(isset($response['customer_vault_id'])){
137
+ $payment->setAdditionalInformation('amg_customer_vault_id', $response['customer_vault_id']);
138
+ }
139
+ $payment->setIsTransactionClosed($transactionClosed);
140
+ $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS,$response);
141
+ }
142
+
143
+ public function refund(Varien_Object $payment, $amount)
144
+ {
145
+ if($amount <= 0){
146
+ Mage::throwException(Mage::helper('securepay')->__('Invalid amount to refund.'));
147
+ }
148
+
149
+ $this->initializeApiModel($payment,$amount);
150
+
151
+ $refundRequest = $this->_apiModel->getRefundRequest();
152
+
153
+ $refundResponse = $this->_apiModel->_postApiRequest($refundRequest);
154
+ if($refundResponse['response'] == 1){
155
+ $transactionClosed = 1;
156
+ $this->updatePaymentWithApiResponse($payment,$refundResponse,$transactionClosed);
157
+ }
158
+ else{
159
+ Mage::throwException($captureResponse['responsetext']);
160
+ }
161
+ return $this;
162
+ }
163
+ }
app/code/community/AMG/SecurePay/Model/Directpost/Customervault.php ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Directpost_Customervault extends AMG_SecurePay_Model_Directpost
3
+ {
4
+ protected $_code = 'securepay_directpost_customervault';
5
+ protected $_formBlockType = 'securepay/directpost_customervault_form';
6
+
7
+ protected $_canAuthorize = true;
8
+ protected $_canCapture = true;
9
+ protected $_canRefund = true;
10
+
11
+ protected $_apiModel;
12
+
13
+ public function __construct()
14
+ {
15
+ $this->_apiModel = Mage::getModel('securepay/api');
16
+ }
17
+
18
+ public function isAvailable($quote = null)
19
+ {
20
+ if(Mage::getStoreConfigFlag('payment/securepay_directpost/active') && $this->getConfigData('active')){
21
+ $isVailable = $this->_apiModel->isPaymentMethodAvailable();
22
+ if($isVailable){
23
+ $customerId = Mage::helper('securepay')->getCurrentQuoteCustomerId();
24
+ if($customerId>0){
25
+ $collection = Mage::getModel('securepay/customervault')->getCollection()
26
+ ->addFieldToFilter('customer_id',$customerId);
27
+ if(count($collection)>0)
28
+ return true;
29
+ }
30
+ }
31
+ }
32
+ return false;
33
+ }
34
+
35
+ public function validate()
36
+ {
37
+ return true;
38
+ }
39
+
40
+ public function authorize(Varien_Object $payment, $amount)
41
+ {
42
+ if($amount <= 0){
43
+ Mage::throwException(Mage::helper('securepay')->__('Invalid amount to authorize.'));
44
+ }
45
+
46
+ $amgCustomerVaultId = $this->getAmgCustomerVaultId($payment);
47
+ if($amgCustomerVaultId <= 0){
48
+ Mage::throwException(Mage::helper('securepay')->__('Could not find customer vault information.'));
49
+ }
50
+
51
+ $this->initializeApiModel($payment,$amount);
52
+
53
+ $authorizeRequest = $this->_apiModel->getCustomerVaultAuthorizationRequest($amgCustomerVaultId);
54
+
55
+ $authorizationResponse = $this->_apiModel->_postApiRequest($authorizeRequest);
56
+
57
+ if($authorizationResponse['response'] == 1){
58
+ $this->updatePaymentWithApiResponse($payment,$authorizationResponse);
59
+ }
60
+ else{
61
+ Mage::throwException($authorizationResponse['responsetext']);
62
+ }
63
+ return $authorizationResponse;
64
+ }
65
+
66
+ protected function sale(Varien_Object $payment, $amount)
67
+ {
68
+ if($amount <= 0){
69
+ Mage::throwException(Mage::helper('securepay')->__('Invalid amount.'));
70
+ }
71
+
72
+ $amgCustomerVaultId = $this->getAmgCustomerVaultId($payment);
73
+ if($amgCustomerVaultId <= 0){
74
+ Mage::throwException(Mage::helper('securepay')->__('Could not find customer vault information.'));
75
+ }
76
+
77
+ $this->initializeApiModel($payment,$amount);
78
+
79
+ $saleRequest = $this->_apiModel->getCustomerVaultSaleRequest($amgCustomerVaultId);
80
+
81
+ $saleResponse = $this->_apiModel->_postApiRequest($saleRequest);
82
+
83
+ if($saleResponse['response'] == 1){
84
+ $this->updatePaymentWithApiResponse($payment,$saleResponse);
85
+ }
86
+ else{
87
+ Mage::throwException($saleResponse['responsetext']);
88
+ }
89
+ }
90
+
91
+ protected function getAmgCustomerVaultId($payment)
92
+ {
93
+ $paymentMethod = $payment->getMethod();
94
+ if($paymentMethod==$this->_code){
95
+ if($payment->hasAdditionalInformation('customervault_id')){
96
+ $customervault_id = $payment->getAdditionalInformation('customervault_id');
97
+ if($customervault_id>0){
98
+ $customerVaultModel = Mage::getModel('securepay/customervault');
99
+ $customerVaultModel->load($customervault_id);
100
+ if($customerVaultModel->getId()){
101
+ if($customerVaultModel->getCustomerId()>0){
102
+ if($payment->getOrder()->getData('customer_id')==$customerVaultModel->getCustomerId()){
103
+ return $customerVaultModel->getAmgCustomerVaultId();
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }
110
+ return 0;
111
+ }
112
+ }
app/code/community/AMG/SecurePay/Model/Directpost/Customervault/Source/PaymentAction.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Directpost_Customervault_Source_PaymentAction
3
+ {
4
+ const AUTHORIZE = 'authorize';
5
+ const AUTHORIZE_CAPTURE = 'authorize_capture';
6
+ const SALE = 'sale';
7
+
8
+ public function toOptionArray()
9
+ {
10
+ return array(
11
+ array(
12
+ 'value' => self::AUTHORIZE,
13
+ 'label' => Mage::helper('securepay')->__('Authorize Only')
14
+ ),
15
+ array(
16
+ 'value' => self::AUTHORIZE_CAPTURE,
17
+ 'label' => Mage::helper('securepay')->__('Authorize And Capture')
18
+ ),
19
+ array(
20
+ 'value' => self::SALE,
21
+ 'label' => Mage::helper('securepay')->__('Sale')
22
+ ),
23
+ );
24
+ }
25
+ }
app/code/community/AMG/SecurePay/Model/Directpost/Source/PaymentAction.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Directpost_Source_PaymentAction
3
+ {
4
+ const AUTHORIZE = 'authorize';
5
+ const AUTHORIZE_CAPTURE = 'authorize_capture';
6
+ const SALE = 'sale';
7
+
8
+ public function toOptionArray()
9
+ {
10
+ return array(
11
+ array(
12
+ 'value' => self::AUTHORIZE,
13
+ 'label' => Mage::helper('securepay')->__('Authorize Only')
14
+ ),
15
+ array(
16
+ 'value' => self::AUTHORIZE_CAPTURE,
17
+ 'label' => Mage::helper('securepay')->__('Authorize And Capture')
18
+ ),
19
+ array(
20
+ 'value' => self::SALE,
21
+ 'label' => Mage::helper('securepay')->__('Sale')
22
+ ),
23
+ );
24
+ }
25
+ }
app/code/community/AMG/SecurePay/Model/Mysql4/Customervault.php ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Mysql4_Customervault extends Mage_Core_Model_Mysql4_Abstract
3
+ {
4
+ public function _construct()
5
+ {
6
+ $this->_init('securepay/customervault','customervault_id');
7
+ }
8
+
9
+ public function getCustomerVaultIdByCreditCard($customerId, $ccType, $ccLast4, $ccExpMonth, $ccExpYear)
10
+ {
11
+ if($customerId && $ccType && $ccLast4 && $ccExpMonth && $ccExpYear){
12
+ $select = $this->_getReadAdapter()->select()->reset();
13
+ $select->from($this->getMainTable(), array('customervault_id'))
14
+ ->where('customer_id =? ',$customerId)
15
+ ->where('cc_type =? ',$ccType)
16
+ ->where('cc_last4 =? ', $ccLast4)
17
+ ->where('cc_exp_month =? ',$ccExpMonth)
18
+ ->where('cc_exp_year =? ',$ccExpYear);
19
+ $rows = $this->_getReadAdapter()->fetchAll($select);
20
+ if(count($rows)>0)
21
+ return $rows[0]['customervault_id'];
22
+ }
23
+ return 0;
24
+ }
25
+
26
+ public function getCustomerVaultIdByCode($code)
27
+ {
28
+ if($code){
29
+ $select = $this->_getReadAdapter()->select()->reset();
30
+ $select->from($this->getMainTable(), array('customervault_id'))
31
+ ->where('code =? ',$code);
32
+ $rows = $this->_getReadAdapter()->fetchAll($select);
33
+ if(count($rows)>0)
34
+ return $rows[0]['customervault_id'];
35
+ }
36
+ return 0;
37
+ }
38
+
39
+ public function generateUniqueCode()
40
+ {
41
+ for($i=0;$i<50;$i++){
42
+ $code = Mage::helper('core')->getRandomString(10);
43
+ $select = $this->_getReadAdapter()->select()->reset()
44
+ ->from(array('main_table'=>$this->getMainTable()),array('noofrd'=>'COUNT(*)'))
45
+ ->where('main_table.code = ?', $code);
46
+ $row = $this->_getReadAdapter()->fetchRow($select);
47
+ if($row['noofrd']==0)
48
+ return $code;
49
+ }
50
+ return '';
51
+ }
52
+ }
app/code/community/AMG/SecurePay/Model/Mysql4/Customervault/Collection.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Mysql4_Customervault_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
3
+ {
4
+ public function _construct()
5
+ {
6
+ parent::_construct();
7
+ $this->_init('securepay/customervault');
8
+ }
9
+ }
app/code/community/AMG/SecurePay/Model/Observer.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_Model_Observer
3
+ {
4
+ public function salesQuotePaymentImportDataBefore(Varien_Event_Observer $observer)
5
+ {
6
+ $input = $observer->getInput();
7
+ $payment = $observer->getPayment();
8
+ $paymentMethod = $input->getMethod();
9
+ if(isset($paymentMethod)){
10
+ $additionalFields = Mage::helper('securepay')->getPaymentMethodAdditionalInfoFields();
11
+ if(!empty($additionalFields)){
12
+ foreach($additionalFields as $field){
13
+ if($payment->hasAdditionalInformation($field)){
14
+ $payment->unsAdditionalInformation($field);
15
+ }
16
+ }
17
+ }
18
+ if($paymentMethod==Mage::getModel('securepay/directpost')->getCode()){
19
+ if(Mage::helper('securepay')->isCcSaveAllowed()){
20
+ if($input['cc_save_future_directpost'] == 'Y'){
21
+ $payment->setAdditionalInformation('cc_save_future_directpost', 'Y');
22
+ }
23
+ }
24
+ }
25
+ elseif($paymentMethod==Mage::getModel('securepay/directpost_customervault')->getCode()){
26
+ if($input['customervault_code']) {
27
+ $code = $input['customervault_code'];
28
+ $customerVaultModel = Mage::getModel('securepay/customervault');
29
+ $customerVaultModel->loadByCode($code);
30
+ if($customerVaultModel->getId()){
31
+ $customerId = Mage::helper('securepay')->getCurrentQuoteCustomerId();
32
+ if($customerVaultModel->getCustomerId()==$customerId)
33
+ $payment->setAdditionalInformation('customervault_id', $customerVaultModel->getCustomervaultId());
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ public function checkoutSubmitAllAfter($observer)
41
+ {
42
+ $orders = $observer->getOrders();
43
+ if(is_null($orders)){
44
+ $orders = array($observer->getOrder());
45
+ }
46
+ foreach($orders as $order){
47
+ $customerId = $order->getData('customer_id');
48
+ if(!$customerId){
49
+ return;
50
+ }
51
+ $payment = $order->getPayment();
52
+ $paymentMethod = $payment->getMethod();
53
+ if(isset($paymentMethod) && $payment->getData('transaction_id') ){
54
+ if($paymentMethod==Mage::getModel('securepay/directpost')->getCode()){
55
+ if(Mage::helper('securepay')->isCcSaveAllowed()){
56
+ if($payment->hasAdditionalInformation('cc_save_future_directpost') &&($payment->getAdditionalInformation('cc_save_future_directpost') == 'Y')){
57
+ $amg_customer_vault_id = '';
58
+ if($payment->hasAdditionalInformation('amg_customer_vault_id') &&($payment->getAdditionalInformation('amg_customer_vault_id') != '')){
59
+ $amg_customer_vault_id = $payment->getAdditionalInformation('amg_customer_vault_id');
60
+
61
+ $customerVaultModel = Mage::getModel('securepay/customervault');
62
+ $customerVaultModel->setData(array(
63
+ 'transaction_id' => $payment->getData('transaction_id'),
64
+ 'customer_id' => $customerId,
65
+ 'cc_type' => $payment->getData('cc_type'),
66
+ 'cc_last4' => $payment->getData('cc_last4'),
67
+ 'cc_exp_month' => $payment->getData('cc_exp_month'),
68
+ 'cc_exp_year' => $payment->getData('cc_exp_year'),
69
+ 'created_date' => now(),
70
+ 'updated_date' => now(),
71
+ 'amg_customer_vault_id' => $amg_customer_vault_id
72
+ ));
73
+ if($payment->hasAdditionalInformation('customervault_id') &&($payment->getAdditionalInformation('customervault_id') != '')){
74
+ $customerVaultModel->setId($payment->getAdditionalInformation('customervault_id'));
75
+ }
76
+ else{
77
+ $code = $customerVaultModel->generateUniqueCode();
78
+ $customerVaultModel->setData('code',$code);
79
+ }
80
+ $customerVaultModel->save();
81
+ }
82
+ }
83
+ }
84
+ }
85
+ elseif($paymentMethod==Mage::getModel('securepay/directpost_customervault')->getCode()){
86
+ if ($payment->hasAdditionalInformation('customervault_id') && $payment->getData('transaction_id')) {
87
+ $customervault_id = $payment->getAdditionalInformation('customervault_id');
88
+ if($customervault_id>0){
89
+ $customerVaultModel = Mage::getModel('securepay/customervault');
90
+ $customerVaultModel->load($customervault_id);
91
+ if($customerVaultModel->getId()){
92
+ if($customerVaultModel->getCustomerId()>0 && ($customerVaultModel->getCustomerId()==$customerId))
93
+ $customerVaultModel->setData('transaction_id', $payment->getData('transaction_id'))
94
+ ->setData('updated_date', now())
95
+ ->save();
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ }
app/code/community/AMG/SecurePay/controllers/StoredcardController.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AMG_SecurePay_StoredcardController extends Mage_Core_Controller_Front_Action
3
+ {
4
+ public function preDispatch()
5
+ {
6
+ parent::preDispatch();
7
+ $action = $this->getRequest()->getActionName();
8
+ $loginUrl = Mage::helper('customer')->getLoginUrl();
9
+
10
+ if(!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)){
11
+ $this->setFlag('', self::FLAG_NO_DISPATCH, true);
12
+ }
13
+ }
14
+
15
+ public function indexAction()
16
+ {
17
+ $this->loadLayout();
18
+ $this->_initLayoutMessages('customer/session');
19
+ $this->renderLayout();
20
+ }
21
+
22
+ public function deleteAction()
23
+ {
24
+ if(Mage::getSingleton('customer/session')->isLoggedIn()){
25
+ $loggedInCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
26
+ if($loggedInCustomerId>0){
27
+ $code = $this->getRequest()->getParam('c');
28
+ if($code){
29
+ $customerVaultModel = Mage::getModel('securepay/customervault');
30
+ $customerVaultModel->loadByCode($code);
31
+ if($customerVaultModel->getId() && ($customerVaultModel->getCustomerId() == $loggedInCustomerId)){
32
+ $customerVaultModel->delete();
33
+ Mage::getSingleton('customer/session')->addSuccess($this->__('Your selected Credit Card record has been deleted.'));
34
+ }
35
+ }
36
+ }
37
+ }
38
+ $this->_redirect('*/*/');
39
+ }
40
+ }
app/code/community/AMG/SecurePay/etc/config.xml ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <AMG_SecurePay>
5
+ <version>0.1.0</version>
6
+ </AMG_SecurePay>
7
+ </modules>
8
+ <frontend>
9
+ <routers>
10
+ <AMG_SecurePay>
11
+ <use>standard</use>
12
+ <args>
13
+ <module>AMG_SecurePay</module>
14
+ <frontName>securepay</frontName>
15
+ </args>
16
+ </AMG_SecurePay>
17
+ </routers>
18
+ <layout>
19
+ <updates>
20
+ <securepay>
21
+ <file>AMG/securepay.xml</file>
22
+ </securepay>
23
+ </updates>
24
+ </layout>
25
+ <translate>
26
+ <modules>
27
+ <AMG_SecurePay>
28
+ <files>
29
+ <default>AMG/SecurePay.csv</default>
30
+ </files>
31
+ </AMG_SecurePay>
32
+ </modules>
33
+ </translate>
34
+ </frontend>
35
+ <global>
36
+ <blocks>
37
+ <securepay>
38
+ <class>AMG_SecurePay_Block</class>
39
+ </securepay>
40
+ </blocks>
41
+ <helpers>
42
+ <securepay>
43
+ <class>AMG_SecurePay_Helper</class>
44
+ </securepay>
45
+ </helpers>
46
+ <models>
47
+ <securepay>
48
+ <class>AMG_SecurePay_Model</class>
49
+ <resourceModel>securepay_mysql4</resourceModel>
50
+ </securepay>
51
+ <securepay_mysql4>
52
+ <class>AMG_SecurePay_Model_Mysql4</class>
53
+ <entities>
54
+ <customervault>
55
+ <table>amg_securepay_customervault</table>
56
+ </customervault>
57
+ </entities>
58
+ </securepay_mysql4>
59
+ </models>
60
+ <resources>
61
+ <securepay_setup>
62
+ <setup>
63
+ <module>AMG_SecurePay</module>
64
+ </setup>
65
+ <connection>
66
+ <use>core_setup</use>
67
+ </connection>
68
+ </securepay_setup>
69
+ <securepay_write>
70
+ <connection>
71
+ <use>core_write</use>
72
+ </connection>
73
+ </securepay_write>
74
+ <securepay_read>
75
+ <connection>
76
+ <use>core_read</use>
77
+ </connection>
78
+ </securepay_read>
79
+ </resources>
80
+ <events>
81
+ <sales_quote_payment_import_data_before>
82
+ <observers>
83
+ <amg_securepay_sales_quote_payment_import_data_before>
84
+ <class>securepay/observer</class>
85
+ <method>salesQuotePaymentImportDataBefore</method>
86
+ </amg_securepay_sales_quote_payment_import_data_before>
87
+ </observers>
88
+ </sales_quote_payment_import_data_before>
89
+ <checkout_submit_all_after>
90
+ <observers>
91
+ <amg_securepay_checkout_submit_all_after>
92
+ <class>securepay/observer</class>
93
+ <method>checkoutSubmitAllAfter</method>
94
+ </amg_securepay_checkout_submit_all_after>
95
+ </observers>
96
+ </checkout_submit_all_after>
97
+ </events>
98
+ </global>
99
+ <default>
100
+ <payment>
101
+ <securepay_directpost>
102
+ <active>0</active>
103
+ <title>AMG SecurePay</title>
104
+ <gateway_url>https://secure.advancedmerchantgroupgateway.com/api/transact.php</gateway_url>
105
+ <payment_action>authorize</payment_action>
106
+ <order_status>processing</order_status>
107
+ <currency>USD</currency>
108
+ <log>1</log>
109
+ <cctypes>AE,VI,MC,DI</cctypes>
110
+ <useccv>1</useccv>
111
+ <allowsavecc>1</allowsavecc>
112
+ <allowspecific>0</allowspecific>
113
+ <check_dup_seconds>0</check_dup_seconds>
114
+ <dup_seconds>0</dup_seconds>
115
+ <sort_order>200</sort_order>
116
+ <model>securepay/directpost</model>
117
+ <api_id backend_model="adminhtml/system_config_backend_encrypted"/>
118
+ <trans_key backend_model="adminhtml/system_config_backend_encrypted"/>
119
+ </securepay_directpost>
120
+ <securepay_directpost_customervault>
121
+ <active>0</active>
122
+ <title>AMG SecurePay - Customer Vault</title>
123
+ <payment_action>authorize</payment_action>
124
+ <order_status>processing</order_status>
125
+ <allowspecific>0</allowspecific>
126
+ <sort_order>201</sort_order>
127
+ <model>securepay/directpost_customervault</model>
128
+ </securepay_directpost_customervault>
129
+ </payment>
130
+ </default>
131
+ </config>
app/code/community/AMG/SecurePay/etc/system.xml ADDED
@@ -0,0 +1,246 @@