Drupal Marketplace with MangoPay

Drupal Marketplace with MangoPay

In this post, I share my experience to create a marketplace with Drupal Commerce and MangoPay.

Mangopay Workflow

We should understand the following MangoPay workflow for any marketplaces business, we can read for more details in this document.

Mnagopay

We should create a mangopay account or sandbox, we need the client ID and the secret key.

Requirements Drupal

Installation Drupal commerce, more information https://docs.drupalcommerce.org/commerce2/developer-guide/install-update/installation

We need to install the library https://github.com/Mangopay/mangopay2-php-sdk => composer require mangopay/php-sdk-v2:^2.3

Mangopay API

// create object to manage MangoPay API
$api = new \MangoPay\MangoPayApi();
// use test client credentails (REPLACE IT BY YOUR CLIENT ONES!)
$api->Config->ClientId = $configuration['client_id'];
$api->Config->ClientPassword = $configuration['client_secret'];
$api->Config->TemporaryFolder = $_SERVER["DOCUMENT_ROOT"].$configuration['temporary_folder'];
if($configuration['mode'] == 'test'){
    $api->Config->BaseUrl = 'https://api.sandbox.mangopay.com';
}else{
    $api->Config->BaseUrl = 'https://api.mangopay.com';
}

Create a MangoPay account by store and customer

 1 - Create a Natural user and Wallet Mangopay, we can use the following code with the store form and the infrmation step of checkout.

// CREATE NATURAL USER
$naturalUser = new \MangoPay\UserNatural();
$naturalUser->Email = $parameters['email'];
$naturalUser->FirstName = $parameters['first_name'];
$naturalUser->LastName = $parameters['last_name'];
$naturalUser->Birthday = $parameters['birthday'];
$naturalUser->Tag = $parameters['tag'];
if(!empty($parameters['billing_address']['line1'])){
    $naturalUser->Address = new \MangoPay\Address();
    $naturalUser->Address->AddressLine1 = $parameters['billing_address']['line1'];
    if(!empty($parameters['billing_address']['line2'])){
        $naturalUser->Address->AddressLine2 = $parameters['billing_address']['line2'];
    }
    $naturalUser->Address->City = $parameters['billing_address']['city'];
    $naturalUser->Address->PostalCode = $parameters['billing_address']['postal_code'];
    $naturalUser->Address->Country = $parameters['billing_address']['country_code'];
}
$naturalUser->Nationality = $parameters['billing_address']['country_code'];
$naturalUser->CountryOfResidence = $parameters['billing_address']['country_code'];
$naturalUserResult = $api->Users->Create($naturalUser);

// CREATE WALLET
$Wallet = new \MangoPay\Wallet();
$Wallet->Owners = array($id_client);
$Wallet->Description =  $id_client;
$Wallet->Currency = $currency;
$result = $api->Wallets->Create($Wallet);

2- Add IBAN & BIC field to create the bank account by store

//create account object mangopay
$bank_account = new \MangoPay\BankAccount();
$bank_account->Type = "IBAN";
//create detail account bank mangopay
$bank_account->Details = new \MangoPay\BankAccountDetailsIBAN();
$bank_account->Details->IBAN = $parameters['iban'];
$bank_account->Details->BIC = $parameters['bic'];
//create address bank account
$bank_account->OwnerAddress = new \MangoPay\Address();
$bank_account->OwnerAddress->AddressLine1 = $parameters['billing_address']['line1'];
if(!empty($parameters['billing_address']['line2'])) {
  $bank_account->OwnerAddress->AddressLine2 = $parameters['billing_address']['line2'];
}
$bank_account->OwnerAddress->City = $parameters['billing_address']['city'];
$bank_account->OwnerAddress->Region = $parameters['billing_address']['city'];
$bank_account->OwnerAddress->PostalCode = $parameters['billing_address']['postal_code'];
$bank_account->OwnerAddress->Country = $parameters['billing_address']['country_code'];
$bank_account->OwnerName = $parameters['name'];
//create account bank mangopay
$created_account = $api->Users->CreateBankAccount($store_Id, $bank_account);

Checkout :  step payment

After creating the MangoPay account, the customer can pay the order by the PayIn action and the amount hold into its wallet .We add the following code in createPayment function of gateway payment methode.

$PayIn = new \MangoPay\PayIn();
$PayIn->CreditedWalletId = $wallet;
$PayIn->AuthorId = $client;
$PayIn->CreditedUserId = $client;
$PayIn->PaymentType = $type_payment;
$PayIn->Tag = $tag;
$PayIn->PaymentDetails = new \MangoPay\PayInPaymentDetailsCard();
$PayIn->PaymentDetails->CardType = type_card;
$PayIn->PaymentDetails->CardId = $card->Id;
$PayIn->DebitedFunds = new \MangoPay\Money();
$PayIn->DebitedFunds->Currency = $parameters['currency'];
$PayIn->DebitedFunds->Amount = $parameters['amount'];
$PayIn->Fees = new \MangoPay\Money();
$PayIn->Fees->Currency = $parameters['currency'];
$PayIn->Fees->Amount = $parameters['fees_amount'];
$PayIn->CultureCode = $lang;
$PayIn->ExecutionType = "DIRECT";
$PayIn->ExecutionDetails = new \MangoPay\PayInExecutionDetailsDirect();
$PayIn->ExecutionDetails->Culture = $lang;
$PayIn->ExecutionDetails->ReturnURL = $return_url;
$PayIn->ExecutionDetails->SecureModeReturnURL = $return_url;
$result = $api->PayIns->Create($PayIn);

Transfer to the store's wallet

Now, we have to transfer the funds from the customer’s wallet to the store’s wallet, we can add a cron to execute this step.

$transfer = new \MangoPay\Transfer();
$transfer->Tag = $parameters['tag'];
$transfer->AuthorId = $parameters['user_id'];
$transfer->CreditedUserId = $parameters['user_id'];
$transfer->DebitedFunds = new \MangoPay\Money();
$transfer->DebitedFunds->Currency = $parameters['currency'];
$transfer->DebitedFunds->Amount = $parameters['amount'];
$transfer->Fees = new \MangoPay\Money();
$transfer->Fees->Currency = $parameters['currency'];
$transfer->Fees->Amount = $parameters['fees_amount'];
$transfer->DebitedWalletId = $parameters['debitedWallet'];
$transfer->CreditedWalletId = $parameters['creditedWallet'];
$api->Transfers->Create($transfer);

PAYOUT Bank

Now, the store can cash-out the collected funds to her/his bank account. we can create view of all orders and add a system action to execute the payout by order.

$payOut = new \MangoPay\PayOut();
$payOut->Tag = $parameters['tag'];
$payOut->AuthorId = $parameters['user_id'];
$payOut->CreditedUserId = $parameters['user_id'];
$payOut->DebitedFunds = new \MangoPay\Money();
$payOut->DebitedFunds->Currency = $parameters['currency'];
$payOut->DebitedFunds->Amount = $parameters['amount'];
$payOut->Fees = new \MangoPay\Money();
$payOut->Fees->Currency = $parameters['currency'];
$payOut->Fees->Amount = $parameters['fees_amount'];
$payOut->DebitedWalletId = $parameters['id_wallet'];
$payOut->MeanOfPaymentDetails = new \MangoPay\PayOutPaymentDetailsBankWire();
$payOut->MeanOfPaymentDetails->BankAccountId = $parameters['bank_id'];
$payOut->MeanOfPaymentDetails->BankWireRef = $parameters['wire_ref'];
$api->PayOuts->Create($payOut);

Finally, we have integrate the workflow of Mangopay for marketplace with drupal commerce.

 

Comments