Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #48852 MX/BR displaying paypal button #376

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions classes/AbstractMethodPaypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@

abstract class AbstractMethodPaypal extends AbstractMethod
{
const AUTHORIZE = 'AUTHORIZE';
const SALE = 'CAPTURE';
/** @var bool */
protected $isSandbox;

Expand Down
6 changes: 1 addition & 5 deletions classes/MethodEC.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
*/
class MethodEC extends AbstractMethodPaypal
{
const AUTHORIZE = 'AUTHORIZE';

const SALE = 'CAPTURE';

/** @var bool pay with card without pp account */
public $credit_card;

Expand Down Expand Up @@ -137,7 +133,7 @@ public function getConfig(PayPal $module)
public function logOut($sandbox = null)
{
if ($sandbox == null) {
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
$mode = $this->isSandbox() ? 'SANDBOX' : 'LIVE';
} else {
$mode = (int) $sandbox ? 'SANDBOX' : 'LIVE';
}
Expand Down
6 changes: 4 additions & 2 deletions classes/MethodMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function setParameters($values)
public function logOut($sandbox = null)
{
if ($sandbox == null) {
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
$mode = $this->isSandbox() ? 'SANDBOX' : 'LIVE';
} else {
$mode = (int) $sandbox ? 'SANDBOX' : 'LIVE';
}
Expand Down Expand Up @@ -164,13 +164,15 @@ public function validation()
*/
public function confirmCapture($orderPayPal)
{
return $this->paypalApiManager->getCaptureAuthorizeRequest($orderPayPal)->execute();
}

/**
* @see AbstractMethodPaypal::void()
*/
public function void($orderPayPal)
{
return $this->paypalApiManager->getAuthorizationVoidRequest($orderPayPal)->execute();
}

/**
Expand Down Expand Up @@ -454,7 +456,7 @@ public function getPaypalPartnerId()

public function getIntent()
{
return Configuration::get('PAYPAL_API_INTENT') == 'sale' ? 'CAPTURE' : 'AUTHORIZE';
return Configuration::get('PAYPAL_API_INTENT') == 'sale' ? self::SALE : self::AUTHORIZE;
}

public function getMerchantId()
Expand Down
4 changes: 2 additions & 2 deletions classes/MethodPPP.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function setParameters($values)
public function logOut($sandbox = null)
{
if ($sandbox == null) {
$mode = Configuration::get('PAYPAL_SANDBOX') ? 'SANDBOX' : 'LIVE';
$mode = $this->isSandbox() ? 'SANDBOX' : 'LIVE';
} else {
$mode = (int) $sandbox ? 'SANDBOX' : 'LIVE';
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public function getSecret($sandbox = null)

public function getIntent()
{
return 'CAPTURE';
return self::SALE;
}

public function getReturnUrl()
Expand Down
6 changes: 0 additions & 6 deletions classes/Shortcut/ShortcutPaymentStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
namespace PaypalAddons\classes\Shortcut;

use Configuration;
use MethodMB;
use PaypalAddons\classes\AbstractMethodPaypal;
use PaypalAddons\classes\Constants\PaypalConfigurations;
use Tools;

Expand All @@ -42,10 +40,6 @@ class ShortcutPaymentStep extends ShortcutAbstract
public function __construct()
{
parent::__construct();

if ($this->method instanceof MethodMB) {
$this->method = AbstractMethodPaypal::load('EC');
}
}

public function getTemplatePath()
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/ecInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function setMethod($method)
protected function getMethodType($requestData)
{
if (empty($requestData['methodType'])) {
return 'EC';
return $this->module->paypal_method;
} else {
return $requestData['methodType'];
}
Expand Down
43 changes: 39 additions & 4 deletions controllers/front/mbValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

use PaypalAddons\classes\AbstractMethodPaypal;
use PaypalAddons\services\PaymentData;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -52,10 +53,10 @@ public function init()
public function postProcess()
{
$paypal = Module::getInstanceByName($this->name);
$payemtData = json_decode(Tools::getValue('paymentData'));
$this->method->setPaymentId($payemtData->paymentId);
$this->method->setPayerId($payemtData->result->payer->payer_info->payer_id);
$this->method->setRememberedCards($payemtData->result->rememberedCards);
$paymentData = $this->parsePaymentData(Tools::getAllValues());
$this->method->setPaymentId($paymentData->getPaymentID());
$this->method->setPayerId($paymentData->getPaymentID());
$this->method->setRememberedCards($paymentData->getRememberedCards());

try {
$this->method->validation();
Expand Down Expand Up @@ -83,4 +84,38 @@ public function displayAjaxGetPaymentInfo()
];
$this->jsonValues = $responseContent;
}

protected function parsePaymentData($data)
{
$paymentDataObj = new PaymentData();

if (!empty($data['token'])) {
$paymentDataObj->setPaymentID($data['token']);
}
if (!empty($data['PayerID'])) {
$paymentDataObj->setPayerID($data['PayerID']);
}

if (!empty($data['paymentData'])) {
$paymentData = json_decode($data['paymentData'], true);

if (!empty($paymentData['paymentId'])) {
$paymentDataObj->setPaymentID($paymentData['paymentId']);
}
if (!empty($paymentData['paymentID'])) {
$paymentDataObj->setPaymentID($paymentData['paymentID']);
}
if (!empty($paymentData['result']['payer']['payer_info']['payer_id'])) {
$paymentDataObj->setPayerID($paymentData['result']['payer']['payer_info']['payer_id']);
}
if (!empty($paymentData['payerID'])) {
$paymentDataObj->setPayerID($paymentData['payerID']);
}
if (!empty($paymentData['result']['rememberedCards'])) {
$paymentDataObj->setRememberedCards($paymentData['result']['rememberedCards']);
}
}

return $paymentDataObj;
}
}
49 changes: 35 additions & 14 deletions services/PaymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,49 @@
class PaymentData
{
/** @var string */
protected $orderId;
protected $orderId = '';

/** @var string */
protected $payerID;
protected $payerID = '';

/** @var string */
protected $paymentID;
protected $paymentID = '';

/** @var string */
protected $billingToken;
protected $billingToken = '';

/** @var string */
protected $facilitatorAccessToken;
protected $facilitatorAccessToken = '';

/** @var string */
protected $rememberedCards = '';

public function fromArray($data)
{
if (false == empty($data['orderID'])) {
if (false === empty($data['orderID'])) {
$this->setOrderId($data['orderID']);
}

if (false == empty($data['payerID'])) {
if (false === empty($data['payerID'])) {
$this->setPayerId($data['payerID']);
}

if (false == empty($data['paymentID'])) {
if (false === empty($data['paymentID'])) {
$this->setPaymentID($data['paymentID']);
}

if (false == empty($data['billingToken'])) {
if (false === empty($data['billingToken'])) {
$this->setBillingToken($data['billingToken']);
}

if (false == empty($data['facilitatorAccessToken'])) {
if (false === empty($data['facilitatorAccessToken'])) {
$this->setFacilitatorAccessToken($data['facilitatorAccessToken']);
}

if (false === empty($data['rememberedCards'])) {
$this->setRememberedCards($data['rememberedCards']);
}

return $this;
}

Expand All @@ -82,7 +89,7 @@ public function setOrderId($orderID)
return $this;
}

protected function setPayerId($payerID)
public function setPayerId($payerID)
{
if (is_string($payerID)) {
$this->payerID = $payerID;
Expand All @@ -91,7 +98,7 @@ protected function setPayerId($payerID)
return $this;
}

protected function setPaymentID($paymentID)
public function setPaymentID($paymentID)
{
if (is_string($paymentID)) {
$this->paymentID = $paymentID;
Expand All @@ -100,7 +107,7 @@ protected function setPaymentID($paymentID)
return $this;
}

protected function setBillingToken($billingToken)
public function setBillingToken($billingToken)
{
if (is_string($billingToken)) {
$this->billingToken = $billingToken;
Expand All @@ -109,7 +116,7 @@ protected function setBillingToken($billingToken)
return $this;
}

protected function setFacilitatorAccessToken($facilitatorAccessToken)
public function setFacilitatorAccessToken($facilitatorAccessToken)
{
if (is_string($facilitatorAccessToken)) {
$this->facilitatorAccessToken = $facilitatorAccessToken;
Expand All @@ -118,6 +125,15 @@ protected function setFacilitatorAccessToken($facilitatorAccessToken)
return $this;
}

public function setRememberedCards($rememberedCards)
{
if (is_string($rememberedCards)) {
$this->rememberedCards = $rememberedCards;
}

return $this;
}

/**
* @return string
*/
Expand Down Expand Up @@ -157,4 +173,9 @@ public function getFacilitatorAccessToken()
{
return $this->facilitatorAccessToken;
}

public function getRememberedCards()
{
return $this->rememberedCards;
}
}
11 changes: 1 addition & 10 deletions services/StatusMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Configuration;
use MethodEC;
use MethodMB;
use MethodPPP;
use PaypalAddons\classes\AbstractMethodPaypal;
use PaypalAddons\classes\Constants\PaypalConfigurations;
use PaypalAddons\classes\Constants\WebHookType;
Expand Down Expand Up @@ -182,15 +181,7 @@ public function isModeSale($method = null)
$method = AbstractMethodPaypal::load();
}

if ($method instanceof MethodPPP) {
return true;
}

if ($method instanceof MethodMB) {
return true;
}

return Configuration::get('PAYPAL_API_INTENT') == 'sale';
return $method->getIntent() === AbstractMethodPaypal::SALE;
}

/**
Expand Down
Loading