Skip to content

Commit

Permalink
Merge pull request vindi#81 from cedran/master
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
cedran authored Jul 4, 2024
2 parents 7b5fcc6 + fe507ca commit 18111fb
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions Model/Payment/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Vindi\Payment\Model\ResourceModel\PaymentProfile\CollectionFactory as PaymentProfileCollectionFactory;

class Customer
{
Expand All @@ -30,25 +31,31 @@ class Customer
/** @var StoreManagerInterface */
protected $storeManager;

/** @var PaymentProfileCollectionFactory */
protected $paymentProfileCollectionFactory;

/**
* @param CustomerRepositoryInterface $customerRepository
* @param Api $api
* @param ManagerInterface $messageManager
* @param AddressRepositoryInterface $addressRepository
* @param StoreManagerInterface $storeManager
* @param PaymentProfileCollectionFactory $paymentProfileCollectionFactory
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
Api $api,
ManagerInterface $messageManager,
AddressRepositoryInterface $addressRepository,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
PaymentProfileCollectionFactory $paymentProfileCollectionFactory
) {
$this->customerRepository = $customerRepository;
$this->api = $api;
$this->messageManager = $messageManager;
$this->addressRepository = $addressRepository;
$this->storeManager = $storeManager;
$this->paymentProfileCollectionFactory = $paymentProfileCollectionFactory;
}

/**
Expand All @@ -62,14 +69,18 @@ public function findOrCreate(Order $order)
{
$billing = $order->getBillingAddress();
$customer = null;
$customerId = null;
$vindiCustomerId = null;

if (!$order->getCustomerIsGuest()) {
$customer = $this->customerRepository->get($billing->getEmail());
$customerId = $this->findVindiCustomerByEmail($customer->getEmail());
$vindiCustomerId = $this->findVindiCustomerIdByCustomerId($customer->getId());
}

if (!$vindiCustomerId && $customer) {
$vindiCustomerId = $this->findVindiCustomerByEmail($customer->getEmail());
}

if ($customerId) {
if ($vindiCustomerId) {
if ($order->getPayment()->getMethod() == "vindi_pix") {
$customerVindi = $this->getVindiCustomerData($customer->getId());
$additionalInfo = $order->getPayment()->getAdditionalInformation();
Expand All @@ -78,13 +89,13 @@ public function findOrCreate(Order $order)
$updateData = [
'registry_code' => $taxVatOrder,
];
$this->updateVindiCustomer($customerId, $updateData);
$this->updateVindiCustomer($vindiCustomerId, $updateData);
$customer->setTaxvat($additionalInfo['document'] ?? '');
$this->customerRepository->save($customer);
}
}

return $customerId;
return $vindiCustomerId;
}

$address = [
Expand All @@ -107,16 +118,16 @@ public function findOrCreate(Order $order)
'address' => $address
];

$customerId = $this->createCustomer($customerVindi);
$vindiCustomerId = $this->createCustomer($customerVindi);

if ($customerId === false) {
if ($vindiCustomerId === false) {
$this->messageManager->addErrorMessage(__('Failed while registering user. Check the data and try again'));
throw new \Magento\Framework\Exception\LocalizedException(
__('Failed while registering user. Check the data and try again')
);
}

return $customerId;
return $vindiCustomerId;
}

/**
Expand All @@ -128,10 +139,14 @@ public function findOrCreate(Order $order)
*/
public function findOrCreateFromCustomerAccount(CustomerInterface $customer)
{
$customerId = $this->findVindiCustomerByEmail($customer->getEmail());
$vindiCustomerId = $this->findVindiCustomerIdByCustomerId($customer->getId());

if ($customerId) {
return $customerId;
if (!$vindiCustomerId) {
$vindiCustomerId = $this->findVindiCustomerByEmail($customer->getEmail());
}

if ($vindiCustomerId) {
return $vindiCustomerId;
}

$billingAddressId = $customer->getDefaultBilling();
Expand Down Expand Up @@ -206,14 +221,27 @@ public function findOrCreateFromCustomerAccount(CustomerInterface $customer)
'address' => $address
];

$customerId = $this->createCustomer($customerVindi);
$vindiCustomerId = $this->createCustomer($customerVindi);

if ($customerId === false) {
if ($vindiCustomerId === false) {
$this->messageManager->addErrorMessage(__('Failed while registering user. Check the data and try again'));
return false;
}

return $customerId;
return $vindiCustomerId;
}

/**
* Find Vindi customer ID by Magento customer ID using ORM.
*
* @param int $customerId
* @return string|false
*/
public function findVindiCustomerIdByCustomerId($customerId)
{
$collection = $this->paymentProfileCollectionFactory->create();
$item = $collection->addFieldToFilter('customer_id', $customerId)->getFirstItem();
return $item->getVindiCustomerId() ?: false;
}

/**
Expand All @@ -232,15 +260,14 @@ public function createCustomer($body)
return false;
}


/**
* Update customer Vindi.
*
* @param string $query
*
* @param string $customerId
* @param array $body
* @return array|bool|mixed
*/
public function updateVindiCustomer($customerId ,$body)
public function updateVindiCustomer($customerId, $body)
{
$response = $this->api->request("customers/{$customerId}", 'PUT', $body);

Expand Down Expand Up @@ -329,7 +356,7 @@ public function formatPhone($phone)
protected function getDocument(Order $order)
{
$document = (string) $order->getPayment()->getAdditionalInformation('document');
if(!$document) {
if (!$document) {
$document = (string) $order->getData('customer_taxvat');
}
return $document;
Expand Down

0 comments on commit 18111fb

Please sign in to comment.