Skip to content

Commit

Permalink
Merge pull request #69 from Troopers/feature/mandate
Browse files Browse the repository at this point in the history
Feature/mandate
  • Loading branch information
paulandrieux authored Apr 9, 2019
2 parents d7446b8 + 2a92d5b commit 3f4edd9
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 43 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->booleanNode('sandbox_mode')->defaultValue(false)->end()
->booleanNode('debug_mode')->defaultValue(false)->end()
->scalarNode('client_id')->isRequired()->end()
->scalarNode('client_password')->isRequired()->end()
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/TroopersMangopayExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$container->setParameter('troopers_mangopay.debug_mode', $config['debug_mode'] === true);
$container->setParameter('troopers_mangopay.sandbox_mode', $config['sandbox_mode']);
$container->setParameter('troopers_mangopay.debug_mode', $config['debug_mode']);
$container->setParameter('troopers_mangopay.client_id', $config['client_id']);
$container->setParameter('troopers_mangopay.client_password', $config['client_password']);
$container->setParameter('troopers_mangopay.base_url', $config['base_url']);
Expand Down
18 changes: 10 additions & 8 deletions Entity/BankInformationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,63 @@ interface BankInformationInterface
/**
* BankInformation address.
*
* @var string
* @return string
*/
public function getBankInformationStreetAddress();

/**
* BankInformation address.
*
* @var string
* @return string
*/
public function getBankInformationAdditionalStreetAddress();

/**
* BankInformation address.
*
* @var string
* @return string
*/
public function getBankInformationCity();

/**
* BankInformation address.
*
* @var string
* @return string
*/
public function getBankInformationPostalCode();

/**
* BankInformation address.
*
* @var string
* @return string
*/
public function getBankInformationCountry();

/**
* BankInformation name.
*
* @var string
* @return string
*/
public function getBankInformationFullName();

/**
* It represents the amount debited on the bank account of the Author.In cents so 100€ will be written like « Amount » : 10000
* DebitedFunds – Fees = CreditedFunds (amount received on wallet).
*
* @var string
* @return string
*/
public function getIban();

/**
* The user bank informations belongs to
*
* @var UserInterface
* @return UserInterface
*/
public function getUser();

public function getMangoBankAccountId();

public function setMangoBankAccountId($mangoBankAccountId);
public function setMangoMandateId($mangoMandateId);
public function setMangoMandateUrl($mangoMandateUrl);
}
4 changes: 4 additions & 0 deletions Entity/LegalUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ public function getCompanyNumber();
* behalf of the legal entity
*/
public function getLegalRepresentativeProofOfIdentityId();
public function setLegalRepresentativeProofOfIdentityId($id);

/**
* @var string
* Certified articles of association (Statute) - formal memorandum stated by the entrepreneurs, in which
* the following information is mentioned: business name, activity, registered address, shareholding…
*/
public function getStatuteId();
public function setStatuteId($id);

/**
* @var string
Expand All @@ -155,12 +157,14 @@ public function getStatuteId();
* authority
*/
public function getProofOfRegistrationId();
public function setProofOfRegistrationId($id);

/**
* @var string
* Shareholder declaration (as https://www.mangopay.com/terms/shareholder-declaration/Shareholder_Declaration-EN.pdf)
*/
public function getShareholderDeclarationId();
public function setShareholderDeclarationId($id);

/**
* @var File
Expand Down
2 changes: 1 addition & 1 deletion Entity/NaturalUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getPostalCode();
* @var \DateTime
* User’s birthdate.
*/
public function getBirthday() :?\DateTime;
public function getBirthday();

/**
* @var string
Expand Down
9 changes: 9 additions & 0 deletions Entity/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ public function getEmail();
* @var int
*/
public function getMangoUserId();
public function setMangoUserId($id);

/**
* @var int
*/
public function getMangoWalletId();
public function setMangoWalletId($id);

/**
* @var int
*/
public function getMangoCardId();
public function setMangoCardId($id);

/**
* @var int
*/
public function getMangoBankAccountId();
public function setMangoBankAccountId($id);

/**
* @var int
*/
public function getBankInformation();
}
53 changes: 51 additions & 2 deletions Helper/BankInformationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class BankInformationHelper
{
private $mangopayHelper;
private $userHelper;
private $mangopaySandbox;

public function __construct(MangopayHelper $mangopayHelper, UserHelper $userHelper)
public function __construct(MangopayHelper $mangopayHelper, UserHelper $userHelper, $mangopaySandbox)
{
$this->mangopayHelper = $mangopayHelper;
$this->userHelper = $userHelper;
$this->mangopaySandbox = $mangopaySandbox;
}

/**
Expand Down Expand Up @@ -67,7 +69,12 @@ public function createBankAccount(BankInformationInterface $bankInformation)
$bankAccount->OwnerAddress = $address;

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

$iban = $bankInformation->getIban();
if ($this->mangopaySandbox) {
$iban = "FR7611808009101234567890147";
}
$bankAccountDetailsIban->IBAN = $iban;

$bankAccount->Details = $bankAccountDetailsIban;

Expand All @@ -77,4 +84,46 @@ public function createBankAccount(BankInformationInterface $bankInformation)

return $bankAccount;
}

/**
* @param BankInformationInterface $bankInformation
* @return BankAccount
* @throws \Exception
*/
public function updateBankAccount(BankInformationInterface $bankInformation)
{
/** @var UserInterface $user */
$user = $bankInformation->getUser();
$bankAccount = $this->mangopayHelper->Users->GetBankAccount($user->getMangoUserId(), $bankInformation->getMangoBankAccountId());

$bankAccount->OwnerName = $bankInformation->getBankInformationFullName();
$bankAccount->UserId = $user->getMangoUserId();
$bankAccount->Type = 'IBAN';

$address = new \MangoPay\Address();
$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 BankInformation of User id : %s', $user->getId()));
}
$address->AddressLine1 = $userAddress;
$address->AddressLine2 = $bankInformation->getBankInformationAdditionalStreetAddress();
$address->City = $city;
$address->Country = $bankInformation->getBankInformationCountry();
$address->PostalCode = $postalCode;
$bankAccount->OwnerAddress = $address;

if ($bankInformation->getIban() !== $bankAccount->Details->IBAN) {
$iban = $bankInformation->getIban();
if ($this->mangopaySandbox) {
$iban = "FR7611808009101234567890147";
}
$bankAccount->Details->IBAN = $iban;
}

$bankAccount = $this->mangopayHelper->Users->UpdateBankAccount($mangoUser->Id, $bankAccount);

return $bankAccount;
}
}
2 changes: 1 addition & 1 deletion Helper/KYCHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function createDocument(File $file)
{
$page = new KycPage();

if (false === $file = @file_get_contents($value->getPathname(), FILE_BINARY)) {
if (false === $file = @file_get_contents($file->getPathname(), FILE_BINARY)) {
throw new TransformationFailedException(sprintf('Unable to read the "%s" file', $value->getPathname()));
}

Expand Down
75 changes: 75 additions & 0 deletions Helper/MandateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Troopers\MangopayBundle\Helper;

use Doctrine\ORM\EntityManager;
use MangoPay\Mandate;
use MangoPay\Sorting;
use MangoPay\Tests\BankAccounts;
use MangoPay\Wallet;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Troopers\MangopayBundle\Entity\BankInformationInterface;
use Troopers\MangopayBundle\Entity\LegalUserInterface;
use Troopers\MangopayBundle\Entity\NaturalUserInterface;
use Troopers\MangopayBundle\Entity\UserInterface;
use Troopers\MangopayBundle\Event\WalletEvent;
use Troopers\MangopayBundle\TroopersMangopayEvents;
use Troopers\MangopayBundle\Helper\User\UserHelper;

class MandateHelper
{
private $mangopayHelper;

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

/**
* @param BankInformationInterface $user
*
* @return Mandate
*/
public function findOrCreateMandate(BankInformationInterface $bankInformation, $returnUrl = 'http://example.com/')
{
$bankInformationId = $bankInformation->getMangoBankAccountId();
$userId = $bankInformation->getUser()->getMangoUserId();
$pagination = null;
$mandates = $this->mangopayHelper->Users->GetMandatesForBankAccount($userId, $bankInformationId, $pagination, (new Sorting())->AddField('CreationDate', 'DESC'));

if (empty($mandates)) {
$mandate = $this->createMandateForBankInformation($bankInformation, $returnUrl);
// else, create a new mango user
} else {
$mandate = array_shift($mandates);
}

return $mandate;
}

public function createMandateForBankInformation(BankInformationInterface $bankInformation, $returnUrl = 'http://example.com/')
{
$bankInformationId = $bankInformation->getMangoBankAccountId();
$userId = $bankInformation->getUser()->getMangoUserId();

$mandate = new Mandate();
$mandate->BankAccountId = $bankInformationId;
$user = $bankInformation->getUser();
if ($user instanceof LegalUserInterface) {
$culture = $user->getLegalRepresentativeNationality();
} else {
$culture = $user->getNationality();
}
$mandate->Culture = $culture;
$mandate->ReturnURL = $returnUrl;
$mangoMandate = $this->mangopayHelper->Mandates->Create($mandate, md5(json_encode([
'bankInformation' => $bankInformationId,
'user' => $userId,
])));

$bankInformation->setMangoMandateId($mandate->Id);
$bankInformation->setMangoMandateUrl($mandate->RedirectURL);

return $mangoMandate;
}
}
73 changes: 73 additions & 0 deletions Helper/PaymentDirectDebitHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Troopers\MangopayBundle\Helper;

use MangoPay\Money;
use MangoPay\PayIn;
use MangoPay\PayInExecutionDetailsDirect;
use MangoPay\PayInPaymentDetailsDirectDebit;
use MangoPay\PayInPaymentDetailsDirectDebitDirect;
use MangoPay\PayInPaymentType;
use Troopers\MangopayBundle\Entity\UserInterface;

class PaymentDirectDebitHelper
{
protected $mangopayHelper;
/**
* @var MandateHelper
*/
protected $mandateHelper;

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

/**
* @param UserInterface $userDebited
* @param UserInterface $userCredited
* @param int $amount
* @param int $fees
* @param string|null $statementDescriptor
* @return PayIn
*/
public function createDirectDebitPayin(UserInterface $userDebited, UserInterface $userCredited, $amount, $fees, $statementDescriptor = null)
{
$mandate = $this->mandateHelper->findOrCreateMandate($userDebited->getBankInformation());

$payin = new PayIn();
$payin->AuthorId = $userDebited->getMangoUserId();
$payin->CreditedUserId = $userCredited->getMangoUserId();
$payin->CreditedWalletId = $userCredited->getMangoWalletId();

$debitedFunds = new Money();
$debitedFunds->Currency = 'EUR';
$debitedFunds->Amount = $amount;

$mangoFees = new Money();
$mangoFees->Currency = 'EUR';
$mangoFees->Amount = $fees;

$payin->DebitedFunds = $debitedFunds;
$payin->Fees = $mangoFees;

$payin->PaymentDetails = new PayInPaymentDetailsDirectDebit();
$payin->PaymentDetails->MandateId = $mandate->Id;
$payin->PaymentDetails->StatementDescriptor = $statementDescriptor;

$payin->ExecutionDetails = new PayInExecutionDetailsDirect();

return $this->mangopayHelper->PayIns->Create($payin);
}

public function getPayin($payinId)
{
return $this->mangopayHelper->PayIns->Get($payinId);
}

public function getPayout($payoutId)
{
return $this->mangopayHelper->PayOuts->Get($payoutId);
}
}
Loading

0 comments on commit 3f4edd9

Please sign in to comment.