Skip to content

Commit

Permalink
Merge pull request #30 from Affirm/shipping-update---PARTNERENG-1729
Browse files Browse the repository at this point in the history
Shipping update
  • Loading branch information
Dipti Bele authored May 6, 2019
2 parents b08f998 + ebbfc9d commit 3320413
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 100 deletions.
98 changes: 0 additions & 98 deletions Model/Plugin/Order/Address/Edit.php

This file was deleted.

160 changes: 160 additions & 0 deletions Model/Plugin/Order/AddressSave/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Astound
* NOTICE OF LICENSE
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Affirm
* @package Astound_Affirm
* @copyright Copyright (c) 2016 Astound, Inc. (http://www.astoundcommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

namespace Astound\Affirm\Model\Plugin\Order\AddressSave;

use Magento\Sales\Controller\Adminhtml\Order\Address;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Astound\Affirm\Model\Ui\ConfigProvider;
use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Class Edit
*/
class Edit
{
const CHARGE_ID = 'charge_id';
const API_CHARGES_PATH = '/api/v2/charges/';


/**
* Collection factory
*
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
protected $_collectionFactory;

/**
* Client factory
*
* @var \Magento\Framework\HTTP\ZendClientFactory
*/
protected $httpClientFactory;

/**
* Construct
*
* @param CollectionFactory $collectionFactory
*/

/**
* Scope config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

public function __construct(
CollectionFactory $collectionFactory,
ZendClientFactory $httpClientFactory,
ScopeConfigInterface $scopeConfig
) {
$this->_collectionFactory = $collectionFactory;
$this->httpClientFactory = $httpClientFactory;
$this->scopeConfig = $scopeConfig;
}

/**
* Plugin for edit order address in admin
*
* @param Address $controller
* @param callable $method
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function afterExecute($controller , $method)
{

$addressId = $controller->getRequest()->getParam('address_id');
$orderCollection = $this->_collectionFactory->create()->addAttributeToSearchFilter(
[
['attribute' => 'billing_address_id', 'eq' => $addressId . '%'],
['attribute' => 'shipping_address_id', 'eq' => $addressId . '%']
]
)->load();

$order = $orderCollection->getFirstItem();
if ($this->isAffirmPaymentMethod($order)) {
$chargeId = $order->getPayment()->getAdditionalInformation(self::CHARGE_ID);
$newAddress = $order->getShippingAddress()->getData();
$street = explode(PHP_EOL, $newAddress['street']);
$url = $this->getApiUrl("{$chargeId}/update");
$data = array(
'shipping' => array(
'name' => array(
'full' => $newAddress['firstname'] . ' ' . $newAddress['lastname']
),
'address' => array(
'line1' => $street[0],
'line2' => isset($street[1]) ? $street[1]: '' ,
'state' => $newAddress['region'],
'city' => $newAddress['city'],
'zipcode' => $newAddress['postcode'],
'country' => $newAddress['country_id']
)
)
);

try {
$client = $this->httpClientFactory->create();
$client->setUri($url);
$client->setAuth($this->getPublicApiKey(), $this->getPrivateApiKey());
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
$client->setRawData($data, 'application/json');
$client->request('POST');
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
}
}

return $method;
}

protected function getApiUrl($additionalPath)
{
$gateway = $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox'
? \Astound\Affirm\Model\Config::API_URL_SANDBOX
: \Astound\Affirm\Model\Config::API_URL_PRODUCTION;

return trim($gateway, '/') . sprintf('%s%s', self::API_CHARGES_PATH, $additionalPath);
}

protected function isAffirmPaymentMethod($order)
{
return $order->getId() && $order->getPayment()->getMethod() == ConfigProvider::CODE;
}

protected function getPrivateApiKey()
{
return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox'
? $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_sandbox')
: $this->scopeConfig->getValue('payment/affirm_gateway/private_api_key_production');
}

/**
* Get public API key
*
* @return string
*/
protected function getPublicApiKey()
{
return $this->scopeConfig->getValue('payment/affirm_gateway/mode') == 'sandbox'
? $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_sandbox')
: $this->scopeConfig->getValue('payment/affirm_gateway/public_api_key_production');
}
}
4 changes: 2 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@
<type name="Magento\Sales\Controller\Adminhtml\Order\Create\Save">
<plugin name="before-create-admin" type="Astound\Affirm\Model\Plugin\Order\Create" sortOrder="1"/>
</type>
<type name="Magento\Sales\Controller\Adminhtml\Order\Address">
<plugin name="before-edit-address-admin" type="Astound\Affirm\Model\Plugin\Order\Address\Edit" sortOrder="1"/>
<type name="Magento\Sales\Controller\Adminhtml\Order\AddressSave">
<plugin name="after-edit-address-save" type="Astound\Affirm\Model\Plugin\Order\AddressSave\Edit" sortOrder="1"/>
</type>
<type name="Magento\Checkout\CustomerData\Cart">
<plugin name="checkout-aslowas-minicart" type="Astound\Affirm\Model\Plugin\Checkout\CustomerData\Cart"/>
Expand Down

0 comments on commit 3320413

Please sign in to comment.