-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Troopers/feature/mandate
Feature/mandate
- Loading branch information
Showing
15 changed files
with
301 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.