Skip to content

Commit

Permalink
Create UserHelpers for Legals¨ and Naturals
Browse files Browse the repository at this point in the history
  • Loading branch information
paulandrieux committed Sep 20, 2018
1 parent deacedc commit e3abdf9
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 51 deletions.
78 changes: 27 additions & 51 deletions Helper/BankInformationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use MangoPay\BankAccount;
use MangoPay\BankAccountDetailsIBAN;
use MangoPay\User;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Troopers\MangopayBundle\Entity\BankInformationInterface;
use Troopers\MangopayBundle\Entity\UserInterface;
use Troopers\MangopayBundle\Helper\User\UserHelper;

/**
* ref: troopers_mangopay.bank_information_helper.
**/
class BankInformationHelper
{
private $mangopayHelper;
Expand All @@ -23,6 +20,11 @@ public function __construct(MangopayHelper $mangopayHelper, UserHelper $userHelp
$this->userHelper = $userHelper;
}

/**
* @param BankInformationInterface $bankInformation
* @return BankAccount
* @throws \Exception
*/
public function findOrCreateBankAccount(BankInformationInterface $bankInformation)
{
if ($mangoBankAccountId = $bankInformation->getMangoBankAccountId()) {
Expand All @@ -34,71 +36,45 @@ public function findOrCreateBankAccount(BankInformationInterface $bankInformatio
return $mangoBankAccount;
}

/**
* @param BankInformationInterface $bankInformation
* @return BankAccount
* @throws \Exception
*/
public function createBankAccount(BankInformationInterface $bankInformation)
{
$mangoUser = $this->userHelper->findOrCreateMangoUser($bankInformation->getUser());
//Create mango bank account
/** @var UserInterface $user */
$user = $bankInformation->getUser();
$mangoUser = $this->userHelper->findOrCreateMangoUser($user);

$bankAccount = new BankAccount();
$bankAccount->OwnerName = $bankInformation->getUser()->getFullName();
$bankAccount->OwnerName = $bankInformation->getBankInformationFullName();
$bankAccount->UserId = $mangoUser->Id;
$bankAccount->Type = 'IBAN';
$bankAccount->OwnerAddress = $bankInformation->getAddress();

$bankAccountDetailsIban = new BankAccountDetailsIBAN();
$bankAccountDetailsIban->IBAN = $bankInformation->getIban();

$bankAccount->Details = $bankAccountDetailsIban;

$bankAccount = $this->mangopayHelper->Users->CreateBankAccount($bankInformation->getUser()->getMangoUserId(), $bankAccount);

$bankInformation->setMangoBankAccountId($bankAccount->Id);

$this->entityManager->persist($bankInformation);
$this->entityManager->flush();

return $bankAccount;
}

public function createBankAccountForUser(UserInterface $user, $iban)
{
$bankAccount = new \MangoPay\BankAccount();
$bankAccount->OwnerName = $this->getUserFullName($user);
$bankAccount->UserId = $user->getMangoUserId();
$bankAccount->Type = 'IBAN';

$address = new \MangoPay\Address();
$userAddress = $user->getAddress();
$city = $user->getCity();
$postalCode = $user->getPostalCode();
$userAddress = $bankInformation->getBankInformationStreetAddress();
$city = $bankInformation->getBankInformationCity();
$postalCode = $bankInformation->getBankInformationPostalCode();
if (null == $userAddress || null == $city || null == $postalCode) {
throw new NotFoundHttpException(sprintf('address, city or postalCode missing for User id : %s', $user->getId()));
throw new NotFoundHttpException(sprintf('address, city or postalCode missing for BankInformation of User id : %s', $user->getId()));
}
$address->AddressLine1 = $userAddress;
$address->AddressLine2 = $bankInformation->getBankInformationAdditionalStreetAddress();
$address->City = $city;
$address->Country = $user->getCountry();
$address->Country = $bankInformation->getBankInformationCountry();
$address->PostalCode = $postalCode;
$bankAccount->OwnerAddress = $address;

$bankAccountDetailsIban = new \MangoPay\BankAccountDetailsIBAN();
$bankAccountDetailsIban->IBAN = $iban;
$bankAccountDetailsIban = new BankAccountDetailsIBAN();
$bankAccountDetailsIban->IBAN = $bankInformation->getIban();

$bankAccount->Details = $bankAccountDetailsIban;

return $this->mangopayHelper->Users->CreateBankAccount($user->getMangoUserId(), $bankAccount);
}
$bankAccount = $this->mangopayHelper->Users->CreateBankAccount($mangoUser->Id, $bankAccount);

/**
* Implode Users's full name with firstName and lastName.
*
* @param User $user
*
* @return string
*/
public function getUserFullName(UserInterface $user)
{
$firstName = $user->getFirstName();
$lastName = $user->getLastName();
$bankInformation->setMangoBankAccountId($bankAccount->Id);

return $firstName.' '.$lastName;
return $bankAccount;
}
}
160 changes: 160 additions & 0 deletions Helper/User/LegalUserHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace Troopers\MangopayBundle\Helper\User;

use Doctrine\ORM\EntityManager;
use MangoPay\KycLevel;
use MangoPay\User;
use MangoPay\UserLegal;
use MangoPay\UserNatural;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\File\File;
use Troopers\MangopayBundle\Entity\BankInformationInterface;
use Troopers\MangopayBundle\Entity\LegalUserInterface;
use Troopers\MangopayBundle\Entity\UserInterface;
use Troopers\MangopayBundle\Event\UserEvent;
use Troopers\MangopayBundle\Helper\KYCHelper;
use Troopers\MangopayBundle\TroopersMangopayEvents;
use Troopers\MangopayBundle\Helper\MangopayHelper;
use MangoPay\BankAccount;
use MangoPay\BankAccountDetailsIBAN;

class LegalUserHelper
{
private $mangopayHelper;
private $dispatcher;
private $KYCHelper;

public function __construct(MangopayHelper $mangopayHelper, EventDispatcherInterface $dispatcher, KYCHelper $KYCHelper)
{
$this->mangopayHelper = $mangopayHelper;
$this->dispatcher = $dispatcher;
$this->KYCHelper = $KYCHelper;
}

public function createMangoUser(LegalUserInterface $user)
{
$birthday = null;
if ($user->getLegalRepresentativeBirthday() instanceof \Datetime) {
$birthday = $user->getLegalRepresentativeBirthday();
} else if (null !== $user->getLegalRepresentativeBirthday()) {
$birthday = new \DateTime($user->getLegalRepresentativeBirthday());
}
$mangoUser = new UserLegal();
$mangoUser->LegalPersonType = $user->getLegalPersonType();
$mangoUser->Name = $user->getName();
$mangoUser->Email = $user->getEmail();
$mangoUser->LegalRepresentativeFirstName = $user->getLegalRepresentativeFirstName();
$mangoUser->LegalRepresentativeLastName = $user->getLegalRepresentativeLastName();
$mangoUser->LegalRepresentativeBirthday = $birthday ? $birthday->getTimestamp() : null;
$mangoUser->LegalRepresentativeNationality = $user->getLegalRepresentativeNationality();
$mangoUser->LegalRepresentativeCountryOfResidence = $user->getLegalRepresentativeCountryOfResidence();

$address = new \MangoPay\Address();
$address->AddressLine1 = $user->getLegalRepresentativeStreetAddress();
$address->AddressLine2 = $user->getLegalRepresentativeAdditionalStreetAddress();
$address->City = $user->getLegalRepresentativeCity();
$address->Country = $user->getLegalRepresentativeCountry();
$address->PostalCode = $user->getLegalRepresentativePostalCode();

$mangoUser->Address = $address;

$headQuartersAddress = new \MangoPay\Address();
$headQuartersAddress->AddressLine1 = $user->getHeadquartersStreetAddress();
$headQuartersAddress->AddressLine2 = $user->getHeadquartersAdditionalStreetAddress();
$headQuartersAddress->City = $user->getHeadquartersCity();
$headQuartersAddress->Country = $user->getHeadquartersCountry();
$headQuartersAddress->PostalCode = $user->getHeadquartersPostalCode();

$mangoUser->HeadquartersAddress = $headQuartersAddress;

$mangoUser = $this->mangopayHelper->Users->Create($mangoUser);
$user->setMangoUserId($mangoUser->Id);

if (null !== $document = $user->getProofOfRegistration()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->ProofOfRegistration = $mangoDocument->Id;
$user->getProofOfRegistrationId($mangoDocument->Id);
}

if (null !== $document = $user->getLegalRepresentativeProofOfIdentity()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->LegalRepresentativeProofOfIdentity = $mangoDocument->Id;
$user->getLegalRepresentativeProofOfIdentityId($mangoDocument->Id);
}

if (null !== $document = $user->getStatute()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->Statute = $mangoDocument->Id;
$user->getStatuteId($mangoDocument->Id);
}

$event = new UserEvent($user, $mangoUser);
$this->dispatcher->dispatch(TroopersMangopayEvents::NEW_USER, $event);

return $mangoUser;
}

public function updateMangoUser(LegalUserInterface $user)
{
if ($user->getLegalRepresentativeBirthday() instanceof \Datetime) {
$birthday = $user->getLegalRepresentativeBirthday()->getTimestamp();
}

$mangoUserId = $user->getMangoUserId();
$mangoUser = $this->mangopayHelper->Users->get($mangoUserId);

$mangoUser->Email = $user->getEmail();
$mangoUser->LegalRepresentativeFirstName = $user->getLegalRepresentativeFirstName();
$mangoUser->LegalRepresentativeLastName = $user->getLegalRepresentativeLastName();
$mangoUser->LegalRepresentativeBirthday = $birthday;
$mangoUser->LegalRepresentativeNationality = $user->getLegalRepresentativeNationality();
$mangoUser->LegalRepresentativeCountryOfResidence = $user->getLegalRepresentativeCountryOfResidence();
$mangoUser->Tag = $user->getId();

$address = new \MangoPay\Address();
$address->AddressLine1 = $user->getLegalRepresentativeStreetAddress();
$address->City = $user->getLegalRepresentativeCity();
$address->Country = $user->getLegalRepresentativeCountry();
$address->PostalCode = $user->getLegalRepresentativePostalCode();

$mangoUser->Address = $address;


if (null !== $document = $user->getProofOfRegistration()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->ProofOfRegistration = $mangoDocument->Id;
$user->setProofOfRegistrationId($mangoDocument->Id);
}

if (null !== $document = $user->getLegalRepresentativeProofOfIdentity()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->LegalRepresentativeProofOfIdentity = $mangoDocument->Id;
$user->setLegalRepresentativeProofOfIdentityId($mangoDocument->Id);
}

if (null !== $document = $user->getStatute()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->Statute = $mangoDocument->Id;
$user->setStatuteId($mangoDocument->Id);
}

if (null !== $document = $user->getShareholderDeclaration()) {
$mangoDocument = $this->createDocument($document, $user);
$mangoUser->ShareholderDeclaration = $mangoDocument->Id;
$user->setShareholderDeclarationId($mangoDocument->Id);
}

$mangoUser = $this->mangopayHelper->Users->Update($mangoUser);

return $mangoUser;
}

protected function createDocument(File $file, UserInterface $user)
{
$document = $this->KYCHelper->createDocument($file);
$document = $this->mangopayHelper->Users->CreateKycDocument($user->getMangoUserId(), $document);

return $document;
}
}
83 changes: 83 additions & 0 deletions Helper/User/NaturalUserHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Troopers\MangopayBundle\Helper\User;

use Doctrine\ORM\EntityManager;
use MangoPay\UserNatural;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Troopers\MangopayBundle\Entity\NaturalUserInterface;
use Troopers\MangopayBundle\Entity\UserInterface;
use Troopers\MangopayBundle\Event\UserEvent;
use Troopers\MangopayBundle\TroopersMangopayEvents;
use Troopers\MangopayBundle\Helper\MangopayHelper;
use MangoPay\BankAccount;
use MangoPay\BankAccountDetailsIBAN;
use Troopers\MangopayBundle\Entity\BankInformationInterface;

class NaturalUserHelper
{
private $mangopayHelper;
private $dispatcher;

public function __construct(MangopayHelper $mangopayHelper, EventDispatcherInterface $dispatcher)
{
$this->mangopayHelper = $mangopayHelper;
$this->dispatcher = $dispatcher;
}

public function createMangoUser(NaturalUserInterface $user)
{
$birthday = null;
if ($user->getBirthday() instanceof \Datetime) {
$birthday = $user->getBirthday();
} else if (null !== $user->getBirthday()) {
$birthday = new \DateTime($user->getBirthday());
}
$mangoUser = new UserNatural();
$mangoUser->Email = $user->getEmail();
$mangoUser->FirstName = $user->getFirstName();
$mangoUser->LastName = $user->getLastName();
$mangoUser->Birthday = $birthday ? $birthday->getTimestamp() : null;
$mangoUser->Nationality = $user->getNationality();
$mangoUser->CountryOfResidence = $user->getCountry();
$mangoUser->Tag = $user->getId();

$mangoUser = $this->mangopayHelper->Users->Create($mangoUser);

$event = new UserEvent($user, $mangoUser);
$this->dispatcher->dispatch(TroopersMangopayEvents::NEW_USER, $event);

return $mangoUser;
}

public function updateMangoUser(NaturalUserInterface $user)
{

if ($user->getBirthday() instanceof \Datetime) {
$birthdate = $user->getBirthday()->getTimestamp();
}
$mangoUserId = $user->getMangoUserId();
$mangoUser = $this->mangopayHelper->Users->get($mangoUserId);

$mangoUser->Email = $user->getEmail();
$mangoUser->FirstName = $user->getFirstname();
$mangoUser->LastName = $user->getLastname();
$mangoUser->Birthday = $birthdate;
$mangoUser->Nationality = $user->getNationality();
$mangoUser->CountryOfResidence = $user->getCountry();
$mangoUser->Tag = $user->getId();

$address = new \MangoPay\Address();
$address->AddressLine1 = $user->getStreetAddress();
$address->AddressLine2 = $user->getAdditionalStreetAddress();
$address->City = $user->getCity();
$address->Country = $user->getCountry();
$address->PostalCode = $user->getPostalCode();

$mangoUser->Address = $address;

$mangoUser = $this->mangopayHelper->Users->Update($mangoUser);

return $mangoUser;
}
}
Loading

0 comments on commit e3abdf9

Please sign in to comment.