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

Oro 5.0 Compatibility Fixes #36

Draft
wants to merge 2 commits into
base: 5.0
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ Extension Points
-----------
#### Events

BraintreePaymentActionEvent (aligent_braintree.payment_action.{action}):
BraintreePaymentActionEvent (aligent_braintree.payment_action):

This event is fired when a payment action executes but before the payload is sent to the payment gateway. It is used internally
to build up the payment payload and can be extended with other listeners to add data to be sent to the payment gateway.

Currently supported events:
- aligent_braintree.payment_action.purchase
Use the `$this->getAction()` method to restrict Event Listener to a specific Braintree Action

Currently supported actions:
- Purchase (`\Aligent\BraintreeBundle\Method\Action\PurchaseAction::ACTION`)

#### Actions

Expand All @@ -78,7 +80,7 @@ Custom payment actions can be added by implementing the BraintreeActionInterface

Support
-------
If you have any issues with this bundle, please feel free to open [GitHub issue](https://github.com/aligent/braintree-orocommerce/issues) with version and steps to reproduce.
If you have any issues with this bundle, please feel free to open a [GitHub issue](https://github.com/aligent/braintree-orocommerce/issues) with version and detailed steps to reproduce.

Contribution
------------
Expand Down
5 changes: 1 addition & 4 deletions src/AligentBraintreeBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

class AligentBraintreeBundle extends Bundle
{
/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
$container
->addCompilerPass(new ActionPass())
Expand Down
80 changes: 36 additions & 44 deletions src/Braintree/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
namespace Aligent\BraintreeBundle\Braintree;

use Aligent\BraintreeBundle\Method\Config\BraintreeConfigInterface;
use Braintree\ClientToken;
use Braintree\Configuration;
use Braintree\Customer;
use Braintree\Exception\NotFound;
use Braintree\Transaction;
use Braintree\Gateway as BraintreeGateway;
use Braintree\Result\Error as BraintreeErrorResult;
use Braintree\Result\Successful as BraintreeSuccessResult;
use Doctrine\ORM\ORMException;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;

Expand All @@ -24,31 +24,15 @@ class Gateway
const PRODUCTION = 'production';
const SANDBOX = 'sandbox';

/**
* @var BraintreeConfigInterface
*/
public $config;

/**
* @var \Braintree\Gateway
*/
protected $braintreeGateway;
protected BraintreeConfigInterface $config;
protected BraintreeGateway $braintreeGateway;
protected DoctrineHelper $doctrineHelper;

/**
* @var DoctrineHelper
*/
protected $doctrineHelper;

/**
* Gateway constructor.
* @param BraintreeConfigInterface $config
* @param DoctrineHelper $doctrineHelper
*/
public function __construct(BraintreeConfigInterface $config, DoctrineHelper $doctrineHelper)
{
$this->config = $config;
$this->doctrineHelper = $doctrineHelper;
$this->braintreeGateway = new \Braintree\Gateway(
$this->braintreeGateway = new BraintreeGateway(
[
'environment' => $config->getEnvironment(),
'publicKey' => $config->getPublicKey(),
Expand All @@ -60,13 +44,14 @@ public function __construct(BraintreeConfigInterface $config, DoctrineHelper $do

/**
* Generate Braintree Authentication Token for a customer user
* @param CustomerUser $customerUser
* @return string
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function getCustomerAuthToken(CustomerUser $customerUser)
public function getCustomerAuthToken(CustomerUser $customerUser): string
{
if (!method_exists($customerUser, 'getBraintreeId')) {
// Schema Migrations most likely have not been run
return '';
}

$braintreeId = $customerUser->getBraintreeId();

// if the customer doesn't already exist with braintree create it
Expand All @@ -85,43 +70,44 @@ public function getCustomerAuthToken(CustomerUser $customerUser)
try {
$this->braintreeGateway->customer()->find($braintreeId);
} catch (NotFound $exception) {
return $this->getAuthToken();
return $this->getAuthToken();
}

return $this->braintreeGateway->clientToken()->generate(
[
'customerId' => $braintreeId
]
);
/** @phpstan-ignore-next-line BrainTree ClientTokenGateway is incorrectly type-hinted, it accepts an array */
return $this->braintreeGateway->clientToken()->generate([
'customerId' => $braintreeId,
]);
}

/**
* Generate a generic auth token for braintree
* @return string
*/
public function getAuthToken()
public function getAuthToken(): string
{
return $this->braintreeGateway->clientToken()->generate();
}

/**
* Charge the payment nonce
* @param array $params
* @return \Braintree\Result\Error|\Braintree\Result\Successful
* @param array<string,mixed> $params
* @return BraintreeErrorResult|BraintreeSuccessResult
*/
public function sale(array $params)
public function sale(array $params): BraintreeErrorResult|BraintreeSuccessResult
{
return $this->braintreeGateway->transaction()->sale($params);
}

/**
* @param CustomerUser $customerUser
* @return \Braintree\Result\Error|\Braintree\Result\Successful
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @return BraintreeErrorResult|BraintreeSuccessResult
*/
public function createBraintreeCustomer(CustomerUser $customerUser)
public function createBraintreeCustomer(CustomerUser $customerUser): BraintreeErrorResult|BraintreeSuccessResult
{
if (!method_exists($customerUser, 'setBraintreeId')) {
return new BraintreeErrorResult('Braintree Bundle not installed correctly');
}

$result = $this->braintreeGateway->customer()->create(
[
'firstName' => $customerUser->getFirstName(),
Expand All @@ -133,8 +119,14 @@ public function createBraintreeCustomer(CustomerUser $customerUser)

if ($result->success) {
$em = $this->doctrineHelper->getEntityManager(CustomerUser::class);
/* @phpstan-ignore-next-line The Braintree Result classes unfortunately use magic methods */
$customerUser->setBraintreeId($result->customer->id);
$em->flush($customerUser);

try {
$em->flush($customerUser);
} catch (ORMException $e) {
return new BraintreeErrorResult($e->getMessage());
}
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,24 @@
class ApplePayConfigurationBuilder implements ConfigurationBuilderInterface, FeatureToggleableInterface
{
use FeatureCheckerHolderTrait;
/**
* @var TotalProcessorProvider
*/
protected $totalsProvider;

/**
* PayPalCreditSettingsBuilder constructor.
* @param TotalProcessorProvider $totalsProvider
*/
protected TotalProcessorProvider $totalsProvider;

public function __construct(TotalProcessorProvider $totalsProvider)
{
$this->totalsProvider = $totalsProvider;
}

/**
* Build the settings object to pass to Dropin
* @param PaymentContextInterface $context
* @param array $configuration
* @return mixed
* {@inheritDoc}
*/
public function build(PaymentContextInterface $context, array $configuration)
public function build(PaymentContextInterface $context, array $configuration): array
{
if (!$this->isFeaturesEnabled()) {
return;
return [];
}

// Strip Null values
// Strip null values
$viewSettings = array_filter(
$configuration,
function ($value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@

class ChainConfigurationBuilder implements ConfigurationBuilderInterface
{
/** @var ConfigurationBuilderInterface[] */
protected array $builders = [];

/**
* @var ConfigurationBuilderInterface[] $builders
* {@inheritDoc}
*/
protected $builders = [];

/**
* Build the settings object to pass to the Drop-in UI
* @param PaymentContextInterface $context
* @param array $configuration
* @return mixed
*/
public function build(PaymentContextInterface $context, array $configuration)
public function build(PaymentContextInterface $context, array $configuration): array
{
$config = [];
foreach ($configuration as $paymentMethod => $paymentMethodConfig) {
Expand All @@ -39,7 +33,7 @@ public function build(PaymentContextInterface $context, array $configuration)
}

$config[$paymentMethod] = $paymentMethodConfig;
} elseif (!$paymentMethodConfig['enabled'] && $paymentMethod === 'card') {
} elseif ($paymentMethod === 'card') {
// handle the card special case, as card is the default it must be explicitly
// set to false, so it doesn't display
$config[$paymentMethod] = false;
Expand All @@ -49,20 +43,12 @@ public function build(PaymentContextInterface $context, array $configuration)
return $config;
}

/**
* @param $method
* @return bool
*/
public function hasBuilder($method)
public function hasBuilder(string $method): bool
{
return isset($this->builders[$method]);
}

/**
* @param $method
* @return ConfigurationBuilderInterface
*/
public function getBuilder($method)
public function getBuilder(string $method): ConfigurationBuilderInterface
{
if (!$this->hasBuilder($method)) {
throw new \InvalidArgumentException("Builder for {$method} does not exist.");
Expand All @@ -71,12 +57,7 @@ public function getBuilder($method)
return $this->builders[$method];
}

/**
* @param $method
* @param ConfigurationBuilderInterface $builder
* @return $this
*/
public function addBuilder($method, ConfigurationBuilderInterface $builder)
public function addBuilder(string $method, ConfigurationBuilderInterface $builder): self
{
if ($this->hasBuilder($method)) {
throw new \InvalidArgumentException("Builder for {$method} already exists");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

interface ConfigurationBuilderInterface
{

/**
* Build the settings object to pass to Dropin
* Build the settings object to pass to Drop-in
* @param PaymentContextInterface $context
* @param array $configuration
* @return mixed
* @param array<string,mixed> $configuration
* @return array<string,mixed>
*/
public function build(PaymentContextInterface $context, array $configuration);
public function build(PaymentContextInterface $context, array $configuration): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,20 @@ class GooglePayConfigurationBuilder implements ConfigurationBuilderInterface, Fe
{
use FeatureCheckerHolderTrait;

/**
* @var TotalProcessorProvider
*/
protected $totalsProvider;
protected TotalProcessorProvider $totalsProvider;

/**
* PayPalCreditSettingsBuilder constructor.
* @param TotalProcessorProvider $totalsProvider
*/
public function __construct(TotalProcessorProvider $totalsProvider)
{
$this->totalsProvider = $totalsProvider;
}

/**
* Build the settings object to pass to Dropin
* @param PaymentContextInterface $context
* @param array $configuration
* @return mixed
* {@inheritDoc}
*/
public function build(PaymentContextInterface $context, array $configuration)
public function build(PaymentContextInterface $context, array $configuration): array
{
if (!$this->isFeaturesEnabled()) {
return;
return [];
}

// Strip Null values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,17 @@

class PayPalConfigurationBuilder implements ConfigurationBuilderInterface
{
protected TotalProcessorProvider $totalsProvider;

/**
* @var TotalProcessorProvider
*/
protected $totalsProvider;

/**
* PayPalCreditSettingsBuilder constructor.
* @param TotalProcessorProvider $totalsProvider
*/
public function __construct(TotalProcessorProvider $totalsProvider)
{
$this->totalsProvider = $totalsProvider;
}

/**
* @inheritdoc
/**
* {@inheritDoc}
*/
public function build(PaymentContextInterface $context, array $configuration)
public function build(PaymentContextInterface $context, array $configuration): array
{
// Strip Null values
$viewSettings = array_filter(
Expand Down
Loading