diff --git a/Helper/BankInformationHelper.php b/Helper/BankInformationHelper.php index 5d0e399..30caa29 100644 --- a/Helper/BankInformationHelper.php +++ b/Helper/BankInformationHelper.php @@ -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; @@ -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()) { @@ -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; } } diff --git a/Helper/User/LegalUserHelper.php b/Helper/User/LegalUserHelper.php new file mode 100644 index 0000000..b056bcb --- /dev/null +++ b/Helper/User/LegalUserHelper.php @@ -0,0 +1,160 @@ +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; + } +} diff --git a/Helper/User/NaturalUserHelper.php b/Helper/User/NaturalUserHelper.php new file mode 100644 index 0000000..5e56392 --- /dev/null +++ b/Helper/User/NaturalUserHelper.php @@ -0,0 +1,83 @@ +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; + } +} diff --git a/Helper/User/UserHelper.php b/Helper/User/UserHelper.php new file mode 100644 index 0000000..d913162 --- /dev/null +++ b/Helper/User/UserHelper.php @@ -0,0 +1,105 @@ +naturalUserHelper = $naturalUserHelper; + $this->legalUserHelper = $legalUserHelper; + $this->mangopayHelper = $mangopayHelper; + } + + /** + * @param UserInterface $user + * @return \MangoPay\UserLegal|UserNatural + * @throws \Exception + */ + public function findOrCreateMangoUser(UserInterface $user) + { + if ($mangoUserId = $user->getMangoUserId()) { + $mangoUser = $this->mangopayHelper->Users->get($mangoUserId); + } else { + $mangoUser = $this->createMangoUser($user); + } + + return $mangoUser; + } + + /** + * @param UserInterface $user + * @return \MangoPay\UserLegal|UserNatural + * @throws \Exception + */ + public function createMangoUser(UserInterface $user) + { + return $this->getUserHelper($user)->createMangoUser($user); + } + + /** + * @param UserInterface $user + * @return \MangoPay\UserLegal|UserNatural + * @throws \Exception + */ + public function updateMangoUser(UserInterface $user) + { + return $this->getUserHelper($user)->updateMangoUser($user); + } + + /** + * @param BankInformationInterface $bankInformation + * @return mixed + * @throws \Exception + */ + public function createBankAccount(BankInformationInterface $bankInformation) + { + return $this->getUserHelper($bankInformation->getUser())->createBankAccount($bankInformation); + } + + public function getTransactions($userId) + { + return $this->mangopayHelper->Users->GetTransactions($userId); + } + + /** + * @param UserInterface $user + * @return LegalUserHelper|NaturalUserHelper + * @throws \Exception + */ + protected function getUserHelper(UserInterface $user) + { + switch (true) { + case $user instanceof NaturalUserInterface: + return $this->naturalUserHelper; + break; + case $user instanceof LegalUserInterface: + return $this->legalUserHelper; + break; + default: + throw new \Exception("Unable to find a UserHelper that match given user: " . get_class($user)); + } + } + +}