Skip to content
This repository was archived by the owner on Aug 6, 2019. It is now read-only.

Commit 98e82a5

Browse files
author
Pieter Poorthuis
authored
Merge pull request #8 from innoppl-developers/master
Previously a Magento invoice was created when a Magento order was created. This MR fixes this, so that a Magento invoice will be created when a Magento order is paid successfully (according to plugin settings)
2 parents 69b5d07 + ce6bf97 commit 98e82a5

File tree

5 files changed

+169
-22
lines changed

5 files changed

+169
-22
lines changed

app/code/Bitpay/Core/Controller/Ipn/Index.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public function execute() {
119119
}
120120

121121
// Does the status match?
122-
if ($invoice -> getStatus() != $ipn -> status) {
122+
/*if ($invoice -> getStatus() != $ipn -> status) {
123123
$this -> _bitpayHelper -> debugData('[ERROR] In \Bitpay\Core\Controller\Ipn::indexAction(), IPN status and status from BitPay are different. Rejecting this IPN!');
124124
$this -> throwException('There was an error processing the IPN - statuses are different. Rejecting this IPN!');
125-
}
125+
}*/
126126

127127
// Does the price match?
128128
if ($invoice -> getPrice() != $ipn -> price) {
@@ -132,12 +132,13 @@ public function execute() {
132132

133133
// Update the order to notifiy that it has been paid
134134
$transactionSpeed = \Magento\Framework\App\ObjectManager::getInstance() -> create('Magento\Framework\App\Config\ScopeConfigInterface') -> getValue('payment/bitpay/speed');
135-
if ($invoice -> getStatus() === 'paid' || ($invoice -> getStatus() === 'confirmed' && $transactionSpeed === 'high')) {
135+
$this -> _bitpayHelper -> debugData('am here'.$ipn -> status);
136+
if ($ipn -> status === 'paid' || $ipn -> status === 'confirmed') {
136137

137138
$payment = \Magento\Framework\App\ObjectManager::getInstance() -> create('Magento\Sales\Model\Order\Payment') -> setOrder($order);
138139

139140
if (true === isset($payment) && false === empty($payment)) {
140-
if ($order -> canInvoice()) {
141+
if ($ipn -> status === 'confirmed') {
141142
// Create invoice for this order
142143
$order_invoice = $this -> _objectManager -> create('Magento\Sales\Model\Service\InvoiceService') -> prepareInvoice($order);
143144

app/code/Bitpay/Core/Model/Method/Bitcoin.php

-18
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,6 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
171171
*/
172172
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount,$iframe = false)
173173
{
174-
175-
// Check if coming from iframe or submit button
176-
/**
177-
if ((!$this->_scopeConfig->getValue('payment/bitpay/fullscreen') && $iframe === false)
178-
|| ($this->_scopeConfig->getValue('payment/bitpay/fullscreen') && $iframe === true)) {
179-
$quoteId = $payment->getOrder()->getQuoteId();
180-
$ipn = \Magento\Framework\App\ObjectManager::getInstance()->get('\Bitpay\Core\Model\Ipn');
181-
182-
if (!$ipn->GetQuotePaid($quoteId))
183-
{
184-
$this->getHelper()->debugData('[ERROR] Order not paid for. Please pay first and then Place Your Order.');
185-
// This is the error that is displayed to the customer during checkout.
186-
throw new \Magento\Framework\Exception\CouldNotSaveException("Order not paid for. Please pay first and then Place your Order.");
187-
188-
}
189-
190-
return $this;
191-
} */
192174

193175
if (false === isset($payment) || false === isset($amount) || true === empty($payment) || true === empty($amount)) {
194176
$this->getHelper()->debugData('[ERROR] In \Bitpay\Core\Model\Method\Bitcoin::authorize(): missing payment or amount parameters.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
// @codingStandardsIgnoreFile
8+
9+
namespace Bitpay\Core\Model\Order;
10+
11+
/**
12+
* Order payment information
13+
*
14+
* @method \Magento\Sales\Model\ResourceModel\Order\Payment _getResource()
15+
* @method \Magento\Sales\Model\ResourceModel\Order\Payment getResource()
16+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
17+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
20+
class Payment extends \Magento\Sales\Model\Order\Payment
21+
{
22+
23+
24+
/**
25+
* Authorize or authorize and capture payment on gateway, if applicable
26+
* This method is supposed to be called only when order is placed
27+
*
28+
* @return $this
29+
* @SuppressWarnings(PHPMD.NPathComplexity)
30+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
31+
*/
32+
public function place()
33+
{
34+
$this->_eventManager->dispatch('sales_order_payment_place_start', ['payment' => $this]);
35+
$order = $this->getOrder();
36+
37+
$this->setAmountOrdered($order->getTotalDue());
38+
$this->setBaseAmountOrdered($order->getBaseTotalDue());
39+
$this->setShippingAmount($order->getShippingAmount());
40+
$this->setBaseShippingAmount($order->getBaseShippingAmount());
41+
42+
$methodInstance = $this->getMethodInstance();
43+
$methodInstance->setStore($order->getStoreId());
44+
45+
$orderState = \Magento\Sales\Model\Order::STATE_NEW;
46+
$orderStatus = $methodInstance->getConfigData('order_status');
47+
$isCustomerNotified = $order->getCustomerNoteNotify();
48+
49+
// Do order payment validation on payment method level
50+
$methodInstance->validate();
51+
$action = $methodInstance->getConfigPaymentAction();
52+
$payment = $order -> getPayment();
53+
$paymentMethodCode = $payment -> getMethodInstance() -> getCode();
54+
55+
if ($action) {
56+
if ($methodInstance->isInitializeNeeded()) {
57+
$stateObject = new \Magento\Framework\DataObject();
58+
// For method initialization we have to use original config value for payment action
59+
$methodInstance->initialize($methodInstance->getConfigData('payment_action'), $stateObject);
60+
if ($paymentMethodCode != 'bitpay'){
61+
$orderState = $stateObject->getData('state') ?: $orderState;
62+
$orderStatus = $stateObject->getData('status') ?: $orderStatus;
63+
}
64+
$isCustomerNotified = $stateObject->hasData('is_notified')
65+
? $stateObject->getData('is_notified')
66+
: $isCustomerNotified;
67+
} else {
68+
$this->processAction($action, $order);
69+
if ($paymentMethodCode != 'bitpay'){
70+
$orderState = \Magento\Sales\Model\Order::STATE_PROCESSING;
71+
$orderState = $order->getState() ? $order->getState() : $orderState;
72+
$orderStatus = $order->getStatus() ? $order->getStatus() : $orderStatus;
73+
}
74+
75+
}
76+
} else {
77+
$order->setState($orderState)
78+
->setStatus($orderStatus);
79+
}
80+
81+
$isCustomerNotified = $isCustomerNotified ?: $order->getCustomerNoteNotify();
82+
83+
if (!array_key_exists($orderStatus, $order->getConfig()->getStateStatuses($orderState))) {
84+
$orderStatus = $order->getConfig()->getStateDefaultStatus($orderState);
85+
}
86+
87+
$this->updateOrder($order, $orderState, $orderStatus, $isCustomerNotified);
88+
89+
$this->_eventManager->dispatch('sales_order_payment_place_end', ['payment' => $this]);
90+
91+
return $this;
92+
}
93+
94+
95+
public function addTransactionCommentsToOrder($transaction, $message)
96+
{
97+
$order = $this->getOrder();
98+
$payment = $order -> getPayment();
99+
$paymentMethodCode = $payment -> getMethodInstance() -> getCode();
100+
101+
$message = $this->_appendTransactionToMessage($transaction, $message);
102+
if ($paymentMethodCode != 'bitpay'){
103+
$order->addStatusHistoryComment($message);
104+
}
105+
}
106+
107+
108+
//@codeCoverageIgnoreEnd
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Bitpay\Core\Model\Order\Payment\State;
7+
8+
class AuthorizeCommand extends \Magento\Sales\Model\Order\Payment\State\AuthorizeCommand
9+
{
10+
11+
public function execute(\Magento\Sales\Api\Data\OrderPaymentInterface $payment, $amount, \Magento\Sales\Api\Data\OrderInterface $order)
12+
{
13+
$paymentmethod = $order -> getPayment();
14+
$paymentMethodCode = $paymentmethod -> getMethodInstance() -> getCode();
15+
if ($paymentMethodCode != 'bitpay'){
16+
$state = \Magento\Sales\Model\Order::STATE_PROCESSING;
17+
$status = false;
18+
$formattedAmount = $order->getBaseCurrency()->formatTxt($amount);
19+
if ($payment->getIsTransactionPending()) {
20+
$state = \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW;
21+
$message = __(
22+
'We will authorize %1 after the payment is approved at the payment gateway.',
23+
$formattedAmount
24+
);
25+
} else {
26+
if ($payment->getIsFraudDetected()) {
27+
$state = \Magento\Sales\Model\Order::STATE_PROCESSING;
28+
$message = __(
29+
'Order is suspended as its authorizing amount %1 is suspected to be fraudulent.',
30+
$formattedAmount
31+
);
32+
} else {
33+
$message = __('Authorized amount of %1', $formattedAmount);
34+
}
35+
}
36+
37+
}
38+
else {
39+
$state = \Magento\Sales\Model\Order::STATE_NEW;
40+
$status = false;
41+
$formattedAmount = $order->getBaseCurrency()->formatTxt($amount);
42+
$message = __('Authorized amount of %1', $formattedAmount);
43+
44+
}
45+
if ($payment->getIsFraudDetected()) {
46+
$status = \Magento\Sales\Model\Order::STATUS_FRAUD;
47+
}
48+
$this->setOrderStateAndStatus($order, $status, $state);
49+
50+
return $message;
51+
}
52+
53+
54+
}

app/code/Bitpay/Core/etc/di.xml

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
</argument>
2525
</arguments>
2626
</type>
27+
<preference for="Magento\Sales\Model\Order\Payment" type="Bitpay\Core\Model\Order\Payment" />
2728
</config>

0 commit comments

Comments
 (0)