Skip to content
This repository was archived by the owner on Jul 23, 2020. It is now read-only.

Commit c791765

Browse files
committed
Merge pull request #1 from smgom/master
version update 0.2
2 parents cb15630 + 2e15d18 commit c791765

30 files changed

+5967
-7136
lines changed

app/code/community/Start/Gateway/Model/Paymentmethod.php

Lines changed: 135 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Start Gateway Model
45
*
@@ -7,39 +8,39 @@
78
* @author Haris Ahmed <haris.ahmed@eweberinc.com>
89
*/
910
class Start_Gateway_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract {
10-
protected $_code = 'gateway';
11-
protected $_formBlockType = 'gateway/form_gateway';
12-
protected $_infoBlockType = 'gateway/info_gateway';
11+
12+
protected $_code = 'gateway';
13+
protected $_formBlockType = 'gateway/form_gateway';
14+
protected $_infoBlockType = 'gateway/info_gateway';
1315

1416
const REQUEST_TYPE_AUTH_CAPTURE = 'AUTH_CAPTURE';
15-
const REQUEST_TYPE_AUTH_ONLY = 'AUTH_ONLY';
17+
const REQUEST_TYPE_AUTH_ONLY = 'AUTH_ONLY';
1618
const REQUEST_TYPE_CAPTURE_ONLY = 'CAPTURE_ONLY';
1719
const REQUEST_TYPE_PRIOR_AUTH_CAPTURE = 'PRIOR_AUTH_CAPTURE';
20+
const PLUGIN_VERSION = '0.2';
1821

19-
/**
22+
/**
2023
* Availability options
2124
*/
22-
protected $_canAuthorize = true;
23-
protected $_canCapture = true;
24-
protected $_canCapturePartial = false;
25-
protected $_canRefund = false;
25+
protected $_canAuthorize = true;
26+
protected $_canCapture = true;
27+
protected $_canCapturePartial = false;
28+
protected $_canRefund = false;
2629
protected $_canRefundInvoicePartial = false;
27-
protected $_canVoid = false;
28-
protected $_canUseInternal = false;
29-
protected $_canUseCheckout = true;
30-
protected $_canUseForMultishipping = false;
31-
protected $_canSaveCc = false;
32-
protected $_isInitializeNeeded = true;
30+
protected $_canVoid = false;
31+
protected $_canUseInternal = false;
32+
protected $_canUseCheckout = true;
33+
protected $_canUseForMultishipping = false;
34+
protected $_canSaveCc = false;
35+
protected $_isInitializeNeeded = true;
3336
protected $_canFetchTransactionInfo = false;
3437

35-
3638
/**
3739
* Check refund availability
3840
*
3941
* @return bool
4042
*/
41-
public function canRefund()
42-
{
43+
public function canRefund() {
4344
return $this->_canRefund;
4445
}
4546

@@ -49,81 +50,133 @@ public function canRefund()
4950
* @param Varien_Object $invoicePayment
5051
* @return bool
5152
*/
52-
public function canVoid(Varien_Object $payment)
53-
{
53+
public function canVoid(Varien_Object $payment) {
5454
return $this->_canVoid;
5555
}
5656

57-
public function assignData($data)
58-
{
59-
$info = $this->getInfoInstance();
57+
public function assignData($data) {
58+
$info = $this->getInfoInstance();
6059

61-
if (isset($_POST['payfortToken']))
62-
{
63-
$info->setPayfortToken($_POST['payfortToken']);
64-
}
60+
if (isset($_POST['payfortToken'])) {
61+
$info->setPayfortToken($_POST['payfortToken']);
62+
}
6563

66-
if (isset($_POST['payfortEmail']))
67-
{
68-
$info->setPayfortToken($_POST['payfortEmail']);
69-
}
64+
if (isset($_POST['payfortEmail'])) {
65+
$info->setPayfortToken($_POST['payfortEmail']);
66+
}
7067

71-
return $this;
72-
}
68+
return $this;
69+
}
7370

74-
public function getFormBlockType()
75-
{
71+
public function getFormBlockType() {
7672
return $this->_formBlockType;
7773
}
7874

79-
public function collectPayment(\Mage_Payment_Model_Info $payment, $amount, $capture=true) {
75+
public function collectPayment(\Mage_Payment_Model_Info $payment, $amount, $capture = true) {
8076

81-
require_once(MAGENTO_ROOT.'/lib/Start/autoload.php'); # At the top of your PHP file
77+
require_once(MAGENTO_ROOT . '/lib/Start/autoload.php'); # At the top of your PHP file
8278

83-
$token = isset($_POST['payfortToken'])?$_POST['payfortToken']:false;
84-
$email = isset($_POST['payfortEmail'])?$_POST['payfortEmail']:false;
85-
if(!$token || !$email){
79+
$token = isset($_POST['payfortToken']) ? $_POST['payfortToken'] : false;
80+
$email = isset($_POST['payfortEmail']) ? $_POST['payfortEmail'] : false;
81+
if (!$token || !$email) {
8682
//this block will be executed if the order was authorized earlier and now trying to capture amount
8783
$token_array = $payment->getAdditionalInformation('token');
8884
$token = $token_array['token'];
8985
$email = $token_array['email'];
9086
}
9187

92-
if(!$token || !$email){
88+
if (!$token || !$email) {
9389
Mage::throwException('Invalid Token');
9490
}
95-
9691
$store = Mage::app()->getStore();
9792
$currency = $store->getCurrentCurrency();
98-
99-
$currency = !isset($currency)?'AED':$currency->getCurrencyCode();
100-
101-
$amount_in_cents = $amount*100;
102-
93+
$currency = !isset($currency) ? 'AED' : $currency->getCurrencyCode();
94+
if (file_exists(MAGENTO_ROOT . '/data/currencies.json')) {
95+
$currency_json_data = json_decode(file_get_contents(MAGENTO_ROOT . '/data/currencies.json'), 1);
96+
$currency_multiplier = $currency_json_data[$currency];
97+
} else {
98+
$currency_multiplier = 100;
99+
}
100+
$amount_in_cents = $amount * $currency_multiplier;
101+
$order = $payment->getOrder();
102+
$order_items_array_full = array();
103+
foreach ($order->getAllVisibleItems() as $value) {
104+
$order_items_array['title'] = $value->getName();
105+
$order_items_array['amount'] = round($value->getPrice(), 2) * $currency_multiplier;
106+
$order_items_array['quantity'] = $value->getQtyOrdered();
107+
array_push($order_items_array_full, $order_items_array);
108+
}
109+
$shipping_amount = $order->getShippingAmount();
110+
$shipping_amount = $shipping_amount * $currency_multiplier;
111+
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
112+
$customer = Mage::getSingleton('customer/session')->getCustomer();
113+
$username = $customer->getName();
114+
$registered_at = date(DATE_ISO8601, strtotime($customer->getCreatedAt()));
115+
} else {
116+
$username = "Guest";
117+
$registered_at = date(DATE_ISO8601, strtotime(date("Y-m-d H:i:s")));
118+
}
119+
$shiping_data = $order->getShippingAddress()->getData();
120+
$shipping_address = array(
121+
"first_name" => $shiping_data['firstname'],
122+
"last_name" => $shiping_data['lastname'],
123+
"country" => $shiping_data['country_id'],
124+
"city" => $shiping_data['city'],
125+
"address" => $shiping_data['customer_address'],
126+
"phone" => $shiping_data['telephone'],
127+
"postcode" => $shiping_data['postcode']
128+
);
129+
$billing_data = $order->getBillingAddress()->getData();
130+
$billing_address = array(
131+
"first_name" => $billing_data['firstname'],
132+
"last_name" => $billing_data['lastname'],
133+
"country" => $billing_data['country_id'],
134+
"city" => $billing_data['city'],
135+
"address" => $billing_data['customer_address'],
136+
"phone" => $billing_data['telephone'],
137+
"postcode" => $billing_data['postcode']
138+
);
139+
140+
$shopping_cart_array = array(
141+
'user_name' => $username,
142+
'registered_at' => $registered_at,
143+
'items' => $order_items_array_full,
144+
'billing_address' => $billing_address,
145+
'shipping_address' => $shipping_address
146+
);
147+
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
103148
$charge_args = array(
104-
'description' => "Magento charge for ".$email,
105-
'card' => $token,
106-
'currency' => $currency,
107-
'email' => $email,
108-
'ip' => $_SERVER['REMOTE_ADDR'],
109-
'amount' => $amount_in_cents,
110-
'capture' => $capture
111-
);
112-
149+
'description' => "Magento charge for " . $email,
150+
'card' => $token,
151+
'currency' => $currency,
152+
'email' => $email,
153+
'ip' => $_SERVER['REMOTE_ADDR'],
154+
'amount' => $amount_in_cents,
155+
'capture' => $capture,
156+
'shipping_amount' => $shipping_amount,
157+
'shopping_cart' => $shopping_cart_array,
158+
'metadata' => array('reference_id' => $orderId)
159+
);
160+
161+
$ver = new Mage;
162+
$version=$ver->getVersion();
163+
$userAgent = 'Magento ' . $version . ' / Start Plugin ' . self::PLUGIN_VERSION;
164+
Start::setUserAgent($userAgent);
165+
113166
$method = $payment->getMethodInstance();
114-
if($method->getConfigData('test_mode')==1)
167+
if ($method->getConfigData('test_mode') == 1)
115168
Start::setApiKey($method->getConfigData('test_secret_key'));
116169
else
117170
Start::setApiKey($method->getConfigData('live_secret_key'));
118-
119-
try{
171+
172+
try {
120173
// Charge the token
121174
$charge = Start_Charge::create($charge_args);
122175
//need to process charge as success or failed
123-
}catch(Start_Error $e){
176+
} catch (Start_Error $e) {
124177
$error_code = $e->getErrorCode();
125178

126-
if ( $error_code === "card_declined" ) {
179+
if ($error_code === "card_declined") {
127180
$errorMsg = $e->getMessage() . ' Please, return to "Payment Information" step and try with another card.';
128181
} else {
129182
$errorMsg = $e->getMessage();
@@ -132,30 +185,26 @@ public function collectPayment(\Mage_Payment_Model_Info $payment, $amount, $capt
132185
Mage::throwException($errorMsg);
133186
}
134187
//need to process charge as success or failed
135-
}
136-
137-
188+
}
138189

139-
public function authorize(Varien_Object $payment, $amount)
140-
{
190+
public function authorize(Varien_Object $payment, $amount) {
141191
if ($amount <= 0) {
142192
Mage::throwException(Mage::helper('paygate')->__('Invalid amount for capture.'));
143193
}
144194

145195
$method = $payment->getMethodInstance();
146196
$capture = false;
147-
if($method->getConfigData('payment_action')==self::ACTION_AUTHORIZE_CAPTURE)
197+
if ($method->getConfigData('payment_action') == self::ACTION_AUTHORIZE_CAPTURE)
148198
$capture = true;
149199

150-
$this->collectPayment($payment, $amount,$capture);
200+
$this->collectPayment($payment, $amount, $capture);
151201

152-
$token = isset($_POST['payfortToken'])?$_POST['payfortToken']:false;
153-
$email = isset($_POST['payfortEmail'])?$_POST['payfortEmail']:false;
202+
$token = isset($_POST['payfortToken']) ? $_POST['payfortToken'] : false;
203+
$email = isset($_POST['payfortEmail']) ? $_POST['payfortEmail'] : false;
154204

155205
$payment->setAdditionalInformation('payment_type', $this->getConfigData('payment_action'));
156-
$payment->setAdditionalInformation('token', array('token'=>$token,'email'=>$email));
206+
$payment->setAdditionalInformation('token', array('token' => $token, 'email' => $email));
157207
return $this;
158-
159208
}
160209

161210
/**
@@ -166,8 +215,7 @@ public function authorize(Varien_Object $payment, $amount)
166215
* @return Start_Gateway_Model_Paymentmethod
167216
* @throws Mage_Core_Exception
168217
*/
169-
public function capture(Varien_Object $payment, $amount)
170-
{
218+
public function capture(Varien_Object $payment, $amount) {
171219
if ($amount <= 0) {
172220
Mage::throwException('Invalid amount for capture.');
173221
}
@@ -183,38 +231,34 @@ public function capture(Varien_Object $payment, $amount)
183231

184232
//please call this function or some function to call API with token and email to capture authorized amount
185233
//$this->collectPayment($payment, $amount);
186-
187234
//returning this as all good but it should return $this in case success and throw exception in case of error
188235
return $this;
189236
}
190237

191-
public function validate()
192-
{
193-
parent::validate();
238+
public function validate() {
239+
parent::validate();
194240

195-
$token = isset($_POST['payfortToken'])?$_POST['payfortToken']:false;
196-
$email = isset($_POST['payfortEmail'])?$_POST['payfortEmail']:false;
241+
$token = isset($_POST['payfortToken']) ? $_POST['payfortToken'] : false;
242+
$email = isset($_POST['payfortEmail']) ? $_POST['payfortEmail'] : false;
197243

198-
if(!$token || !$email){
244+
if (!$token || !$email) {
199245
Mage::throwException('Invalid Token');
200246
}
201247

202-
if (isset($errorMsg))
203-
{
204-
Mage::throwException($errorMsg);
205-
}
248+
if (isset($errorMsg)) {
249+
Mage::throwException($errorMsg);
250+
}
206251

207-
return $this;
208-
}
252+
return $this;
253+
}
209254

210-
/**
255+
/**
211256
* Instantiate state and set it to state object
212257
*
213258
* @param string $paymentAction
214259
* @param Varien_Object
215260
*/
216-
public function initialize($paymentAction, $stateObject)
217-
{
261+
public function initialize($paymentAction, $stateObject) {
218262
switch ($paymentAction) {
219263
case self::ACTION_AUTHORIZE:
220264
$payment = $this->getInfoInstance();
@@ -232,7 +276,7 @@ public function initialize($paymentAction, $stateObject)
232276
break;
233277
case self::ACTION_AUTHORIZE_CAPTURE:
234278
$payment = $this->getInfoInstance();
235-
279+
236280
$order = $payment->getOrder();
237281
$order->setCanSendNewEmailFlag(false);
238282
$payment->authorize(true, $order->getBaseTotalDue()); // base amount will be set inside

0 commit comments

Comments
 (0)