Skip to content

Instantly share code, notes, and snippets.

@olivier1208
Created August 7, 2019 13:02
Show Gist options
  • Save olivier1208/35d7c5cb04a826195460fb9366fe7348 to your computer and use it in GitHub Desktop.
Save olivier1208/35d7c5cb04a826195460fb9366fe7348 to your computer and use it in GitHub Desktop.
MangoPay Implementation for Symfony
<?php
class MangoPayClient
{
/**
* @var MangoPay\MangoPayApi
*/
private $mangoPayApi;
/**
* @var Container
*/
private $container;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* MangoPayClient constructor.
* @param ContainerInterface $container
* @param EntityManagerInterface $em
*/
public function __construct(ContainerInterface $container, EntityManagerInterface $em)
{
$this->container = $container;
$this->em = $em;
$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->Config->ClientId = $this->container->getParameter('mangopay_clientId');
$this->mangoPayApi->Config->ClientPassword = $this->container->getParameter('mangopay_clientPassword');
$this->mangoPayApi->Config->TemporaryFolder = '../var/logs';
$this->mangoPayApi->Config->BaseUrl = $this->container->getParameter('mangopay_baseUrl');
}
/**
* @param $firstName
* @param $lastName
* @param $birthday
* @param $nationality
* @param $email
* @return UserLegal|MangoPay\UserNatural
* @throws MangoPay\Libraries\Exception
*/
public function createNaturalUser($firstName, $lastName, $birthday, $nationality, $email)
{
$mangoUser = new MangoPay\UserNatural();
$mangoUser->FirstName = $firstName;
$mangoUser->LastName = $lastName;
$mangoUser->Birthday = $birthday;
$mangoUser->Nationality = $nationality;
$mangoUser->CountryOfResidence = "FR";
$mangoUser->Email = $email;
//Send the request
$mangoUser = $this->mangoPayApi->Users->Create($mangoUser);
return $mangoUser;
}
/**
* @param $userID
* @return bool|UserLegal|MangoPay\UserNatural
*/
public function viewUser($userID)
{
try {
if ($userID !== null) {
$userObject = $this->mangoPayApi->Users->Get($userID);
}
} catch (MangoPay\Libraries\Exception $e) {
$errorHandler = new MangoPayErrorHandler($this->em);
$errorHandler->saveResponseErrorsInDb($e);
}
if (isset($userObject)) {
return $userObject;
} else {
return false;
}
}
/**
* @return array
*/
public function listAllUsers()
{
return $this->mangoPayApi->Users->GetAll();
}
/**
* @param $owners array
* @param $description string
* @param $currency
* @return Wallet
*/
public function createWalletForNaturalUser($owners, $description, $currency)
{
$Wallet = new Wallet();
$Wallet->Owners = $owners;
$Wallet->Description = $description;
$Wallet->Currency = $currency;
$result = $this->mangoPayApi->Wallets->Create($Wallet);
return $result;
}
/**
* @param $userId
* @return bool|Wallet[]
*/
public function listAllWalletsByUser($userId)
{
if ($this->mangoPayApi->Users !== null) {
return $this->mangoPayApi->Users->GetWallets($userId);
} else {
return false;
}
}
/**
* @param $userId
* @return array
*/
public function listBankAccountsByUser($userId)
{
return $this->mangoPayApi->Users->GetBankAccounts($userId);
}
/**
* @param $name
* @param $personType
* @param $email
* @param $legalRepresentativeFirstName
* @param $legalRepresentativeLastName
* @param $legalRepresentativeBirthday
* @return UserLegal|MangoPay\UserNatural
* @throws MangoPay\Libraries\Exception
*/
public function createLegalUser($name, $personType, $email, $legalRepresentativeFirstName, $legalRepresentativeLastName, $legalRepresentativeBirthday)
{
$User = new UserLegal();
$User->Name = $name;
$User->LegalPersonType = $personType;
$User->Email = $email;
$User->LegalRepresentativeFirstName = $legalRepresentativeFirstName;
$User->LegalRepresentativeLastName = $legalRepresentativeLastName;
$User->LegalRepresentativeBirthday = $legalRepresentativeBirthday;
$User->LegalRepresentativeNationality = "FR";
$User->LegalRepresentativeCountryOfResidence = "FR";
$result = $this->mangoPayApi->Users->Create($User);
return $result;
}
/**
* @param $tag
* @param $owners
* @param $description
* @param $currency
* @return Wallet
*/
public function createWalletForLegalUser($tag, $owners, $description, $currency)
{
$Wallet = new Wallet();
$Wallet->Tag = $tag;
$Wallet->Owners = $owners;
$Wallet->Description = $description;
$Wallet->Currency = $currency;
$Result = $this->mangoPayApi->Wallets->Create($Wallet);
return $Result;
}
/**
* @param $walletId
* @param $authorId
* @param $paymentType
* @param $paymentCardType
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @param $returnUrl
* @return PayIn
*/
public function createPayInCardWeb($walletId, $authorId, $paymentType, $paymentCardType, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount, $returnUrl)
{
$PayIn = new PayIn();
$PayIn->CreditedWalletId = $walletId;
$PayIn->AuthorId = $authorId;
$PayIn->PaymentType = $paymentType;
$PayIn->PaymentDetails = new PayInPaymentDetailsCard();
$PayIn->PaymentDetails->CardType = $paymentCardType;
$PayIn->DebitedFunds = new Money();
$PayIn->DebitedFunds->Currency = $debitedFundsCurrency;
$PayIn->DebitedFunds->Amount = $debitedFundsAmount;
$PayIn->Fees = new Money();
$PayIn->Fees->Currency = $feesCurrency;
$PayIn->Fees->Amount = $feesAmount;
$PayIn->ExecutionType = "WEB";
$PayIn->ExecutionDetails = new PayInExecutionDetailsWeb();
$PayIn->ExecutionDetails->ReturnURL = $returnUrl;
$PayIn->ExecutionDetails->Culture = "FR";
$Result = $this->mangoPayApi->PayIns->Create($PayIn);
return $Result;
}
/**
* @param $payInId
* @return PayIn
*/
public function reviewPayInCardWeb($payInId)
{
$result = $this->mangoPayApi->PayIns->Get($payInId);
return $result;
}
/**
* @param $userNaturalId
* @param $currency
* @return CardRegistration
*/
public function createCardRegistration($userNaturalId, $currency)
{
$cardRegister = new CardRegistration();
$cardRegister->UserId = $userNaturalId;
$cardRegister->Currency = $currency;
$result = $this->mangoPayApi->CardRegistrations->Create($cardRegister);
return $result;
}
/**
* @param $registrationData
* @param $cardRegistration
* @return CardRegistration
*/
public function finishCardRegistration($registrationData, $cardRegistration)
{
$cardRegisterPut = $this->mangoPayApi->CardRegistrations->Get($cardRegistration);
$cardRegisterPut->RegistrationData = $registrationData;
$result = $this->mangoPayApi->CardRegistrations->Update($cardRegisterPut);
return $result;
}
/**
* @param $CardRegistrationId
* @return CardRegistration
*/
public function viewCardRegistration($CardRegistrationId)
{
$CardRegistration = $this->mangoPayApi->CardRegistrations->Get($CardRegistrationId);
return $CardRegistration;
}
/**
* @param $CardId
* @return MangoPay\Card
*/
public function viewCard($CardId)
{
$Card = $this->mangoPayApi->Cards->Get($CardId);
return $Card;
}
/**
* @param $creditedWalletId
* @param $authorId
* @param $paymentType
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @param $secureModeReturnUrl
* @param $cardId
* @return PayIn
*/
public function doPayInCardDirect($creditedWalletId, $authorId, $paymentType, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount, $secureModeReturnUrl, $cardId)
{
$PayIn = new PayIn();
$PayIn->CreditedWalletId = $creditedWalletId;
$PayIn->AuthorId = $authorId;
$PayIn->PaymentType = $paymentType;
$PayIn->PaymentDetails = new PayInPaymentDetailsCard();
$PayIn->DebitedFunds = new Money();
$PayIn->DebitedFunds->Currency = $debitedFundsCurrency;
$PayIn->DebitedFunds->Amount = $debitedFundsAmount;
$PayIn->Fees = new Money();
$PayIn->Fees->Currency = $feesCurrency;
$PayIn->Fees->Amount = $feesAmount;
$PayIn->ExecutionType = "DIRECT";
$PayIn->ExecutionDetails = new PayInExecutionDetailsDirect();
$PayIn->ExecutionDetails->SecureModeReturnURL = $secureModeReturnUrl;
$PayIn->ExecutionDetails->CardId = $cardId;
$result = $this->mangoPayApi->PayIns->Create($PayIn);
return $result;
}
/**
* @param $authorId
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $cardId
* @param $secureModeReturnUrl
* @return CardPreAuthorization
*/
public function setUpPreAuth($authorId, $debitedFundsCurrency, $debitedFundsAmount, $cardId, $secureModeReturnUrl)
{
$CardPreAuthorization = new CardPreAuthorization();
$CardPreAuthorization->AuthorId = $authorId;
$CardPreAuthorization->DebitedFunds = new Money();
$CardPreAuthorization->DebitedFunds->Currency = $debitedFundsCurrency;
$CardPreAuthorization->DebitedFunds->Amount = $debitedFundsAmount;
$CardPreAuthorization->SecureMode = "DEFAULT";
$CardPreAuthorization->CardId = $cardId;
$CardPreAuthorization->SecureModeReturnURL = $secureModeReturnUrl;
$result = $this->mangoPayApi->CardPreAuthorizations->Create($CardPreAuthorization);
return $result;
}
/**
* @param $creditedWalletId
* @param $authorId
* @param $paymentType
* @param $preAuthId
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @return PayIn
*/
public function doPayInPreAuth($creditedWalletId, $authorId, $paymentType, $preAuthId, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount)
{
$PayIn = new PayIn();
$PayIn->CreditedWalletId = $creditedWalletId;
$PayIn->AuthorId = $authorId;
$PayIn->PaymentType = $paymentType;
$PayIn->PaymentDetails = new PayInPaymentDetailsPreAuthorized();
$PayIn->PaymentDetails->PreauthorizationId = $preAuthId;
$PayIn->DebitedFunds = new Money();
$PayIn->DebitedFunds->Currency = $debitedFundsCurrency;
$PayIn->DebitedFunds->Amount = $debitedFundsAmount;
$PayIn->Fees = new Money();
$PayIn->Fees->Currency = $feesCurrency;
$PayIn->Fees->Amount = $feesAmount;
$PayIn->ExecutionType = "DIRECT";
$PayIn->ExecutionDetails = new PayInExecutionDetailsDirect();
$result = $this->mangoPayApi->PayIns->Create($PayIn);
return $result;
}
/**
* @param $payInId
* @param $authorId
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @return Refund
*/
public function doPayInRefund($payInId, $authorId, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount)
{
$PayInId = $payInId;
$Refund = new Refund();
$Refund->AuthorId = $authorId;
$Refund->DebitedFunds = new Money();
$Refund->DebitedFunds->Currency = $debitedFundsCurrency;
$Refund->DebitedFunds->Amount = $debitedFundsAmount;
$Refund->Fees = new Money();
$Refund->Fees->Currency = $feesCurrency;
$Refund->Fees->Amount = $feesAmount;
$result = $this->mangoPayApi->PayIns->CreateRefund($PayInId, $Refund);
return $result;
}
/**
* @param $authorId
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @param $debitedWalletId
* @param $creditedWalletId
* @return Transfer
*/
public function doTransfer($authorId, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount, $debitedWalletId, $creditedWalletId)
{
$Transfer = new Transfer();
$Transfer->AuthorId = $authorId;
$Transfer->DebitedFunds = new Money();
$Transfer->DebitedFunds->Currency = $debitedFundsCurrency;
$Transfer->DebitedFunds->Amount = $debitedFundsAmount;
$Transfer->Fees = new Money();
$Transfer->Fees->Currency = $feesCurrency;
$Transfer->Fees->Amount = $feesAmount;
$Transfer->DebitedWalletId = $debitedWalletId;
$Transfer->CreditedWalletId = $creditedWalletId;
$result = $this->mangoPayApi->Transfers->Create($Transfer);
return $result;
}
/**
* @param $userId
* @param $iban
* @param $bic
* @param $ownerName
* @param $ownerAddressLine1
* @param $ownerAddressCity
* @param $ownerAddressCountry
* @param $ownerAddressPostalCode
* @return BankAccount
*/
public function createBankAccount($userId, $iban, $bic, $ownerName, $ownerAddressLine1, $ownerAddressPostalCode, $ownerAddressCity, $ownerAddressCountry)
{
$ownerAddress = new MangoPay\Address();
$ownerAddress->AddressLine1 = $ownerAddressLine1;
$ownerAddress->PostalCode = $ownerAddressPostalCode;
$ownerAddress->City = $ownerAddressCity;
$ownerAddress->Country = $ownerAddressCountry;
$UserId = $userId;
$BankAccount = new BankAccount();
$BankAccount->Type = "IBAN";
$BankAccount->Details = new BankAccountDetailsIBAN();
$BankAccount->Details->IBAN = $iban;
$BankAccount->Details->BIC = $bic;
$BankAccount->OwnerName = $ownerName;
$BankAccount->OwnerAddress = $ownerAddress;
$result = $this->mangoPayApi->Users->CreateBankAccount($UserId, $BankAccount);
return $result;
}
/**
* @param $authorId
* @param $debitedWalletID
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @param $bankAccountId
* @return PayOut
*/
public function doPayOut($authorId, $debitedWalletID, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount, $bankAccountId)
{
$PayOut = new PayOut();
$PayOut->AuthorId = $authorId;
$PayOut->DebitedWalletId = $debitedWalletID;
$PayOut->DebitedFunds = new Money();
$PayOut->DebitedFunds->Currency = $debitedFundsCurrency;
$PayOut->DebitedFunds->Amount = $debitedFundsAmount;
$PayOut->Fees = new Money();
$PayOut->Fees->Currency = $feesCurrency;
$PayOut->Fees->Amount = $feesAmount;
$PayOut->PaymentType = "BANK_WIRE";
$PayOut->MeanOfPaymentDetails = new PayOutPaymentDetailsBankWire();
$PayOut->MeanOfPaymentDetails->BankAccountId = $bankAccountId;
$result = $this->mangoPayApi->PayOuts->Create($PayOut);
return $result;
}
/**
* @param $kycDocumentType
* @param $userNatural
* @param $filePath
* @param $kycDocumentStatus
* @return array
* @throws MangoPay\Libraries\Exception
*/
public function submitKycDocument($kycDocumentType, $userNatural, $filePath, $kycDocumentStatus)
{
// create the doc
// KYCDocumentType: may be :
// IDENTITY_PROOF, REGISTRATION_PROOF, ARTICLES_OF_ASSOCIATION, SHAREHOLDER_DECLARATION, ADDRESS_PROOF( not used)
$KycDocument = new KycDocument();
$KycDocument->Type = $kycDocumentType;
$result = $this->mangoPayApi->Users->CreateKycDocument($userNatural, $KycDocument);
$KycDocumentId = $result->Id;
//add a page to this doc
$this->mangoPayApi->Users->CreateKycPageFromFile($userNatural, $KycDocumentId, $filePath);
//submit the doc for validation
// Statuses may be : CREATED, VALIDATION_ASKED, VALIDATED, REFUSED
$KycDocument = new KycDocument();
$KycDocument->Id = $KycDocumentId;
$KycDocument->Status = $kycDocumentStatus;
$result3 = $this->mangoPayApi->Users->UpdateKycDocument($userNatural, $KycDocument);
return [
// $result,
// $result2,
$result3
];
}
/**
* @param $UserId
* @param $KycDocumentId
* @return KycDocument
*/
public function viewKycDocument($UserId, $KycDocumentId)
{
$view = $this->mangoPayApi->Users->GetKycDocument($UserId, $KycDocumentId);
return $view;
}
/**
* @param $UserId
* @return array
*/
public function listKycDocumentsForUser($UserId)
{
$pagination = new MangoPay\Pagination();
$pagination->ItemsPerPage = 100;
$pagination->TotalItems = 100;
$pagination->TotalPages = 100;
$list = $this->mangoPayApi->Users->GetKycDocuments($UserId, $pagination);
return $list;
}
/**
* @param $authorId
* @param $debitedFundsCurrency
* @param $debitedFundsAmount
* @param $feesCurrency
* @param $feesAmount
* @return Refund
*/
public function doTransferRefund($authorId, $debitedFundsCurrency, $debitedFundsAmount, $feesCurrency, $feesAmount)
{
$TransferId = $_SESSION["MangoPayDemo"]["Transfer"];
$Refund = new Refund();
$Refund->AuthorId = $authorId;
$Refund->DebitedFunds = new Money();
$Refund->DebitedFunds->Currency = $debitedFundsCurrency;
$Refund->DebitedFunds->Amount = $debitedFundsAmount;//Note that partial Refunds for Transfers are not possible
$Refund->Fees = new Money();
$Refund->Fees->Currency = $feesCurrency;
$Refund->Fees->Amount = $feesAmount;
$result = $this->mangoPayApi->Transfers->CreateRefund($TransferId, $Refund);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment