Apruve_B2B_Payment_Gateway - Version 2.0.1

Version Notes

Stable Version

Download this release

Release Info

Developer Apruve
Extension Apruve_B2B_Payment_Gateway
Version 2.0.1
Comparing to
See all releases


Code changes from version 2.0.0 to 2.0.1

app/code/community/Apruve/ApruvePayment/Model/Api/Abstract.php CHANGED
@@ -85,8 +85,13 @@ abstract class Apruve_ApruvePayment_Model_Api_Abstract
85
}
86
87
if ($http_status < 200 || $http_status >= 300) {
88
- $message = "Request Error: Request could not be processed";
89
- $success = false;
90
}
91
92
$result['success'] = $success;
@@ -218,9 +223,9 @@ abstract class Apruve_ApruvePayment_Model_Api_Abstract
218
{
219
$http = $secure ? 'https://' : 'http://';
220
if($this->getIsTestMode()) {
221
- return $http . 'test.apruve.com/';
222
} else {
223
- return $http . 'app.apruve.com/';
224
}
225
}
226
85
}
86
87
if ($http_status < 200 || $http_status >= 300) {
88
+ $response_decoded = json_decode($response);
89
+ if (isset($response_decoded->error)) {
90
+ $message = $response_decoded->error;
91
+ } else {
92
+ $message = "Request Error: Request could not be processed";
93
+ }
94
+ $success = false;
95
}
96
97
$result['success'] = $success;
223
{
224
$http = $secure ? 'https://' : 'http://';
225
if($this->getIsTestMode()) {
226
+ return $http . 'test.apruve.com/';
227
} else {
228
+ return $http . 'app.apruve.com/';
229
}
230
}
231
app/code/community/Apruve/ApruvePayment/Model/Api/Rest/Account.php ADDED
@@ -0,0 +1,121 @@
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Apache License, Version 2.0
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/Apache-2.0
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@apruve.com so we can send you a copy immediately.
14
+ *
15
+ * @category Apruve
16
+ * @package Apruve_Payment
17
+ * @copyright Copyright (coffee) 2017 Apruve, Inc. (http://www.apruve.com).
18
+ * @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
19
+ */
20
+
21
+ /**
22
+ * Class Apruve_ApruvePayment_Model_Api_Rest_Account
23
+ *
24
+ * Provide rest methods to communicate with apruve
25
+ */
26
+ class Apruve_ApruvePayment_Model_Api_Rest_Account extends Apruve_ApruvePayment_Model_Api_Rest
27
+ {
28
+
29
+ /**
30
+ * Corporate account general fields
31
+ * @var array
32
+ */
33
+ protected $_fields = array(
34
+ //required
35
+ 'id',
36
+ 'customer_uuid',
37
+ 'merchant_uuid',
38
+ 'type',
39
+ 'payment_term_strategy_name',
40
+ 'name',
41
+ 'authorized_buyers' => array(),
42
+ );
43
+
44
+ /**
45
+ * Retrieve an existing corporate account by its ID in apruve
46
+ *
47
+ * @param string $apruveEmail
48
+ * @param string $apruveMerchantId
49
+ * @return $result string
50
+ */
51
+ public function getCorporateAccount($email)
52
+ {
53
+ $data = json_encode([
54
+ 'email' => urlencode($email)
55
+ ]);
56
+
57
+ $curlOptions = [];
58
+ $curlOptions[CURLOPT_POSTFIELDS] = $data;
59
+
60
+ $result = $this->execCurlRequest($this->_getCorporateAccountUrl(), 'GET', $curlOptions);
61
+
62
+ if ($result) {
63
+ if($result['success'] == false){
64
+ Mage::throwException(Mage::helper('apruvepayment')->__($result['messsage']));
65
+ }
66
+ $this->_fields = $result['response'];
67
+ return $this->_fields;
68
+ } else {
69
+ Mage::throwException(Mage::helper('apruvepayment')->__('An unknown error has occurred. Please try again or contact Apruve support.'));
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Retrieve an the first instance of the buyers shopper_id
75
+ *
76
+ * @param string $mail
77
+ * @return $result string
78
+ */
79
+ public function getShopperId($email)
80
+ {
81
+ $corperateAccountArray = $this->_fields[0];
82
+ foreach ($corperateAccountArray['authorized_buyers'] as $buyer) {
83
+ if (strcasecmp($buyer['email'],$email) == 0) {
84
+ return $buyer['id'];
85
+ }
86
+ }
87
+ Mage::throwException(Mage::helper('apruvepayment')->__('Couldn\'t find a shopper with that email address at Apruve.'));
88
+ }
89
+
90
+ /**
91
+ * Retrieve the payment term
92
+ *
93
+ * Get url for an Apruve corporate account
94
+ * @return string
95
+ */
96
+ public function getPaymentTerm()
97
+ {
98
+ if ($this->getCorporateAccountId()) {
99
+ return array('corporate_account_id' => $this->getCorporateAccountId());
100
+ } else {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ public function getCorporateAccountId()
106
+ {
107
+ return $this->_fields[0]['id'];
108
+ }
109
+
110
+ /**
111
+ * Retrieve an existing corporate account by its ID in apruve
112
+ *
113
+ * Get url for an Apruve corporate account
114
+ * @return string
115
+ */
116
+ protected function _getCorporateAccountUrl()
117
+ {
118
+ return $this->getBaseUrl(true) . $this->getApiUrl() . 'merchants/' . $this->getMerchantKey() . '/corporate_accounts';
119
+
120
+ }
121
+ }
app/code/community/Apruve/ApruvePayment/Model/Api/Rest/Order.php CHANGED
@@ -94,7 +94,7 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
94
$apruveEntity->setMagentoId($order->getIncrementId());
95
$apruveEntity->setEntityType('order');
96
$apruveEntity->save();
97
- } catch(Exception $e) {
98
Mage::helper('apruvepayment')->logException('Couldn\'t update the order: ' . $e->getMessage());
99
Mage::throwException(Mage::helper('apruvepayment')->__('Couldn\'t update order.'));
100
}
@@ -104,18 +104,92 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
104
/**
105
* Update an existing order by its ID in apruve
106
*
107
* @param string $apruveOrderId
108
* @param Mage_Sales_Model_Order $order
109
* @return string $result
110
*/
111
public function updateOrder($apruveOrderId, $order)
112
{
113
$lineItems = [];
114
// get discount line item
115
if(($discountItem = $this->_getDiscountItem($order))) {
116
$lineItems[] = $discountItem;
117
}
118
-
119
$data = json_encode(array(
120
'order' => array(
121
'merchant_order_id' => $order->getIncrementId(),
@@ -126,10 +200,8 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
126
'order_items' => $lineItems
127
)
128
));
129
-
130
$curlOptions = [];
131
$curlOptions[CURLOPT_POSTFIELDS] = $data;
132
-
133
$result = $this->execCurlRequest($this->_getUpdateOrderUrl($apruveOrderId), 'PUT', $curlOptions);
134
if($result['success'] == true) {
135
Mage::helper('apruvepayment')->logException('Order updated successfully...');
@@ -149,7 +221,7 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
149
{
150
$orderIncrementId = $quote->getReservedOrderId();
151
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
152
- if(!$order->getId()) {
153
Mage::throwException(Mage::helper('apruvepayment')->__('Couldn\'t load the order.'));
154
}
155
return $order;
@@ -165,7 +237,7 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
165
public function finalizeOrder($apruveOrderId, $order)
166
{
167
$result = $this->execCurlRequest($this->_getFinalizeOrderUrl($apruveOrderId), 'POST');
168
- if($result['success'] == true) {
169
$this->_updateOrderId($apruveOrderId, $order);
170
Mage::helper('apruvepayment')->logException('Order finalized successfully...');
171
}
@@ -184,4 +256,29 @@ class Apruve_ApruvePayment_Model_Api_Rest_Order extends Apruve_ApruvePayment_Mod
184
$result = $this->execCurlRequest($this->_getCancelOrderUrl($apruveOrderId), 'POST');
185
return $result;
186
}
187
- }
94
$apruveEntity->setMagentoId($order->getIncrementId());
95
$apruveEntity->setEntityType('order');
96
$apruveEntity->save();
97
+ } catch (Exception $e) {
98
Mage::helper('apruvepayment')->logException('Couldn\'t update the order: ' . $e->getMessage());
99
Mage::throwException(Mage::helper('apruvepayment')->__('Couldn\'t update order.'));
100
}
104
/**
105
* Update an existing order by its ID in apruve
106
*
107
+ * This also determines if the order is from the frontend or the backend
108
+ *
109
* @param string $apruveOrderId
110
* @param Mage_Sales_Model_Order $order
111
* @return string $result
112
*/
113
public function updateOrder($apruveOrderId, $order)
114
+ {
115
+ if (Mage::app()->getStore()->isAdmin()) {
116
+ return $this->_updateAdminOrder($apruveOrderId, $order);
117
+ } else {
118
+ return $this->_updateFrontendOrder($apruveOrderId, $order);
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Update an existing admin order by its ID in apruve
124
+ *
125
+ * @param string $apruveOrderId
126
+ * @param Mage_Sales_Model_Order $order
127
+ * @return string $result
128
+ */
129
+ protected function _updateAdminOrder($apruveOrderId, $order)
130
+ {
131
+ $result = null;
132
+ $lineItems = $this->_getLineItems($order);
133
+
134
+ // get discount line item
135
+ if (($discountItem = $this->_getDiscountItem($order))) {
136
+ $lineItems[] = $discountItem;
137
+ }
138
+ $corporateAccount = Mage::getModel('apruvepayment/api_rest_account');
139
+ $corporateAccount->getCorporateAccount($order->getCustomerEmail());
140
+ $shopper_id = $corporateAccount->getShopperId($order->getCustomerEmail());
141
+ $payment_term = $corporateAccount->getPaymentTerm();
142
+
143
+ if ($shopper_id) {
144
+ $data = json_encode(array(
145
+ 'order' => array(
146
+ 'merchant_id' => $this->getMerchantKey(),
147
+ 'merchant_order_id' => $order->getIncrementId(),
148
+ 'shopper_id' => $shopper_id,
149
+ 'payment_term' => $payment_term,
150
+ 'amount_cents' => $this->convertPrice($order->getBaseGrandTotal()),
151
+ 'shipping_cents' => $this->convertPrice($order->getBaseShippingAmount()),
152
+ 'tax_cents' => $this->convertPrice($order->getBaseTaxAmount()),
153
+ 'invoice_on_create' => 'false',
154
+ 'order_items' => $lineItems
155
+ )
156
+ ));
157
+
158
+ $curlOptions = [];
159
+ $curlOptions[CURLOPT_POSTFIELDS] = $data;
160
+
161
+ if (@$apruveOrderId === null) {
162
+ $curlAction = 'POST';
163
+ } else {
164
+ $curlAction = 'PUT';
165
+ }
166
+
167
+ $result = $this->execCurlRequest($this->_getUpdateOrderUrl($apruveOrderId), $curlAction, $curlOptions);
168
+ if ($result['success'] == true) {
169
+ if ($apruveOrderId == null) {
170
+ $apruveOrderId = $result['response']['id'];
171
+ }
172
+ Mage::helper('apruvepayment')->logException('Order updated successfully...');
173
+ $this->_updateOrderId($apruveOrderId, $order);
174
+ }
175
+ }
176
+ return $result;
177
+ }
178
+
179
+ /**
180
+ * Update an existing frontend order by its ID in apruve
181
+ *
182
+ * @param string $apruveOrderId
183
+ * @param Mage_Sales_Model_Order $order
184
+ * @return string $result
185
+ */
186
+ protected function _updateFrontendOrder($apruveOrderId, $order)
187
{
188
$lineItems = [];
189
// get discount line item
190
if(($discountItem = $this->_getDiscountItem($order))) {
191
$lineItems[] = $discountItem;
192
}
193
$data = json_encode(array(
194
'order' => array(
195
'merchant_order_id' => $order->getIncrementId(),
200
'order_items' => $lineItems
201
)
202
));
203
$curlOptions = [];
204
$curlOptions[CURLOPT_POSTFIELDS] = $data;
205
$result = $this->execCurlRequest($this->_getUpdateOrderUrl($apruveOrderId), 'PUT', $curlOptions);
206
if($result['success'] == true) {
207
Mage::helper('apruvepayment')->logException('Order updated successfully...');
221
{
222
$orderIncrementId = $quote->getReservedOrderId();
223
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
224
+ if (!$order->getId()) {
225
Mage::throwException(Mage::helper('apruvepayment')->__('Couldn\'t load the order.'));
226
}
227
return $order;
237
public function finalizeOrder($apruveOrderId, $order)
238
{
239
$result = $this->execCurlRequest($this->_getFinalizeOrderUrl($apruveOrderId), 'POST');
240
+ if ($result['success'] == true) {
241
$this->_updateOrderId($apruveOrderId, $order);
242
Mage::helper('apruvepayment')->logException('Order finalized successfully...');
243
}
256
$result = $this->execCurlRequest($this->_getCancelOrderUrl($apruveOrderId), 'POST');
257
return $result;
258
}
259
+
260
+ /**
261
+ * Get Magento line items prepared for Apruve
262
+ *
263
+ * @param $lineItems Mage_Sales_Model_Order_Item
264
+ * @return $items array
265
+ */
266
+ protected function _getLineItems($order)
267
+ {
268
+ $items = array();
269
+
270
+ foreach ($order->getAllVisibleItems() as $item) {
271
+ $items[] = array(
272
+ 'title' => $item->getName(),
273
+ 'description' => $item->getDescription(),
274
+ 'price_total_cents' => $item->getRowTotal() * 100,
275
+ 'price_ea_cents' => $item->getPrice() * 100,
276
+ 'quantity' => $item->getQtyOrdered(),
277
+ 'sku' => $item->getSku(),
278
+ 'view_product_url' => $item->getProduct()->getUrlInStore()
279
+ );
280
+ }
281
+
282
+ return $items;
283
+ }
284
+ }
app/code/community/Apruve/ApruvePayment/Model/Api/Rest/Shipment.php CHANGED
@@ -143,7 +143,7 @@ class Apruve_ApruvePayment_Model_Api_Rest_Shipment extends Apruve_ApruvePayment_
143
'currency' => $this->getCurrency(),
144
'merchant_notes' => $comment->getComment(),
145
'shipment_items' => $items,
146
- 'status' => 'FULFILLED'
147
]);
148
149
return $data;
@@ -191,6 +191,11 @@ class Apruve_ApruvePayment_Model_Api_Rest_Shipment extends Apruve_ApruvePayment_
191
$apruveShipmentId = isset($result['response']['id']) ? $result['response']['id'] : '';
192
if($result['success'] == true) {
193
$this->_updateShipmentId($apruveShipmentId, $shipment);
194
}
195
return $result;
196
}
@@ -211,4 +216,4 @@ class Apruve_ApruvePayment_Model_Api_Rest_Shipment extends Apruve_ApruvePayment_
211
$result = $this->execCurlRequest($this->_getUpdateShipmentUrl($apruveInvoiceId, $apruveShipmentId), 'PUT', $curlOptions);
212
return $result;
213
}
214
- }
143
'currency' => $this->getCurrency(),
144
'merchant_notes' => $comment->getComment(),
145
'shipment_items' => $items,
146
+ 'status' => 'fulfilled'
147
]);
148
149
return $data;
191
$apruveShipmentId = isset($result['response']['id']) ? $result['response']['id'] : '';
192
if($result['success'] == true) {
193
$this->_updateShipmentId($apruveShipmentId, $shipment);
194
+ $order = Mage::getModel('sales/order')->loadByIncrementId($shipment->getOrder());
195
+ if($order->getStatus() == 'buyer_approved'){
196
+ $order->setStatus('pending');
197
+ $order->save();
198
+ }
199
}
200
return $result;
201
}
216
$result = $this->execCurlRequest($this->_getUpdateShipmentUrl($apruveInvoiceId, $apruveShipmentId), 'PUT', $curlOptions);
217
return $result;
218
}
219
+ }
app/code/community/Apruve/ApruvePayment/Model/PaymentMethod.php CHANGED
@@ -24,21 +24,21 @@
24
*/
25
class Apruve_ApruvePayment_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
26
{
27
- const PAYMENT_METHOD_CODE = 'apruvepayment';
28
-
29
- protected $_code = self::PAYMENT_METHOD_CODE;
30
- protected $_formBlockType = 'apruvepayment/payment_form';
31
-
32
- protected $_canAuthorize = true;
33
- protected $_canCapture = true;
34
- protected $_canVoid = true;
35
- protected $_canUseInternal = false;
36
- protected $_canUseCheckout = true;
37
- protected $_canCreateBillingAgreement = true;
38
- protected $_isGateway = true;
39
- protected $_canManageRecurringProfiles = false;
40
- protected $_canUseForMultishipping = false;
41
- protected $_canReviewPayment = true;
42
43
/**
44
* Can edit order (renew order)
@@ -76,7 +76,18 @@ class Apruve_ApruvePayment_Model_PaymentMethod extends Mage_Payment_Model_Method
76
{
77
parent::validate();
78
79
- if (!$this->getInfoInstance()->getAdditionalInformation('aprt')) {
80
Mage::throwException('Something is going wrong, try again to post order with apruve.');
81
}
82
}
@@ -105,6 +116,7 @@ class Apruve_ApruvePayment_Model_PaymentMethod extends Mage_Payment_Model_Method
105
/** @var Apruve_ApruvePayment_Model_Api_Rest_Order $orderApi */
106
$orderApi = Mage::getModel('apruvepayment/api_rest_order');
107
108
$updateResult = $orderApi->updateOrder($token, $order);
109
if (!$updateResult || !$updateResult['success']) {
110
Mage::throwException('Couldn\'t update order in Apruve.');
24
*/
25
class Apruve_ApruvePayment_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
26
{
27
+ const PAYMENT_METHOD_CODE = 'apruvepayment';
28
+
29
+ protected $_code = self::PAYMENT_METHOD_CODE;
30
+ protected $_formBlockType = 'apruvepayment/payment_form';
31
+
32
+ protected $_canAuthorize = true;
33
+ protected $_canCapture = true;
34
+ protected $_canVoid = true;
35
+ protected $_canUseInternal = true;
36
+ protected $_canUseCheckout = true;
37
+ protected $_canCreateBillingAgreement = true;
38
+ protected $_isGateway = true;
39
+ protected $_canManageRecurringProfiles = false;
40
+ protected $_canUseForMultishipping = false;
41
+ protected $_canReviewPayment = true;
42
43
/**
44
* Can edit order (renew order)
76
{
77
parent::validate();
78
79
+ if (Mage::app()->getStore()->isAdmin()) {
80
+ $paymentInfo = $this->getInfoInstance();
81
+ if ($paymentInfo instanceof Mage_Sales_Model_Order_Payment) {
82
+ $billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
83
+ } else {
84
+ $billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
85
+ }
86
+ if (!$this->canUseForCountry($billingCountry)) {
87
+ Mage::throwException($this->_getHelper()->__('Selected payment type is not allowed for billing country.'));
88
+ }
89
+ return $this;
90
+ } elseif (!$this->getInfoInstance()->getAdditionalInformation('aprt')) {
91
Mage::throwException('Something is going wrong, try again to post order with apruve.');
92
}
93
}
116
/** @var Apruve_ApruvePayment_Model_Api_Rest_Order $orderApi */
117
$orderApi = Mage::getModel('apruvepayment/api_rest_order');
118
119
+
120
$updateResult = $orderApi->updateOrder($token, $order);
121
if (!$updateResult || !$updateResult['success']) {
122
Mage::throwException('Couldn\'t update order in Apruve.');
app/code/community/Apruve/ApruvePayment/controllers/WebhookController.php CHANGED
@@ -18,7 +18,6 @@
18
* @copyright Copyright (coffee) 2014 Apruve, Inc. (http://www.apruve.com).
19
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
20
*/
21
-
22
class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_Action
23
{
24
public function updateOrderStatusAction()
@@ -26,7 +25,7 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
26
$hash = $this->_getHashedQueryString();
27
28
// if the hash doesn't match the data sent by Apruve terminate the code
29
- if(!isset($_GET[$hash])) {
30
header("HTTP/1.1 404 Not Found");
31
exit;
32
}
@@ -41,31 +40,34 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
41
$entity = $data->entity;
42
43
// check the event triggered in Apruve to call appropriate action in Magento
44
- if($event == 'invoice.closed') {
45
$invoiceId = $entity->merchant_invoice_id;
46
- if(!$this->_capturePayment($invoiceId)) {
47
header("HTTP/1.1 404 Not Found");
48
exit;
49
};
50
- } elseif($event == 'order.accepted' ) {
51
exit; // should not be triggering anything in magento
52
53
$orderId = $entity->merchant_order_id;
54
- if(!$this->_changeOrderStatus($orderId)) {
55
header("HTTP/1.1 404 Not Found");
56
exit;
57
};
58
- } elseif($event == 'order.canceled' ) {
59
$orderId = $entity->merchant_order_id;
60
- if(!$this->_cancelOrder($orderId)) {
61
header("HTTP/1.1 404 Not Found");
62
exit;
63
};
64
- } elseif($event == 'payment_term.accepted' ) {
65
- header("HTTP/1.1 404 Not Found");
66
- exit;
67
}
68
- } catch(Exception $e) {
69
Mage::helper('apruvepayment')->logException('Error for transaction UUID: ' . $data->uuid . '. Message: ' . $e->getMessage());
70
}
71
@@ -81,7 +83,7 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
81
*/
82
protected function _capturePayment($invoiceId)
83
{
84
- if($invoiceId) {
85
/** @var Mage_Sales_Model_Order_Invoice_Api $iApi */
86
$iApi = Mage::getModel('sales/order_invoice_api');
87
$iApi->capture($invoiceId);
@@ -99,10 +101,10 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
99
protected function _changeOrderStatus($orderId)
100
{
101
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
102
- Mage::helper('apruvepayment')->logException($order->getData());
103
- Mage::helper('apruvepayment')->logException($orderId);
104
105
- if($order && $order->getId() && !$order->isCanceled()) {
106
Mage::helper('apruvepayment')->logException('creating invoice...');
107
$result = $this->_createInvoice($order->getIncrementId());
108
return $result;
@@ -116,9 +118,32 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
116
* @param string $orderId
117
* @return bool
118
*/
119
protected function _createInvoice($orderId)
120
{
121
- if($orderId) {
122
/** @var Mage_Sales_Model_Order_Invoice_Api $iApi */
123
$iApi = Mage::getModel('sales/order_invoice_api');
124
$invoiceId = $iApi->create($orderId, array());
@@ -136,7 +161,7 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
136
protected function _cancelOrder($orderId)
137
{
138
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
139
- if($order && $order->getId() && !$order->isCanceled()) {
140
$order->cancel();
141
$order->save();
142
return true;
@@ -176,7 +201,7 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
176
}
177
178
/**
179
- * Get the hashed string id based on Apruve merchant id and API key
180
*
181
* @return string
182
*/
@@ -184,8 +209,8 @@ class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_
184
{
185
$merchantKey = Mage::getStoreConfig('payment/apruvepayment/merchant');
186
$apiKey = Mage::getStoreConfig('payment/apruvepayment/api');
187
- $data = $apiKey.$merchantKey;
188
$hash = hash('sha256', $data);
189
return $hash;
190
}
191
- }
18
* @copyright Copyright (coffee) 2014 Apruve, Inc. (http://www.apruve.com).
19
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
20
*/
21
class Apruve_ApruvePayment_WebhookController extends Mage_Core_Controller_Front_Action
22
{
23
public function updateOrderStatusAction()
25
$hash = $this->_getHashedQueryString();
26
27
// if the hash doesn't match the data sent by Apruve terminate the code
28
+ if (!isset($_GET[$hash])) {
29
header("HTTP/1.1 404 Not Found");
30
exit;
31
}
40
$entity = $data->entity;
41
42
// check the event triggered in Apruve to call appropriate action in Magento
43
+ if ($event == 'invoice.closed') {
44
$invoiceId = $entity->merchant_invoice_id;
45
+ if (!$this->_capturePayment($invoiceId)) {
46
header("HTTP/1.1 404 Not Found");
47
exit;
48
};
49
+ } elseif ($event == 'order.accepted') {
50
exit; // should not be triggering anything in magento
51
52
$orderId = $entity->merchant_order_id;
53
+ if (!$this->_changeOrderStatus($orderId)) {
54
+ header("HTTP/1.1 404 Not Found");
55
+ exit;
56
+ };
57
+ } elseif ($event == 'order.canceled') {
58
+ $orderId = $entity->merchant_order_id;
59
+ if (!$this->_cancelOrder($orderId)) {
60
header("HTTP/1.1 404 Not Found");
61
exit;
62
};
63
+ } elseif ($event == 'payment_term.accepted') {
64
$orderId = $entity->merchant_order_id;
65
+ if (!$this->_paymentTermAccepted($orderId)) {
66
header("HTTP/1.1 404 Not Found");
67
exit;
68
};
69
}
70
+ } catch (Exception $e) {
71
Mage::helper('apruvepayment')->logException('Error for transaction UUID: ' . $data->uuid . '. Message: ' . $e->getMessage());
72
}
73
83
*/
84
protected function _capturePayment($invoiceId)
85
{
86
+ if ($invoiceId) {
87
/** @var Mage_Sales_Model_Order_Invoice_Api $iApi */
88
$iApi = Mage::getModel('sales/order_invoice_api');
89
$iApi->capture($invoiceId);
101
protected function _changeOrderStatus($orderId)
102
{
103
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
104
+ Mage::helper('apruvepayment')->logException($order->getData());
105
+ Mage::helper('apruvepayment')->logException($orderId);
106
107
+ if ($order && $order->getId() && !$order->isCanceled()) {
108
Mage::helper('apruvepayment')->logException('creating invoice...');
109
$result = $this->_createInvoice($order->getIncrementId());
110
return $result;
118
* @param string $orderId
119
* @return bool
120
*/
121
+ protected function _paymentTermAccepted($orderId)
122
+ {
123
+ $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
124
+ Mage::helper('apruvepayment')->logException($order->getData());
125
+ Mage::helper('apruvepayment')->logException($orderId);
126
+
127
+ if ($order) {
128
+ Mage::helper('apruvepayment')->logException('creating payment accepted...');
129
+
130
+ $order->setStatus('buyer_approved');
131
+
132
+ $order->save();
133
+ return true;
134
+ }
135
+ return false;
136
+ }
137
+
138
+ /**Ball
139
+ * Change the order status based on the order increment ID
140
+ *
141
+ * @param string $orderId
142
+ * @return bool
143
+ */
144
protected function _createInvoice($orderId)
145
{
146
+ if ($orderId) {
147
/** @var Mage_Sales_Model_Order_Invoice_Api $iApi */
148
$iApi = Mage::getModel('sales/order_invoice_api');
149
$invoiceId = $iApi->create($orderId, array());
161
protected function _cancelOrder($orderId)
162
{
163
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
164
+ if ($order && $order->getId() && !$order->isCanceled()) {
165
$order->cancel();
166
$order->save();
167
return true;
201
}
202
203
/**
204
+ * Get the hashed string id based on Apruve merchant id and API key
205
*
206
* @return string
207
*/
209
{
210
$merchantKey = Mage::getStoreConfig('payment/apruvepayment/merchant');
211
$apiKey = Mage::getStoreConfig('payment/apruvepayment/api');
212
+ $data = $apiKey . $merchantKey;
213
$hash = hash('sha256', $data);
214
return $hash;
215
}
216
+ }
app/code/community/Apruve/ApruvePayment/etc/config.xml CHANGED
@@ -1,136 +1,149 @@
1
- <?xml version="1.0"?>
2
- <config>
3
- <modules>
4
- <Apruve_ApruvePayment>
5
- <version>2.0.0</version>
6
- </Apruve_ApruvePayment>
7
- </modules>
8
- <global>
9
- <models>
10
- <apruvepayment>
11
- <class>Apruve_ApruvePayment_Model</class>
12
- <resourceModel>apruvepayment_resource</resourceModel>
13
- </apruvepayment>
14
- <apruvepayment_resource>
15
- <class>Apruve_ApruvePayment_Model_Resource</class>
16
- <entities>
17
- <entity>
18
- <table>apruve_entity</table>
19
- </entity>
20
- </entities>
21
- </apruvepayment_resource>
22
- <sales>
23
- <rewrite>
24
- <service_order>Apruve_ApruvePayment_Model_Sales_Service_Order</service_order>
25
- </rewrite>
26
- </sales>
27
- </models>
28
- <resources>
29
- <apruvepayment_setup>
30
- <setup>
31
- <module>Apruve_ApruvePayment</module>
32
- </setup>
33
- </apruvepayment_setup>
34
- <apruvepayment_write>
35
- <connection>
36
- <use>core_write</use>
37
- </connection>
38
- </apruvepayment_write>
39
- <apruvepayment_read>
40
- <connection>
41
- <use>core_read</use>
42
- </connection>
43
- </apruvepayment_read>
44
- </resources>
45
- <blocks>
46
- <apruvepayment>
47
- <class>Apruve_ApruvePayment_Block</class>
48
- </apruvepayment>
49
- <adminhtml>
50
- <rewrite>
51
- <sales_order_view>Apruve_ApruvePayment_Block_Adminhtml_Sales_Order_View</sales_order_view>
52
- </rewrite>
53
- </adminhtml>
54
- </blocks>
55
- <helpers>
56
- <apruvepayment>
57
- <class>Apruve_ApruvePayment_Helper</class>
58
- </apruvepayment>
59
- </helpers>
60
- <events>
61
- <checkout_submit_all_after>
62
- <observers>
63
- <apruvepayment>
64
- <class>apruvepayment/observer</class>
65
- <method>finalizeOrder</method>
66
- </apruvepayment>
67
- </observers>
68
- </checkout_submit_all_after>
69
- <order_cancel_after>
70
- <observers>
71
- <apruvepayment>
72
- <class>apruvepayment/observer</class>
73
- <method>cancelOrder</method>
74
- </apruvepayment>
75
- </observers>
76
- </order_cancel_after>
77
- <sales_order_invoice_save_after>
78
- <observers>
79
- <apruvepayment>
80
- <class>apruvepayment/observer</class>
81
- <method>createInvoice</method>
82
- </apruvepayment>
83
- </observers>
84
- </sales_order_invoice_save_after>
85
- <sales_order_shipment_save_after>
86
- <observers>
87
- <apruvepayment>
88
- <class>apruvepayment/observer</class>
89
- <method>createShipment</method>
90
- </apruvepayment>
91
- </observers>
92
- </sales_order_shipment_save_after>
93
- <sales_order_shipment_comment_save_after>
94
- <observers>
95
- <apruvepayment>
96
- <class>apruvepayment/observer</class>
97
- <method>createShipment</method>
98
- </apruvepayment>
99
- </observers>
100
- </sales_order_shipment_comment_save_after>
101
- </events>
102
- </global>
103
- <frontend>
104
- <routers>
105
- <apruvepayment>
106
- <use>standard</use>
107
- <args>
108
- <module>Apruve_ApruvePayment</module>
109
- <frontName>apruvepayment</frontName>
110
- </args>
111
- </apruvepayment>
112
- </routers>
113
- <layout>
114
- <updates>
115
- <apruvepayment>
116
- <file>apruvepayment.xml</file>
117
- </apruvepayment>
118
- </updates>
119
- </layout>
120
- </frontend>
121
- <default>
122
- <payment>
123
- <apruvepayment>
124
- <mode>1</mode>
125
- <version>v4</version>
126
- <autosubmit>1</autosubmit>
127
- <active>1</active>
128
- <model>apruvepayment/paymentMethod</model>
129
- <order_status>pending_payment</order_status>
130
- <title>Pay with Apruve</title>
131
- <payment_action>authorize</payment_action>
132
- <log>0</log>
133
- </apruvepayment>
134
- </payment>
135
- </default>
136
- </config>
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Apruve_ApruvePayment>
5
+ <version>2.0.1</version>
6
+ </Apruve_ApruvePayment>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <apruvepayment>
11
+ <class>Apruve_ApruvePayment_Model</class>
12
+ <resourceModel>apruvepayment_resource</resourceModel>
13
+ </apruvepayment>
14
+ <apruvepayment_resource>
15
+ <class>Apruve_ApruvePayment_Model_Resource</class>
16
+ <entities>
17
+ <entity>
18
+ <table>apruve_entity</table>
19
+ </entity>
20
+ </entities>
21
+ </apruvepayment_resource>
22
+ <sales>
23
+ <rewrite>
24
+ <service_order>Apruve_ApruvePayment_Model_Sales_Service_Order</service_order>
25
+ </rewrite>
26
+ </sales>
27
+ </models>
28
+ <resources>
29
+ <apruvepayment_setup>
30
+ <setup>
31
+ <module>Apruve_ApruvePayment</module>
32
+ </setup>
33
+ </apruvepayment_setup>
34
+ <apruvepayment_write>
35
+ <connection>
36
+ <use>core_write</use>
37
+ </connection>
38
+ </apruvepayment_write>
39
+ <apruvepayment_read>
40
+ <connection>
41
+ <use>core_read</use>
42
+ </connection>
43
+ </apruvepayment_read>
44
+ </resources>
45
+ <blocks>
46
+ <apruvepayment>
47
+ <class>Apruve_ApruvePayment_Block</class>
48
+ </apruvepayment>
49
+ <adminhtml>
50
+ <rewrite>
51
+ <sales_order_view>Apruve_ApruvePayment_Block_Adminhtml_Sales_Order_View</sales_order_view>
52
+ </rewrite>
53
+ </adminhtml>
54
+ </blocks>
55
+ <helpers>
56
+ <apruvepayment>
57
+ <class>Apruve_ApruvePayment_Helper</class>
58
+ </apruvepayment>
59
+ </helpers>
60
+ <events>
61
+ <checkout_submit_all_after>
62
+ <observers>
63
+ <apruvepayment>
64
+ <class>apruvepayment/observer</class>
65
+ <method>finalizeOrder</method>
66
+ </apruvepayment>
67
+ </observers>
68
+ </checkout_submit_all_after>
69
+ <order_cancel_after>
70
+ <observers>
71
+ <apruvepayment>
72
+ <class>apruvepayment/observer</class>
73
+ <method>cancelOrder</method>
74
+ </apruvepayment>
75
+ </observers>
76
+ </order_cancel_after>
77
+ <sales_order_invoice_save_after>
78
+ <observers>
79
+ <apruvepayment>
80
+ <class>apruvepayment/observer</class>
81
+ <method>createInvoice</method>
82
+ </apruvepayment>
83
+ </observers>
84
+ </sales_order_invoice_save_after>
85
+ <sales_order_shipment_save_after>
86
+ <observers>
87
+ <apruvepayment>
88
+ <class>apruvepayment/observer</class>
89
+ <method>createShipment</method>
90
+ </apruvepayment>
91
+ </observers>
92
+ </sales_order_shipment_save_after>
93
+ <sales_order_shipment_comment_save_after>
94
+ <observers>
95
+ <apruvepayment>
96
+ <class>apruvepayment/observer</class>
97
+ <method>createShipment</method>
98
+ </apruvepayment>
99
+ </observers>
100
+ </sales_order_shipment_comment_save_after>
101
+ </events>
102
+ <sales>
103
+ <order>
104
+ <states>
105
+ <buyer_approved translate="label">
106
+ <label>Buyer Approved</label>
107
+ <statuses>
108
+ <order_received default="0"/>
109
+ </statuses>
110
+ <visible_on_front>1</visible_on_front>
111
+ </buyer_approved>
112
+ </states>
113
+ </order>
114
+ </sales>
115
+ </global>
116
+ <frontend>
117
+ <routers>
118
+ <apruvepayment>
119
+ <use>standard</use>
120
+ <args>
121
+ <module>Apruve_ApruvePayment</module>
122
+ <frontName>apruvepayment</frontName>
123
+ </args>
124
+ </apruvepayment>
125
+ </routers>
126
+ <layout>
127
+ <updates>
128
+ <apruvepayment>
129
+ <file>apruvepayment.xml</file>
130
+ </apruvepayment>
131
+ </updates>
132
+ </layout>
133
+ </frontend>
134
+ <default>
135
+ <payment>
136
+ <apruvepayment>
137
+ <mode>1</mode>
138
+ <version>v4</version>
139
+ <autosubmit>1</autosubmit>
140
+ <active>1</active>
141
+ <model>apruvepayment/paymentMethod</model>
142
+ <order_status>pending_payment</order_status>
143
+ <title>Pay with Apruve</title>
144
+ <payment_action>authorize</payment_action>
145
+ <log>0</log>
146
+ </apruvepayment>
147
+ </payment>
148
+ </default>
149
+ </config>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
<?xml version="1.0"?>
2
<package>
3
<name>Apruve_B2B_Payment_Gateway</name>
4
- <version>2.0.0</version>
5
<stability>stable</stability>
6
<license uri="http://www.apache.org/licenses/LICENSE-2.0">Apache License, 2.0</license>
7
<channel>community</channel>
@@ -10,9 +10,9 @@
10
<description>Payments designed for business spending. Increase sales and reduce cart abandonment by allowing your shoppers to send purchases directly to their company or client for payment, simply by using an email address instead of a credit card. Learn more and sign up for a free merchant account at http://www.apruve.com.</description>
11
<notes>Stable Version</notes>
12
<authors><author><name>Apruve</name><user>Apruve</user><email>magento@apruve.com</email></author></authors>
13
- <date>2017-01-11</date>
14
- <time>09:17:07</time>
15
- <contents><target name="magecommunity"><dir name="Apruve"><dir name="ApruvePayment"><dir name="Block"><dir name="Admin"><file name="Webhook.php" hash="5a4eb68f48306322a973f1f837f53ac8"/></dir><dir name="Adminhtml"><dir name="Sales"><dir name="Order"><file name="View.php" hash="517c950361e27545c3ff841cd7bccae4"/></dir></dir></dir><dir name="Payment"><file name="Form.php" hash="c84481487b8a831fc382d90c76eeb962"/></dir></dir><dir name="Helper"><file name="Data.php" hash="c6d29ea8c507e36eae18d1a3491cb7f1"/></dir><file name="LICENSE.txt" hash="9b76dd4b62de4f9840b5d462fcb25e39"/><dir name="Model"><dir name="Api"><file name="Abstract.php" hash="2b93d9f6f9d686271ab2061fbfa6c09e"/><file name="Payment.php" hash="cdc85003d73cf6b9de6f82e0c163b320"/><dir name="Rest"><file name="Invoice.php" hash="7b2f037c9c30218cc379eb551c6f27cf"/><file name="Order.php" hash="d4b128741165c5a2f982b57d2f33f40e"/><file name="Shipment.php" hash="6a8541fa8b19f5bd9d6617c083c4e308"/></dir><file name="Rest.php" hash="c1071949a8dc1229d85cebae150985b7"/></dir><file name="Entity.php" hash="6652836c17449c86c8a599ea63079258"/><file name="Mode.php" hash="95e5f17903661df50f3dea2849adc1bb"/><file name="Observer.php" hash="7c162dfa058e307b9b299d4ed7581f60"/><file name="PaymentMethod.php" hash="dcabe292295d011244421d26a77fc3b7"/><dir name="Resource"><dir name="Entity"><file name="Collection.php" hash="7a8f8115e87d6deaf6d05501e8160863"/></dir><file name="Entity.php" hash="9a2298e1c979f7ce998f867a81728013"/></dir><dir name="Sales"><dir name="Service"><file name="Order.php" hash="807c36c87883a4f01f4f8f7d86b44415"/></dir></dir><file name="Version.php" hash="5d76f8613344d1dfc278add7c2fa3ebf"/></dir><dir name="controllers"><file name="WebhookController.php" hash="ecb19b3c1ce91deaec7e1debcde691af"/></dir><dir name="etc"><file name="config.xml" hash="39b4a1c67ceb27dba4571706284c211c"/><file name="system.xml" hash="5762459db1687ed91c2d855591227c7f"/></dir><dir name="sql"><dir name="apruvepayment_setup"><file name="install-2.0.0.php" hash="eb0b5b2ab790d8710a00a817c52ef2b1"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="apruvepayment.xml" hash="6ade24fa77f4f55ce0bdf4f5c79fd65c"/></dir><dir name="template"><dir name="apruvepayment"><file name="head.phtml" hash="36809c6a8fa5aad587b03e2107482bdf"/><dir name="payment"><file name="form.phtml" hash="d0bff443c44e35201ced6abffdf7ec8f"/></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Apruve_ApruvePayment.xml" hash="8e20101d5c688c8505302df6d73686fb"/></dir></target><target name="mage"><dir name="js"><dir name="Apruve"><file name="ApruvePayment.js" hash="a9fab0a38c6bc2923106e9925462ef3e"/></dir></dir></target></contents>
16
<compatible/>
17
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
</package>
1
<?xml version="1.0"?>
2
<package>
3
<name>Apruve_B2B_Payment_Gateway</name>
4
+ <version>2.0.1</version>
5
<stability>stable</stability>
6
<license uri="http://www.apache.org/licenses/LICENSE-2.0">Apache License, 2.0</license>
7
<channel>community</channel>
10
<description>Payments designed for business spending. Increase sales and reduce cart abandonment by allowing your shoppers to send purchases directly to their company or client for payment, simply by using an email address instead of a credit card. Learn more and sign up for a free merchant account at http://www.apruve.com.</description>
11
<notes>Stable Version</notes>
12
<authors><author><name>Apruve</name><user>Apruve</user><email>magento@apruve.com</email></author></authors>
13
+ <date>2017-03-10</date>
14
+ <time>07:41:55</time>
15
+ <contents><target name="magecommunity"><dir name="Apruve"><dir name="ApruvePayment"><dir name="Block"><dir name="Admin"><file name="Webhook.php" hash="5a4eb68f48306322a973f1f837f53ac8"/></dir><dir name="Adminhtml"><dir name="Sales"><dir name="Order"><file name="View.php" hash="517c950361e27545c3ff841cd7bccae4"/></dir></dir></dir><dir name="Payment"><file name="Form.php" hash="c84481487b8a831fc382d90c76eeb962"/></dir></dir><dir name="Helper"><file name="Data.php" hash="c6d29ea8c507e36eae18d1a3491cb7f1"/></dir><file name="LICENSE.txt" hash="9b76dd4b62de4f9840b5d462fcb25e39"/><dir name="Model"><dir name="Api"><file name="Abstract.php" hash="799c80a34d98fa19248720364fdcb733"/><file name="Payment.php" hash="cdc85003d73cf6b9de6f82e0c163b320"/><dir name="Rest"><file name="Account.php" hash="3ffe86022f08f9969fab56d0e12850db"/><file name="Invoice.php" hash="7b2f037c9c30218cc379eb551c6f27cf"/><file name="Order.php" hash="4b8ce16292d3ea593313ae4fb30a07cb"/><file name="Shipment.php" hash="fb8150febd9643cfb4fe3dd23d0c5be6"/></dir><file name="Rest.php" hash="c1071949a8dc1229d85cebae150985b7"/></dir><file name="Entity.php" hash="6652836c17449c86c8a599ea63079258"/><file name="Mode.php" hash="95e5f17903661df50f3dea2849adc1bb"/><file name="Observer.php" hash="7c162dfa058e307b9b299d4ed7581f60"/><file name="PaymentMethod.php" hash="6fc52aaad006d142162c690822925d5f"/><dir name="Resource"><dir name="Entity"><file name="Collection.php" hash="7a8f8115e87d6deaf6d05501e8160863"/></dir><file name="Entity.php" hash="9a2298e1c979f7ce998f867a81728013"/></dir><dir name="Sales"><dir name="Service"><file name="Order.php" hash="807c36c87883a4f01f4f8f7d86b44415"/></dir></dir><file name="Version.php" hash="5d76f8613344d1dfc278add7c2fa3ebf"/></dir><dir name="controllers"><file name="WebhookController.php" hash="fbaf0657861c73d2bbb7d1c372b960d4"/></dir><dir name="etc"><file name="config.xml" hash="ced9a00271a91454ebf6a25c79c8c2d6"/><file name="system.xml" hash="5762459db1687ed91c2d855591227c7f"/></dir><dir name="sql"><dir name="apruvepayment_setup"><file name="install-2.0.0.php" hash="eb0b5b2ab790d8710a00a817c52ef2b1"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="apruvepayment.xml" hash="6ade24fa77f4f55ce0bdf4f5c79fd65c"/></dir><dir name="template"><dir name="apruvepayment"><file name="head.phtml" hash="36809c6a8fa5aad587b03e2107482bdf"/><dir name="payment"><file name="form.phtml" hash="d0bff443c44e35201ced6abffdf7ec8f"/></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Apruve_ApruvePayment.xml" hash="8e20101d5c688c8505302df6d73686fb"/></dir></target><target name="mage"><dir name="js"><dir name="Apruve"><file name="ApruvePayment.js" hash="a9fab0a38c6bc2923106e9925462ef3e"/></dir></dir></target></contents>
16
<compatible/>
17
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
</package>