Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Reworked the confirm order Declined failure handling, and resolved is…
Browse files Browse the repository at this point in the history
…sues noted by andrewkett

This fixes issues:
#14
#15
#12
  • Loading branch information
16hands committed Jul 12, 2018
1 parent 7f27406 commit 102295a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
59 changes: 39 additions & 20 deletions app/code/Laybuy/LaybuyPayments/Controller/Payment/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Process extends Action {
private $orderFactory;


private $orderManagement;
private $orderManagement;

protected $quoteRepository;

Expand Down Expand Up @@ -143,40 +143,60 @@ public function __construct(
public function execute() {

$this->logger->debug([__METHOD__ => 'start']);

//status=CANCELLED | SUCCESS
$status = strtoupper($this->getRequest()->getParam('status') );
$status = strtoupper($this->getRequest()->getParam('status'));
$token = $this->getRequest()->getParam('token');

/* @var $quote \Magento\Quote\Model\Quote */
// try checkout session -- look into fall backs?
$quote = $this->checkoutSession->getQuote();


try {
if($status == Laybuy::SUCCESS ){
try {
if ($status == Laybuy::SUCCESS) {
if ($this->client->getCheckoutMethod($quote) === Onepage::METHOD_GUEST) {

}


// move this higher
$laybuy_order_id = $this->client->laybuyConfirm($token);

if (!$laybuy_order_id) {
$this->messageManager->addNoticeMessage('Laybuy: There was an error' . ( ($this->client->last_error)?', '. $this->client->last_error : '' ) . '.' );
$this->client->laybuyCancel($token);

// we are done
return $this->_redirect('checkout/cart', ['_secure' => TRUE]);
}


// setup order with the onepage helper
$this->checkout->setQuote($quote);

$paymentData = [
"method" => "laybuy_laybuypayments",
];

$this->checkout->savePayment($paymentData);
$this->checkout->saveOrder();

$laybuy_order_id = $this->client->laybuyConfirm($token);

if(!$laybuy_order_id){
$this->messageManager->addNoticeMessage('Laybuy order confirmation error.');
}

/* @var $order \Magento\Sales\Model\Order */
//$order = $this->checkoutSession->getLastRealOrder();
$order = $this->checkoutSession->getLastRealOrder();

$invoices = $order->getInvoiceCollection();

$this->logger->debug(['order_id' => $order->getId()]);
$this->logger->debug(['invoices' => count($invoices)]);

foreach ($invoices as $invoice) {
/* @var $invoice \Magento\Sales\Model\Order\Invoice */
/* $invoice->setState(2); */
$invoice->pay();
$invoice->save();
}

$txn_id = $laybuy_order_id . '_' . $token;

//TODO look into assigning a Txn ID on the order
Expand All @@ -197,26 +217,25 @@ public function execute() {
// the Neat this is that we done need to do anything
// there isn't an order yet

if($status == Laybuy::CANCELLED ){
if ($status == Laybuy::CANCELLED) {
$this->messageManager->addNoticeMessage('Laybuy payment was Cancelled.');
}
else {
$this->messageManager->addNoticeMessage('Laybuy: There was an error, your payment failed.');
}

$this->client->laybuyCancel($token);

// fall though to cart redirect
}



} catch (\Exception $e) {
$this->logger->debug(['process error ' => $e->getTraceAsString()]);
$this->messageManager->addExceptionMessage($e, $e->getMessage());

}

return $this->_redirect('checkout/cart', ['_secure' => TRUE]);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public function execute() {
* @throws \InvalidArgumentException
*/
protected function validateQuote(\Magento\Quote\Model\Quote $quote) {

$this->logger->debug([ __METHOD__ . ' QUOTE IS: ' . get_class($quote)]);
if (!$quote || !$quote->getItemsCount()) {
throw new \InvalidArgumentException(__("We can't initialize checkout."));
}
Expand Down
8 changes: 8 additions & 0 deletions app/code/Laybuy/LaybuyPayments/Gateway/Http/Client/Laybuy.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Laybuy implements ClientInterface

private $checkoutHelper;

public $last_error;

/**
* @param Config $config
* @param Session $checkoutSession
Expand All @@ -135,6 +137,8 @@ public function __construct(
$this->paymentHelper = $paymentHelper;
$this->checkoutHelper = $checkoutHelper;

$this->last_error = null;

$this->logger->debug([__METHOD__ . ' TEST sandbox? ' => $this->config->getUseSandbox()]);
$this->logger->debug([__METHOD__ . ' TEST sandbox_merchantid? ' => $this->config->getSandboxMerchantId()]);

Expand Down Expand Up @@ -240,6 +244,7 @@ public function getLaybuyRedirectUrl(array $laybuy_order){
if (!$body->paymentUrl) {
// $this->noLaybuyRedirectError($body);
$this->logger->debug(['FAILED TO GET returnURL' => $body]);
$this->last_error = $body->error;
return FALSE;
}

Expand All @@ -251,6 +256,7 @@ public function getLaybuyRedirectUrl(array $laybuy_order){
else {

$this->logger->debug(['FAILED TO GET returnURL' => $body]);
$this->last_error = $body->error;
return FALSE;

}
Expand Down Expand Up @@ -282,6 +288,7 @@ public function laybuyConfirm( $token ) {
if (!$body->orderId) {

$this->logger->debug(['FAILED confirm order' => $body]);
$this->last_error = $body->error;
return FALSE;
}

Expand All @@ -292,6 +299,7 @@ public function laybuyConfirm( $token ) {
else {

$this->logger->debug(['FAILED confirm order' => $body]);
$this->last_error = $body->error;
return FALSE;

}
Expand Down
6 changes: 2 additions & 4 deletions app/code/Laybuy/LaybuyPayments/Model/Helper/QuoteUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Payment;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Braintree\Model\Ui\PayPal\ConfigProvider;
use Magento\Framework\Exception\LocalizedException;
use Laybuy\LaybuyPayments\Observer\DataAssignObserver;
use Laybuy\LaybuyPayments\Gateway\Config\Config;
Expand Down Expand Up @@ -61,8 +60,8 @@ public function execute($nonce, array $details, Quote $quote)

$payment = $quote->getPayment();

$payment->setMethod(ConfigProvider::PAYPAL_CODE);
$payment->setAdditionalInformation(DataAssignObserver::PAYMENT_METHOD_NONCE, $nonce);
$payment->setMethod(Config::CODE);
//$payment->setAdditionalInformation(DataAssignObserver::PAYMENT_METHOD_NONCE, $nonce);

$this->updateQuote($quote, $details);
}
Expand Down Expand Up @@ -130,7 +129,6 @@ private function updateShippingAddress(Quote $quote, array $details)

$this->updateAddressData($shippingAddress, $details['shippingAddress']);

// PayPal's address supposes not saving against customer account
$shippingAddress->setSaveInAddressBook(false);
$shippingAddress->setSameAsBilling(false);
$shippingAddress->unsCustomerAddressId();
Expand Down
1 change: 1 addition & 0 deletions app/code/Laybuy/LaybuyPayments/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<sdk_url><![CDATA[https://sandbox-docs.laybuy.com]]></sdk_url>
<paymentInfoKeys>result,token,orderId,amount,currency,merchantReference,processed</paymentInfoKeys>
<privateInfoKeys>token,processed</privateInfoKeys>
<payment_currency>NZD</payment_currency> <!-- default currency if none set -->
</laybuy_laybuypayments>
</payment>
</default>
Expand Down

0 comments on commit 102295a

Please sign in to comment.