Skip to content

Commit

Permalink
Merge pull request #285 from FatchipRobert/OX6-164-PayPalV2
Browse files Browse the repository at this point in the history
OX6-164 - PayPal V2
  • Loading branch information
jvarelmann authored Nov 21, 2024
2 parents e71a6ed + e0c0a41 commit 6cacb02
Show file tree
Hide file tree
Showing 27 changed files with 929 additions and 346 deletions.
23 changes: 23 additions & 0 deletions application/helper/fcpobasehelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

abstract class fcpobasehelper
{
/**
* Helper object for dealing with different shop versions
*
* @var object
*/
protected $_oHelper = null;

/**
* Returns fcpohelper object
*
* @return fcpohelper
*/
public function getMainHelper() {
if ($this->_oHelper === null) {
$this->_oHelper = oxNew('fcpohelper');
}
return $this->_oHelper;
}
}
78 changes: 78 additions & 0 deletions application/helper/fcpopaymenthelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

class fcpopaymenthelper extends fcpobasehelper
{
/**
* @var self
*/
protected static $oInstance;

/**
* Create instance of payment helper singleton
*
* @return static
*/
public static function getInstance()
{
if (self::$oInstance === null) {
self::$oInstance = oxNew(self::class);
}
return self::$oInstance;
}

/**
* Resets singleton class
* Needed for unit testing
*
* @return void
*/
public static function destroyInstance()
{
self::$oInstance = null;
}

/**
* @param string $sPaymentId
* @return object|false
*/
public function loadPaymentMethod($sPaymentId)
{
$oPayment = oxNew('oxpayment');
if ($oPayment->load($sPaymentId) === true) {
return $oPayment;
}
return false;
}

/**
* Returns if given payment method is active
*
* @param string $sPaymentId
* @return bool
*/
public function isPaymentMethodActive($sPaymentId)
{
$blActive = false;

$oPayment = $this->loadPaymentMethod($sPaymentId);
if ($oPayment !== false && (bool)$oPayment->oxpayments__oxactive->value === true) {
$blActive = true;
}
return $blActive;
}

/**
* @param string $sPaymentId
* @return bool
*/
public function isLiveMode($sPaymentId)
{
$blLiveMode = false;

$oPayment = $this->loadPaymentMethod($sPaymentId);
if ($oPayment !== false && (bool)$oPayment->oxpayments__fcpolivemode->value === true) {
$blLiveMode = true;
}
return $blLiveMode;
}
}
162 changes: 162 additions & 0 deletions application/helper/fcpopaypalhelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

class fcpopaypalhelper extends fcpobasehelper
{
const PPE_EXPRESS = 'fcpopaypal_express';
const PPE_V2_EXPRESS = 'fcpopaypalv2_express';

/**
* @var self
*/
protected static $oInstance;


/**
* Locale codes supported by misc images (marks, shortcuts etc)
*
* @var array
*/
protected $aSupportedLocales = [
'de_DE',
'en_AU',
'en_GB',
'en_US',
'es_ES',
'es_XC',
'fr_FR',
'fr_XC',
'it_IT',
'ja_JP',
'nl_NL',
'pl_PL',
'zh_CN',
'zh_XC',
];

/**
* Create instance of payment helper singleton
*
* @return static
*/
public static function getInstance()
{
if (self::$oInstance === null) {
self::$oInstance = oxNew(self::class);
}
return self::$oInstance;
}

/**
* Resets singleton class
* Needed for unit testing
*
* @return void
*/
public static function destroyInstance()
{
self::$oInstance = null;
}

/**
* @return bool
*/
public function showBNPLButton()
{
$blReturn = false;
if ((bool)$this->getMainHelper()->fcpoGetConfig()->getConfigParam('blFCPOPayPalV2BNPL') === true) {
$blReturn = true;
}
return $blReturn;
}

/**
* @return string
*/
protected function getIntent()
{
return "authorize"; // authorize = preauthorize // capture = authorize but Payone said to always use authorize
}

/**
* @return string
*/
protected function getCurrency()
{
return $this->getMainHelper()->fcpoGetSession()->getBasket()->getBasketCurrency()->name;
}

/**
* @return string
*/
protected function getMerchantId()
{
$sMerchantId = "3QK84QGGJE5HW"; // Default for testmode (fixed)
if (fcpopaymenthelper::getInstance()->isLiveMode(self::PPE_V2_EXPRESS)) {
$sMerchantId = $this->getMainHelper()->fcpoGetConfig()->getConfigParam('blFCPOPayPalV2MerchantID');
}
return $sMerchantId;
}

/**
* @return string
*/
protected function getClientId()
{
$sClientId = "AUn5n-4qxBUkdzQBv6f8yd8F4AWdEvV6nLzbAifDILhKGCjOS62qQLiKbUbpIKH_O2Z3OL8CvX7ucZfh"; // Default for testmode (fixed)
if (fcpopaymenthelper::getInstance()->isLiveMode(self::PPE_V2_EXPRESS)) {
$sClientId = "AVNBj3ypjSFZ8jE7shhaY2mVydsWsSrjmHk0qJxmgJoWgHESqyoG35jLOhH3GzgEPHmw7dMFnspH6vim"; // Livemode (fixed)
}
return $sClientId;
}

/**
* Check whether specified locale code is supported. Fallback to en_US
*
* @param string $sLocale
* @return string
*/
protected function getSupportedLocaleCode($sLocale = null)
{
if (!$sLocale || !in_array($sLocale, $this->aSupportedLocales)) {
return 'en_US';
}
return $sLocale;
}

/**
* @return string
*/
protected function getLocale()
{
$sCurrentLocal = $this->getMainHelper()->fcpoGetLang()->translateString('FCPO_LOCALE', null, false);
return $this->getSupportedLocaleCode($sCurrentLocal);
}

/**
* @return string
*/
public function getJavascriptUrl()
{
$sUrl = "https://www.paypal.com/sdk/js?client-id=".$this->getClientId()."&merchant-id=".$this->getMerchantId()."&currency=".$this->getCurrency()."&intent=".$this->getIntent()."&locale=".$this->getLocale()."&commit=false&vault=false&disable-funding=card,sepa,bancontact";
if ($this->showBNPLButton() === true) {
$sUrl .= "&enable-funding=paylater";
}
return $sUrl;
}

/**
* @return string
*/
public function getButtonColor()
{
return $this->getMainHelper()->fcpoGetConfig()->getConfigParam('blFCPOPayPalV2ButtonColor');
}

/**
* @return string
*/
public function getButtonShape()
{
return $this->getMainHelper()->fcpoGetConfig()->getConfigParam('blFCPOPayPalV2ButtonShape');
}
}
Loading

0 comments on commit 6cacb02

Please sign in to comment.