Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save feelinc/de817030e00adc7ff7001de1807c1835 to your computer and use it in GitHub Desktop.
Save feelinc/de817030e00adc7ff7001de1807c1835 to your computer and use it in GitHub Desktop.
Process the Paypal express payment information from external app in Magento 2
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Paypal\Model\Express">
<plugin name="sulaeman_magento_paypal_model_express_plugin"
type="Sulaeman\PaypalWebService\Plugin\Paypal\Model\Express"
sortOrder="99999"
disabled="false" />
</type>
</config>
<?php
/**
* This file is part of the Sulaeman Paypal Web Service package.
*
* @author Sulaeman <[email protected]>
*/
namespace Sulaeman\PaypalWebService\Plugin\Paypal\Model;
use Magento\Framework\Registry;
use Magento\Framework\Model\Context;
use Magento\Paypal\Model\Express as PaypalExpress;
use Magento\Sales\Model\Order;
use Magento\Payment\Model\InfoInterface;
class Express
{
/**
* @var \Magento\Framework\App\State
*/
protected $_appState;
/**
* @var Registry
*/
protected $_registry;
/**
* @var LoggerInterface
*/
protected $_logger;
/**
* @var array
*/
private $_areas = [
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP
];
/**
* @var array
*/
private $_paymentPayload = null;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(Context $context, Registry $registry)
{
$this->_appState = $context->getAppState();
$this->_logger = $context->getLogger();
$this->_registry = $registry;
}
/**
* Interceptor capture.
*
* @param \Magento\Paypal\Model\Express $subject
*
* {@inheritdoc}
*/
public function aroundCapture(
PaypalExpress $subject,
\Closure $unknown,
InfoInterface $payment,
$amount
) {
if (in_array($this->_appState->getAreaCode(), $this->_areas)
&& $this->_paymentPayload != null) {
$payment->setLastTransId($this->_paymentPayload['id']);
$payment->setTransactionId($this->_paymentPayload['id']);
foreach ($this->_paymentPayload as $key => $value) {
$payment->setTransactionAdditionalInfo($key, $value);
}
$payment->setIsTransactionPending(false);
}
return $subject;
}
/**
* Interceptor assignData.
*
* @param \Magento\Paypal\Model\Express $subject
*
* {@inheritdoc}
*/
public function beforeAssignData(
PaypalExpress $subject,
\Magento\Framework\DataObject $data
) {
if (in_array($this->_appState->getAreaCode(), $this->_areas)) {
$additionalData = $data->getData('additional_data');
if ($additionalData) {
if (isset($additionalData['paypal_express_payment_payload'])) {
$this->_paymentPayload = json_decode($additionalData['paypal_express_payment_payload'], true);
}
}
}
return [$data];
}
}
@hongnguyenhuu96
Copy link

hongnguyenhuu96 commented Oct 26, 2017

What about the other files, can you share the folder structure, code of each file, or send me your folder code of this module, I really appreciate your module. I am dealing with paypal express checkout in magento 2 by using rest api. The documents is not much, and your module is the only solution that I found

@mrmonsters
Copy link

I suppose your plugin function aroundCapture() has completed bypassed the parent function since \Closure is not executed. As a result, Paypal transactions cannot be captured correctly and Paypal transaction ID(s) was missing. Please fix it by return $unknown($payment, $amount); instead of return $subject;.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment