Skip to content

Commit 90bd313

Browse files
committed
Version 4.1.0
+ Adds online refund option (credit memo) + Eliminates the need to copy webhook URLs + Adds version check (looks for new github release)
1 parent 809f64b commit 90bd313

File tree

13 files changed

+400
-51
lines changed

13 files changed

+400
-51
lines changed

app/code/community/Mollie/Mpm/Helper/Api.php

+39-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
class Mollie_Mpm_Helper_Api
4141
{
42-
const PLUGIN_VERSION = '4.0.8';
42+
const PLUGIN_VERSION = '4.1.0';
4343

4444
protected $api_key = null;
4545
protected $amount = 0;
@@ -89,7 +89,6 @@ public function createPayment($amount, $description, $order, $redirect_url, $met
8989
return false;
9090
}
9191

92-
$billing = $order->getBillingAddress();
9392

9493
$params = array(
9594
"amount" => $this->getAmount(),
@@ -100,12 +99,20 @@ public function createPayment($amount, $description, $order, $redirect_url, $met
10099
"metadata" => array(
101100
"order_id" => $order->getId(),
102101
),
103-
"billingCity" => $billing->getCity(),
104-
"billingRegion" => $billing->getRegion(),
105-
"billingPostal" => $billing->getPostcode(),
106-
"billingCountry" => $billing->getCountryId(),
102+
"webhookUrl" => $this->getWebhookURL(),
107103
);
108104

105+
106+
if ($billing = $order->getBillingAddress())
107+
{
108+
$params += array(
109+
"billingCity" => $billing->getCity(),
110+
"billingRegion" => $billing->getRegion(),
111+
"billingPostal" => $billing->getPostcode(),
112+
"billingCountry" => $billing->getCountryId(),
113+
);
114+
}
115+
109116
if ($shipping = $order->getShippingAddress())
110117
{
111118
$params += array(
@@ -117,7 +124,31 @@ public function createPayment($amount, $description, $order, $redirect_url, $met
117124
);
118125
}
119126

120-
$payment = $api->payments->create($params);
127+
try
128+
{
129+
$payment = $api->payments->create($params);
130+
}
131+
catch (Mollie_API_Exception $e)
132+
{
133+
try
134+
{
135+
if ($e->getField() == "webhookUrl")
136+
{
137+
unset($params["webhookUrl"]);
138+
$payment = $api->payments->create($params);
139+
}
140+
else
141+
{
142+
throw $e;
143+
}
144+
}
145+
catch (Mollie_API_Exception $e)
146+
{
147+
$this->error_message = __METHOD__ . ' said: Unable to set up payment. Reason: ' . $e->getMessage();
148+
Mage::log($this->error_message);
149+
return false;
150+
}
151+
}
121152

122153

123154
$this->setTransactionId($payment->id);
@@ -369,7 +400,7 @@ public function getPaymentMethods()
369400
catch (Exception $e)
370401
{
371402
Mage::log($e);
372-
return Mage::helper('core')->__('There was an error:') . '<br />' . $e->getFile() . ':' . $e->getLine() . ' - ' . $e->getMessage();
403+
return Mage::helper('core')->__('There was an error:') . '<br />' . $e->getMessage();
373404
}
374405
}
375406

app/code/community/Mollie/Mpm/Helper/Data.php

+82-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
class Mollie_Mpm_Helper_Data extends Mage_Core_Helper_Abstract
3737
{
38+
public $update_url = 'https://github.com/mollie/Magento';
39+
public $should_update = 'maybe';
3840

3941
/**
4042
* Get payment bank status by order_id
@@ -155,7 +157,7 @@ public function getApiKey()
155157
*/
156158
public function getConfig($paymentmethod = NULL, $key = NULL)
157159
{
158-
$arr = array('active', 'apikey', 'description', 'skip_invoice', 'skip_order_mails', 'skip_invoice_mails', 'show_images', 'show_bank_list', 'webhook_tested');
160+
$arr = array('active', 'apikey', 'description', 'skip_invoice', 'skip_order_mails', 'skip_invoice_mails', 'show_images', 'show_bank_list');
159161
$paymentmethods = array('mollie');
160162

161163
if(in_array($key, $arr) && in_array($paymentmethod, $paymentmethods))
@@ -170,8 +172,19 @@ public function getConfig($paymentmethod = NULL, $key = NULL)
170172
*/
171173
public function getModuleStatus($method_count, $method_limit)
172174
{
175+
/* Precedence:
176+
* 1) Missing files
177+
* 2) Magento version
178+
* 3) New version on github
179+
* 4) Method limit
180+
* 5) Disabled check
181+
* 6) Deprecated files
182+
*/
183+
173184
$core = Mage::helper('core');
174-
// check missing files
185+
186+
187+
// 1) Check missing files
175188
$needFiles = array();
176189
$modFiles = array(
177190
Mage::getBaseDir('lib') . "/Mollie/src/Mollie/API/Client.php",
@@ -223,7 +236,7 @@ public function getModuleStatus($method_count, $method_limit)
223236
}
224237

225238

226-
// check version
239+
// 2) Check magento version
227240
if ( version_compare(Mage::getVersion(), '1.4.1.0', '<'))
228241
{
229242
return '<b>'.$core->__('Version incompatible!').'</b><br />
@@ -235,7 +248,14 @@ public function getModuleStatus($method_count, $method_limit)
235248
}
236249

237250

238-
// check method count
251+
// 3) Check github version
252+
if ($this->should_update === 'yes')
253+
{
254+
return '<b>'.$core->__('Status').'</b><br /><span style="color:#EB5E00">'.$core->__('Module status: Outdated!').'</span>';
255+
}
256+
257+
258+
// 4) Check method limit
239259
if ($method_count > $method_limit)
240260
{
241261
return '<b>'.$core->__('Module outdated!').'</b><br />
@@ -247,14 +267,14 @@ public function getModuleStatus($method_count, $method_limit)
247267
}
248268

249269

250-
// Check if webhook is set
251-
if (!Mage::Helper('mpm/data')->getConfig('mollie', 'webhook_tested'))
270+
// 5) Check if disabled
271+
if (!Mage::Helper('mpm/data')->getConfig('mollie', 'active'))
252272
{
253-
return '<b>'.$core->__('Webhook not set!').'</b><br /><span style="color:red;">'.$core->__('Warning: It seems you have not set a webhook in your Mollie profile.').'</span><br />';
273+
return '<b>'.$core->__('Status').'</b><br /><span style="color:#EB5E00">'.$core->__('Module status: Disabled!').'</span>';
254274
}
255275

256276

257-
// check deprecated files
277+
// 6) Check deprecated files
258278
$deprFiles = array();
259279
$oldFiles = array(
260280
Mage::getRoot() .'/code/community/Mollie/Mpm/Block/Payment/Idl/Fail.php',
@@ -273,11 +293,13 @@ public function getModuleStatus($method_count, $method_limit)
273293
}
274294
}
275295

296+
276297
if (count($deprFiles) > 0)
277298
{
278299
return '<b>'.$core->__('Outdated file(s) found!').'</b><br />' . implode('<br />', $deprFiles) . '<br />'.$core->__('These aren&lsquo;t needed any longer; you might as well delete them.');
279300
}
280301

302+
// All is fine
281303
return '<b>'.$core->__('Status').'</b><br /><span style="color:green">'.$core->__('Module status: OK!').'</span>';
282304
}
283305

@@ -286,4 +308,56 @@ public function getModuleVersion()
286308
return Mage::getConfig()->getNode('modules')->children()->Mollie_Mpm->version;
287309
}
288310

311+
/**
312+
* @return string
313+
*/
314+
public function _getUpdateMessage()
315+
{
316+
$core = Mage::helper('core');
317+
$update_message = '';
318+
$update_xml = $this->_getUpdateXML();
319+
if ($update_xml === FALSE)
320+
{
321+
$this->should_update = 'maybe';
322+
$update_message = $core->__('Warning: Could not retrieve update xml file from github.', 'mollie');
323+
}
324+
else
325+
{
326+
/** @var SimpleXMLElement $tags */
327+
$tags = new SimpleXMLElement($update_xml);
328+
if (!empty($tags) && isset($tags->entry, $tags->entry[0], $tags->entry[0]->id))
329+
{
330+
$title = $tags->entry[0]->id;
331+
$latest_version = preg_replace("/[^0-9,.]/", "", substr($title, strrpos($title, '/')));
332+
$this_version = $this->getModuleVersion();
333+
if (!version_compare($this_version, $latest_version, '>='))
334+
{
335+
$update_message = sprintf(
336+
$core->__('<a href=%s/releases>You are currently using version %s. We strongly recommend you to upgrade to the new version %s!</a>', 'mollie'),
337+
$this->update_url, $this_version, $latest_version
338+
);
339+
$this->should_update = 'yes';
340+
}
341+
else
342+
{
343+
$this->should_update = 'no';
344+
}
345+
}
346+
else
347+
{
348+
$this->should_update = 'maybe';
349+
$update_message = $core->__('Warning: Update xml file from github follows an unexpected format.', 'mollie');
350+
}
351+
}
352+
return $update_message;
353+
}
354+
355+
/**
356+
* @return string
357+
*/
358+
protected function _getUpdateXML()
359+
{
360+
return @file_get_contents($this->update_url . '/releases.atom');
361+
}
362+
289363
}

app/code/community/Mollie/Mpm/Model/Api.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Mollie_Mpm_Model_Api extends Mage_Payment_Model_Method_Abstract
7777
protected $_canUseCheckout = TRUE;
7878
protected $_canUseInternal = TRUE;
7979
protected $_canUseForMultishipping = FALSE; // wouldn't work without event capturing anyway
80-
protected $_canRefund = FALSE;
80+
protected $_canRefund = TRUE;
81+
protected $_canRefundInvoicePartial = FALSE;
8182
protected $_canCapture = FALSE;
8283

8384
// Payment statusses
@@ -359,4 +360,38 @@ public function isValidIndex ()
359360
}
360361
return isset($this->_index) && $this->_index >= 0 && $this->_index < sizeof($this->_api->methods);
361362
}
363+
364+
public function refund(Varien_Object $payment, $amount)
365+
{
366+
// fetch order and transaction info
367+
$order = $payment->getOrder();
368+
$row = $this->_mysqlr->fetchRow(
369+
'SELECT * FROM `' . $this->_table . '` WHERE `order_id` = ' . intval($order->entity_id),
370+
array(),
371+
Zend_Db::FETCH_ASSOC
372+
);
373+
$transaction_id = $row['transaction_id'];
374+
375+
// only complete refunds are allowed
376+
if (round($order->getBaseGrandTotal(), 2) !== round($amount, 2))
377+
{
378+
Mage::throwException('Impossible to create a refund for this transaction. Details: The amount to refund must equal the amount paid.<br />');
379+
}
380+
381+
// fetch payment info
382+
$mollie = $this->_api->_getMollieAPI();
383+
$mollie_payment = $mollie->payments->get($transaction_id);
384+
385+
// attempt a refund
386+
try
387+
{
388+
$mollie->payments->refund($mollie_payment);
389+
}
390+
catch (Exception $e)
391+
{
392+
Mage::throwException('Impossible to create a refund for this transaction. Details: ' . $e->getMessage() . '<br />');
393+
}
394+
395+
return $this;
396+
}
362397
}

app/code/community/Mollie/Mpm/controllers/ApiController.php

+16-19
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ public function webhookAction ()
218218
// Determine if this is a connection test
219219
if ($this->getRequest()->getParam('testByMollie'))
220220
{
221-
Mage::getConfig()->saveConfig('payment/mollie/webhook_tested', '1');
222221
return;
223222
}
224223
// Get transaction_id from post parameter
@@ -270,7 +269,19 @@ public function webhookAction ()
270269
$order->sendNewOrderEmail()->setEmailSent(TRUE);
271270
}
272271

273-
if (Mage::Helper('mpm/data')->getConfig('mollie', 'skip_invoice'))
272+
if ($transaction = $payment->getTransaction($transactionId))
273+
{
274+
$transaction->setTxnType(Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE);
275+
$transaction->setIsClosed(TRUE);
276+
$transaction->save();
277+
}
278+
else
279+
{
280+
Mage::log(__METHOD__ . ' said: Could not find a transaction with id ' . $transactionId . ' for order ' . $orderId);
281+
return;
282+
}
283+
284+
if (Mage::Helper('mpm/data')->getConfig('mollie', 'skip_invoice') || !$order->canInvoice())
274285
{
275286
/*
276287
* Update the total amount paid
@@ -292,21 +303,11 @@ public function webhookAction ()
292303
}
293304
else
294305
{
295-
$this->_savePaidInvoice($order);
306+
$this->_savePaidInvoice($order, $transaction->getId());
296307
}
297308

298309
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, Mage_Sales_Model_Order::STATE_PROCESSING, $this->__(Mollie_Mpm_Model_Api::PAYMENT_FLAG_PROCESSED), TRUE);
299310

300-
if ($transaction = $payment->getTransaction($transactionId))
301-
{
302-
$transaction->setTxnType(Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE);
303-
$transaction->setIsClosed(TRUE);
304-
$transaction->save();
305-
}
306-
else
307-
{
308-
Mage::log(__METHOD__ . ' said: Could not find a transaction with id ' . $transactionId . ' for order ' . $orderId);
309-
}
310311
$order->save();
311312
}
312313
else
@@ -333,15 +334,11 @@ public function webhookAction ()
333334
*
334335
* @return bool
335336
*/
336-
protected function _savePaidInvoice(Mage_Sales_Model_Order $order)
337+
protected function _savePaidInvoice(Mage_Sales_Model_Order $order, $transaction_id = null)
337338
{
338-
if (!$order->canInvoice())
339-
{
340-
return FALSE;
341-
}
342-
343339
$invoice = $order->prepareInvoice()
344340
->register()
341+
->setTransactionId($transaction_id)
345342
->pay();
346343

347344
Mage::getModel('core/resource_transaction')

app/code/community/Mollie/Mpm/etc/config.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<config>
3838
<modules>
3939
<Mollie_Mpm>
40-
<version>4.0.8</version>
40+
<version>4.1.0</version>
4141
</Mollie_Mpm>
4242
</modules>
4343

@@ -174,7 +174,7 @@
174174
<group>mollie</group>
175175
<active>0</active>
176176
<model>mpm/idl</model>
177-
<title>iDeal</title>
177+
<title>{{Deprecated}}</title>
178178
<currency>EUR</currency>
179179
</mpm_idl>
180180

0 commit comments

Comments
 (0)