The most simple and fast way to integrate the Moip into your PHP application.
Current SDK's state
Statistics
Versions
Index
- Installing
- Configuring the authentication
- Application examples
- PHP >= 5.5
- rmccue/requests >= 1.0
- phpunit/phpunit ~ 4.0
Run on your shell:
composer require moip/moip-sdk-php
require 'vendor/autoload.php';
use Moip\Moip;
use Moip\Auth\BasicAuth;
$token = '01010101010101010101010101010101';
$key = 'ABABABABABABABABABABABABABABABABABABABAB';
$moip = new Moip(new BasicAuth($token, $key), Moip::ENDPOINT_SANDBOX);
require 'vendor/autoload.php';
use Moip\Moip;
use Moip\Auth\OAuth;
$access_token = '33031e2aad484051b89030487e59d133_v2';
$moip = new Moip(new OAuth($access_token), Moip::ENDPOINT_SANDBOX);
The Customer is a service's user or a buyer of a virtual store.
$customer = $moip->customers()->setOwnId(uniqid())
->setFullname('Fulano de Tal')
->setEmail('[email protected]')
->setBirthDate('1988-12-30')
->setTaxDocument('22222222222')
->setPhone(11, 66778899)
->addAddress('BILLING',
'Rua de teste', 123,
'Bairro', 'Sao Paulo', 'SP',
'01234567', 8)
->addAddress('SHIPPING',
'Rua de teste do SHIPPING', 123,
'Bairro do SHIPPING', 'Sao Paulo', 'SP',
'01234567', 8)
->create();
print_r($customer);
$customer = $moip->customers()->get('CUS-Q3BL0CAJ2G33');
print_r($customer);
This endpoint allows you to add one or more credit cards to a customer.
$customer = $moip->customers()->creditCard()
->setExpirationMonth('12')
->setExpirationYear(2018)
->setNumber('4012001037141112')
->setCVC('123')
->setFullName('Jose Portador da Silva')
->setBirthDate('1988-12-30')
->setTaxDocument('CPF', '33333333333')
->setPhone('55','11','66778899')
->create();
This endpoint allows you to delete a customer's saved credit card.
$moip->customers()->creditCard()->delete(CREDIT_CARD_ID);
The Order is the representation of a product or a provided service.
This endpoint allows you to create a Order containing the data of a product.
You can use the customer's ID (informed in the customer request's response), calling the method setCustomerId()
.
$order = $moip->orders()->setOwnId(uniqid())
->addItem("bicicleta 1", 1, "sku1", 10000)
->addItem("bicicleta 2", 1, "sku2", 11000)
->addItem("bicicleta 3", 1, "sku3", 12000)
->addItem("bicicleta 4", 1, "sku4", 13000)
->setShippingAmount(3000)
->setAddition(1000)
->setDiscount(5000)
->setCustomerId($customerId)
->create();
Or you can use the customer object, calling the method setCustomer()
.
$order = $moip->orders()->setOwnId(uniqid())
->addItem("bicicleta 1", 1, "sku1", 10000)
->addItem("bicicleta 2", 1, "sku2", 11000)
->addItem("bicicleta 3", 1, "sku3", 12000)
->addItem("bicicleta 4", 1, "sku4", 13000)
->setShippingAmount(3000)
->setAddition(1000)
->setDiscount(5000)
->setCustomer($customer)
->create();
Calling this method, you can get the details of an specific Order.
$order = $moip->orders()->get('ORD-KZCH1S1ORAH23');
Another way is listing all created orders, with or without filters and search parameters.
$orders = $this->moip->orders()->getList();
$filters = new Filters();
$filters->greaterThanOrEqual(OrdersList::CREATED_AT, '2017-08-17');
$filters->in(OrdersList::PAYMENT_METHOD, ['BOLETO', 'DEBIT_CARD']);
$filters->lessThan(OrdersList::VALUE, 100000);
$orders = $this->moip->orders()->getList(null, $filters);
$orders = $this->moip->orders()->getList(new Pagination(10,0));
$orders = $this->moip->orders()->getList(null, null, 'josé silva');
You can use all search parameters in the same request.
$filters = new Filters();
$filters->greaterThanOrEqual(OrdersList::CREATED_AT, '2017-08-17');
$filters->lessThan(OrdersList::VALUE, 100000);
$orders = $this->moip->orders()->getList(new Pagination(10,0), $filters, 'josé silva');
The Payment is a financial transaction between the Customer and the Receiver by credit card, boleto or another payment method.
There are three payment methods:
Before requesting the payment by credit card it is necessary to set the holder (the owner of the credit card) informations. This step is very important, because the holder may be the customer or someone else, a third party.
$holder = $moip->holders()->setFullname('Jose Silva')
->setBirthDate("1990-10-10")
->setTaxDocument('22222222222', 'CPF')
->setPhone(11, 66778899, 55)
->setAddress('BILLING', 'Avenida Faria Lima', '2927', 'Itaim', 'Sao Paulo', 'SP', '01234000', 'Apt 101');
To pay by credit card hash, it is necessary to encrypt the card's informations.
$hash = 'i1naupwpTLrCSXDnigLLTlOgtm+xBWo6iX54V/hSyfBeFv3rvqa1VyQ8/pqWB2JRQX2GhzfGppXFPCmd/zcmMyDSpdnf1GxHQHmVemxu4AZeNxs+TUAbFWsqEWBa6s95N+O4CsErzemYZHDhsjEgJDe17EX9MqgbN3RFzRmZpJqRvqKXw9abze8hZfEuUJjC6ysnKOYkzDBEyQibvGJjCv3T/0Lz9zFruSrWBw+NxWXNZjXSY0KF8MKmW2Gx1XX1znt7K9bYNfhA/QO+oD+v42hxIeyzneeRcOJ/EXLEmWUsHDokevOkBeyeN4nfnET/BatcDmv8dpGXrTPEoxmmGQ==';
If you don't know how to generate the hash, click here to use the Moip's Credit Card Cryptography.
With the hash and the holder, is possible create the payment.
$payment = $order->payments()
->setCreditCardHash($hash, $holder)
->setInstallmentCount(3)
->setStatementDescriptor('teste de pag')
->execute();
Another way to create a payment is to use the credit card data, without encryption.
Warning! This method require PCI compliance, (check the Moip documentation).
$payment = $order->payments()->setCreditCard(12, 21, '4073020000000002', '123', $holder)
->execute();
To create a boleto payment it is necessary to send just three parameters:
- the
logo_uri
(String) - URL containing the site logo (optional); - the
expiration_date
(String) - date of boleto expiration; - the
instruction_lines
(String array {3}) - boleto instructions (optional).
$logo_uri = 'https://cdn.moip.com.br/wp-content/uploads/2016/05/02163352/logo-moip.png';
$expiration_date = new DateTime();
$instruction_lines = ['INSTRUÇÃO 1', 'INSTRUÇÃO 2', 'INSTRUÇÃO 3'];
$payment = $order->payments()
->setBoleto($expiration_date, $logo_uri, $instruction_lines)
->execute();
This method requires three parameters too:
- the
bank_number
(String) - number of the bank (actually, the only possible value is 341, referring to Itaú Bank); - the
return_uri
(String) - URL to which the user will be redirect; - the
expiration_date
(DateTime) - the date of online bank debit expiration.
$bank_number = '341';
$return_uri = 'https://moip.com.br';
$expiration_date = new DateTime();
$payment = $order->payments()
->setOnlineBankDebit($bank_number, $expiration_date, $return_uri)
->execute();
$payment = $moip->payments()->get('PAYMENT-ID');
The new APIs allow you to control the flow of pre-authorization and capture a credit card transaction. This way, instead of the default configuration, where Moip captures the transaction after the anti-fraud analysis (Authorized status), you can control when the capture will be made. The deadline to capture is 5 days after the payment pre-authorization.
The only addition you have to make to create a pre-authorized payment is to add the parameter delayCapture
.
$payment = $order->payments()
->setCreditCardHash($hash, $holder)
->setInstallmentCount(3)
->setStatementDescriptor("Minha Loja")
->setDelayCapture()
->execute();
$captured_payment = $payment->capture();
$payment = $payment->cancel();
The Refund is the devolution of a payment to the customer/payer. To make the refund request, you should have the Payment object or the Payment ID, related with payment that you want refund.
$refund = $payment->refunds()->creditCardFull();
$refund = $payment->refunds()->creditCardPartial(30000);
$type = 'CHECKING';
$bank_number = '001';
$agency_number = 4444444;
$agency_check_number = 2;
$account_number = 1234;
$account_check_number = 4;
$refund = $payment->refunds()
->bankAccountFull(
$type,
$bank_number,
$agency_number,
$agency_check_number,
$account_number,
$account_check_number,
$customer
);
$amount = 30000;
$type = 'SAVING';
$bank_number = '001';
$agency_number = 4444444;
$agency_check_number = 2;
$account_number = 1234;
$account_check_number = 4;
$refund = $payment->refunds()
->bankAccountPartial(
$amount,
$type,
$bank_number,
$agency_number,
$agency_check_number,
$account_number,
$account_check_number,
$customer
);
$refund = $payment->refunds()->get($refundId);
Moip Connect allows your application to be a Marketplace, an app to contract services, an e-commerce platform or any system to help people and enterprises receive payments. This process allows your application to access informations of the user's Moip Account, creating transactions, making transfers, visualizing the balance, etc. You'll register your application on Moip, connect with your user's Moip Account (in case they have one already) or create new accounts.
Thereby, you can:
- request payments on behalf your users;
- get user's Moip Account informations.
There are two ways of using Connect:
- get permission to connect with user's Moip Account using OAuth 2.0;
- create Transparent Account, managed by your own application, checkout the difference between Classical Account and Transparent Account here.
Check the API reference to know how create a Moip APP.
To offer a amazing on-boarding, the Marketplace should consider which experience it wants to give to their merchants.
Thinking this, Moip developed two options to help you give the best experience to your clients!
Before creating an account, you can check if the user already has a Moip Account. This can save you some time... This endpoing receives the CPF or CNPJ as a parameter.
$moip->accounts()->checkAccountExists('CPF');
It will return 200 if the user already has an account or 400 if the user hasn't an account.
The Classical Moip Account allows users to manage their transactions easily, having available a complete dashboard (with Moip's layout) where it is possible to track incomes and set the best options to their business.
If you don't want to provide a feature to create a Classical Moip Account on your application, you may redirect the user to Moip account creation.
$street = 'Rua de teste';
$number = 123;
$district = 'Bairro';
$city = 'Sao Paulo';
$state = 'SP';
$zip = '01234567';
$complement = 'Apt. 23';
$country = 'BRA';
$area_code = 11;
$phone_number = 66778899;
$country_code = 55;
$identity_document = '4737283560';
$issuer = 'SSP';
$issue_date = '2015-06-23';
$account = $moip->accounts()
->setName('Fulano')
->setLastName('De Tal')
->setEmail('[email protected]')
->setIdentityDocument($identity_document, $issuer, $issue_date)
->setBirthDate('1988-12-30')
->setTaxDocument('16262131000')
->setType('MERCHANT')
->setPhone($area_code, $phone_number, $country_code)
->addAlternativePhone(11, 66448899, 55)
->addAddress($street, $number, $district, $city, $state, $zip, $complement, $country)
->create();
$street = 'Rua de teste';
$number = 123;
$district = 'Bairro';
$city = 'Sao Paulo';
$state = 'SP';
$zip = '01234567';
$complement = 'Apt. 23';
$country = 'BRA';
$area_code = 11;
$phone_number = 66778899;
$country_code = 55;
$identity_document = '4737283560';
$issuer = 'SSP';
$issue_date = '2015-06-23';
$account = $moip->accounts()
->setName('Fulano')
->setLastName('De Tal')
->setEmail('[email protected]')
->setIdentityDocument($identity_document, $issuer, $issue_date)
->setBirthDate('1988-12-30')
->setTaxDocument('16262131000')
->setType('MERCHANT')
->setPhone($area_code, $phone_number, $country_code)
->addAlternativePhone(11, 66448899, 55)
->addAddress($street, $number, $district, $city, $state, $zip, $complement, $country)
->setCompanyName('Empresa Teste', 'Teste Empresa ME')
->setCompanyOpeningDate('2011-01-01')
->setCompanyPhone(11, 66558899, 55)
->setCompanyTaxDocument('69086878000198')
->setCompanyAddress('Rua de teste 2', 123, 'Bairro Teste', 'Sao Paulo', 'SP', '01234567', 'Apt. 23', 'BRA')
->setCompanyMainActivity('82.91-1/00', 'Atividades de cobranças e informações cadastrais')
->create();
But what if you don't want your sellers to know about the existence of Moip behind your marketplace?
It is simple, Moip can process your transactions in a transparent way. To attend this issue, we have what is called Moip Transparent Account.
WARNING! If you choose to use the transparent account, you should assume the responsibility of providing all information and resources your sellers need to manage their business, besides that you will assume full responsibility of support to your sellers.
$account = $moip->accounts()
->setName('Fulano')
->setLastName('De Tal')
->setEmail('[email protected]')
->setIdentityDocument('4737283560', 'SSP', '2015-06-23')
->setBirthDate('1988-12-30')
->setTaxDocument('16262131000')
->setType('MERCHANT')
->setTransparentAccount(true)
->setPhone(11, 66778899, 55)
->addAlternativePhone(11, 66448899, 55)
->addAddress('Rua de teste', 123, 'Bairro', 'Sao Paulo', 'SP', '01234567', 'Apt. 23', 'BRA')
->create();
$account = $moip->accounts()->get(ACCOUNT_ID);
The public key is used to encrypt the credit card data. It's available on Sandbox and Production (after the homologation) accounts. If you want to learn more about how to use the public key, check our documentation about Encryption (PT-BR).
IMPORTANT! The homologation is the process where we'll test your integration, certifying that all is correct. To learn more about this process, check our documentation (PT-BR).
$keys = $moip->keys()->get();
To request permissions, you'll need to call the method getAuthUrl
(responsible for generating the URLs), sending the below parameters:
parameters | type | example value |
---|---|---|
redirect_uri | String | http://seusite.com.br/callback.php |
client_id | String | APP-18JTHC3LOMT9 |
scope | boolean | true |
IMPORTANT! The
redirect_uri
must be the same as the one registered in the APP.
$redirect_uri = 'http://seusite.com.br/callback.php';
$client_id = 'APP-18JTHC3LOMT9';
$scope = true;
$connect = new Connect($redirect_uri, $client_id, $scope, Connect::ENDPOINT_SANDBOX);
$connect->setScope(Connect::RECEIVE_FUNDS)
->setScope(Connect::REFUND)
->setScope(Connect::MANAGE_ACCOUNT_INFO)
->setScope(Connect::RETRIEVE_FINANCIAL_INFO);
header('Location: '.$connect->getAuthUrl());
Possibles permissions scopes:
scopes | description |
---|---|
RECEIVE_FUNDS | permission to create and consult orders, payments, multiorders and multipayments; consult entries; get sales reconciliation files. |
REFUND | permission to create and consult refunds. |
MANAGE_ACCOUNT_INFO | permission to consult registered information of account. |
RETRIEVE_FINANCIAL_INFO | permission to consult the account balances and get sales reconciliation files. |
TRANSFER_FUNDS | permission to create transfers to a bank account and between Moip accounts. |
DEFINE_PREFERENCES | permission to notification preferences creation, modification and deletion. |
When the permission is granted, a code will be returned on the redirect URL endpoint. This code will allow you to generate the accessToken
. It's used to authenticate the requests involving the Moip account that granted the permission.
$redirect_uri = 'http://seusite.com.br/callback.php';
$client_id = 'APP-18JTHC3LOMT9';
$scope = true;
$connect = new Connect($redirect_uri, $client_id, $scope, Connect::ENDPOINT_SANDBOX);
$client_secret = '20f76456f6ec4874a1f38082d3139326';
$connect->setClientSecret($client_secret);
$code = 'f9053ca6e9853dd73f0bc4f332a5ce337b0bb0da';
$connect->setCode($code);
$auth = $connect->authorize();
IMPORTANT!
- To run this request correctly, the object
Connect
must be instantiated, sending the necessary parameters, like the above example.- The
redirect_uri
must be the same as registered in the APP. If there is any divergence, theaccessToken
will not be generated.
The Moip balance is the composition of current values available, unavailable (blocked) and futures, of a Moip Account.
This API uses
application/json;version=2.1
. For more information, check our API reference.
$balances = $moip->balances()->get();
The multiorder is a collection of orders. It's used to allow transactions with different sellers in the same shopping cart. When a multiorder is created with a single customer interaction, Moip generates multiple charges and associates each of them with the appropriate seller, simplifying the management of Marketplaces.
IMPORTANT! By definition, it's not possible refund a multiorder entirely at once, but you can refund order by order.
As multiorder is composed by an array of orders, its structure is very similar from a simple order structure.
$order = $moip->orders()->setOwnId(uniqid())
->addItem("bicicleta 1",1, "sku1", 10000)
->addItem("bicicleta 2",1, "sku2", 11000)
->addItem("bicicleta 3",1, "sku3", 12000)
->addItem("bicicleta 4",1, "sku4", 13000)
->setShippingAmount(3000)
->setAddition(1000)
->setDiscount(5000)
->setCustomer($customer)
->addReceiver('MPA-VB5OGTVPCI52', 'PRIMARY', NULL);
$order2 = $moip->orders()->setOwnId(uniqid())
->addItem("bicicleta 1",1, "sku1", 10000)
->addItem("bicicleta 2",1, "sku2", 11000)
->addItem("bicicleta 3",1, "sku3", 12000)
->setShippingAmount(3000)
->setAddition(1000)
->setDiscount(5000)
->setCustomer($customer)
->addReceiver('MPA-IFYRB1HBL73Z', 'PRIMARY', NULL);
$multiorder = $this->moip->multiorders()
->setOwnId(uniqid())
->addOrder($order)
->addOrder($order2)
->create();
$multiorder_id = 'ORD-KZCH1S1ORAH25';
$multiorder = $moip->multiorders()->get($multiorder_id);
print_r($multiorder);
The multipayment is a collection of payments associated with a multiorder. It's used in the implementation of the shopping cart/multiorder, when it is necessary charge for different sellers with only one checkout. When a multipayment is created Moip creates a payment for each order of a multiorder and does the auto-charge.
IMPORTANT! In cases of multipayment with credit card, multiple authorizations are generated, one for each payment, dividing the charges of a customer, to facilitate the management of the Marketplace or Platform.
In the same way that multiorder's structure is similar to order's structure, the multipayment is similar to payment.
$hash = 'i1naupwpTLrCSXDnigLLTlOgtm+xBWo6iX54V/hSyfBeFv3rvqa1VyQ8/pqWB2JRQX2GhzfGppXFPCmd/zcmMyDSpdnf1GxHQHmVemxu4AZeNxs+TUAbFWsqEWBa6s95N+O4CsErzemYZHDhsjEgJDe17EX9MqgbN3RFzRmZpJqRvqKXw9abze8hZfEuUJjC6ysnKOYkzDBEyQibvGJjCv3T/0Lz9zFruSrWBw+NxWXNZjXSY0KF8MKmW2Gx1XX1znt7K9bYNfhA/QO+oD+v42hxIeyzneeRcOJ/EXLEmWUsHDokevOkBeyeN4nfnET/BatcDmv8dpGXrTPEoxmmGQ==';
$payment = $multiorder->multipayments()
->setCreditCardHash($hash, $customer)
->setInstallmentCount(3)
->setStatementDescriptor('teste de pag')
->execute();
print_r($payment);
$payment = $moip->payments()->get('MULTIPAYMENT-ID');
print_r($payment);
The bank account is the bank address of a Moip Account.
$bankAccount = $moip->bankaccount()
->setBankNumber('237')
->setAgencyNumber('12345')
->setAgencyCheckNumber('0')
->setAccountNumber('12345678')
->setAccountCheckNumber('7')
->setType('CHECKING')
->setHolder('Demo Moip', '622.134.533-22', 'CPF')
->create($moipAccountId);
$bankAccount = $moip->bankaccount()->get($bankAccountId);
$bankAccounts = $moip->bankaccount()->getList($moipAccountId)->getBankAccounts();
With this feature you can update all bank account data.
$bankAccount = $moip->bankaccount()
->setAccountCheckNumber('8')
->update($bankAccountId);
$moip->bankaccount()->delete($bankAccountId);
The Transfer is a fund movement between the Moip account and another payment account (it can be a bank account or another Moip account).
The transfer can be created to a bank account (previously created or not) or to another Moip account.
$amount = 500;
$bankNumber = '001';
$agencyNumber = '1111';
$agencyCheckNumber = '2';
$accountNumber = '9999';
$accountCheckNumber = '8';
$holderName = 'Nome do Portador';
$taxDocument = '22222222222';
$transfer = $moip->transfers()
->setTransfers($amount, $bankNumber, $agencyNumber, $agencyCheckNumber, $accountNumber, $accountCheckNumber)
->setHolder($holderName, $taxDocument)
->execute();
To transfer using a previously created bank account:
$transfer = $moip->transfers()
->setTransfersToBankAccount($amount, $bankAccountId)
->execute();
$transferId = 'TRA-28HRLYNLMUFH';
$transfer = $moip->transfers()->get($transferId);
Is possible to use search parameters to list the transfers.
parameters | type |
---|---|
pagination | integer |
$transfers = $moip->transfers()->getList();
$transfers = $moip->transfers()->getList(new Pagination(10,0));
$transferId = 'TRA-28HRLYNLMUFH';
$transfer = $moip->transfers()->revert($transferId);
Webhooks are the notifications sent by Moip to your system every time your transaction has its status changed. So, through webhooks, is possible to synchronize your application with Moip.
To receive webhooks you should create a notification preference. At this moment, you can register one or more URLs to receive webhooks.
$notification = $moip->notifications()->addEvent('ORDER.*')
->addEvent('PAYMENT.AUTHORIZED')
->setTarget('http://requestb.in/1dhjesw1')
->create();
$notification = $this->moip->notifications()->get('NPR-N6QZE3223P98');
$notifications = $moip->notifications()->getList();
$notification = $moip->notifications()->delete('NOTIFICATION-ID');
PHP is, by default, able to receive just some types of 'content-type' ('application/x-www-form-urlencoded' and 'multipart/form-data'), but Moip sends data in JSON format. So, to receive and access the data sent by Moip, you should add the below code to the file which will receive the notifications.
// Gets the request's raw data
$json = file_get_contents('php://input');
// Converts data to JSON
$response = json_decode($json, true);
This listing can be filtered with some search parameters:
parameters | type |
---|---|
pagination | Integer |
order/payment ID | String |
status/event | String |
$moip->webhooks()->get();
$moip->webhooks()->get(new Pagination(10, 0), 'ORD-ID', 'ORDER.PAID');
When an error occurs on the API, an exception it's thrown:
errors | cause | status |
---|---|---|
UnautorizedException | to authentication errors | == 401 |
ValidationException | to validation errors | >= 400 && <= 499 (except 401) |
UnexpectedException | to unexpected errors | >= 500 |
To catch these errors for debug:
try {
$moip->customers()->setOwnId(uniqid())
->setFullname('Fulano de Tal')
->setEmail('[email protected]')
//...
->create();
} catch (\Moip\Exceptions\UnautorizedException $e) {
//StatusCode 401
echo $e->getMessage();
} catch (\Moip\Exceptions\ValidationException $e) {
//StatusCode entre 400 e 499 (exceto 401)
printf($e->__toString());
} catch (\Moip\Exceptions\UnexpectedException $e) {
//StatusCode >= 500
echo $e->getMessage();
}
By default, the tests don't make any request to Moip API. However, is possible run the tests on Sandbox environment. In order to do it, it's enough set the environment variable:
- MOIP_ACCESS_TOKEN
(the authorization token of your Moip APP).
Example:
export MOIP_ACCESS_TOKEN=76926cb0305243c8adc79aad54321ec1_v2
vendor/bin/phpunit -c .