Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 40 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,125 +192,57 @@ try{
}
```

## 🌟 Getting started with payment via Checkout Pro
## 🌟 Getting started with Checkout Pro via Orders

### Step 1: Require the libraries
The SDK exposes the bounded Orders API through `MercadoPago\Client\Order\OrderClient` and `MercadoPago\Client\Order\OrderTransactionClient`. Create the order with its transaction data; no separate transaction-creation step is required before processing.

```php
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Client\Preference\PreferenceClient;
use MercadoPago\Exceptions\MPApiException;
```
### Create an order

### Step 2: Create an authentication function
The snippet uses the `RequestOptions`, `OrderClient`, and `MPApiException` imports and access-token configuration established above.

```php
protected function authenticate()
{
// Getting the access token from .env file (create your own function)
$mpAccessToken = getVariableFromEnv('mercado_pago_access_token');
// Set the token the SDK's config
MercadoPagoConfig::setAccessToken($mpAccessToken);
// (Optional) Set the runtime enviroment to LOCAL if you want to test on localhost
// Default value is set to SERVER
MercadoPagoConfig::setRuntimeEnviroment(MercadoPagoConfig::LOCAL);
}
```

### Step 3: Create customer's preference before proceeding to Checkout Pro page

```php
// Function that will return a request object to be sent to Mercado Pago API
function createPreferenceRequest($items, $payer): array
{
$paymentMethods = [
"excluded_payment_methods" => [],
"installments" => 12,
"default_installments" => 1
];

$backUrls = array(
'success' => route('mercadopago.success'),
'failure' => route('mercadopago.failed')
);

$request = [
"items" => $items,
"payer" => $payer,
"payment_methods" => $paymentMethods,
"back_urls" => $backUrls,
"statement_descriptor" => "NAME_DISPLAYED_IN_USER_BILLING",
"external_reference" => "1234567890",
"expires" => false,
"auto_return" => 'approved',
];

return $request;
}
```

### Step 4: Create the preference on Mercado Pago ([DOCS](https://www.mercadopago.com.br/developers/pt/docs/sdks-library/server-side/php/preferences))
$order_client = new OrderClient();
$request = [
'type' => 'online',
'processing_mode' => 'automatic',
'capture_mode' => 'automatic_async',
'external_reference' => 'checkout-pro-order-001',
'total_amount' => '500.00',
'payer' => [
'email' => '<PAYER_EMAIL>',
],
'transactions' => [
'payments' => [
[
'amount' => '500.00',
'payment_method' => [
'id' => 'master',
'type' => 'credit_card',
'token' => '<CARD_TOKEN>',
'installments' => 1,
],
],
],
],
];

```php
public function createPaymentPreference(): ?Preference
{
// Fill the data about the product(s) being purchased
$product1 = array(
"id" => "1234567890",
"title" => "Product 1 Title",
"description" => "Product 1 Description",
"currency_id" => "BRL",
"quantity" => 12,
"unit_price" => 9.90
);

$product2 = array(
"id" => "9012345678",
"title" => "Product 2 Title",
"description" => "Product 2 Description",
"currency_id" => "BRL",
"quantity" => 5,
"unit_price" => 19.90
);

// Mount the array of products that will integrate the purchase amount
$items = array($product1, $product2);

// Retrieve information about the user (use your own function)
$user = getSessionUser();

$payer = array(
"name" => $user->name,
"surname" => $user->surname,
"email" => $user->email,
);

// Create the request object to be sent to the API when the preference is created
$request = createPreferenceRequest($item, $payer);

// Instantiate a new Preference Client
$client = new PreferenceClient();
$options = new RequestOptions();
$options->setCustomHeaders([
'X-Idempotency-Key: <SOME_UNIQUE_VALUE>',
]);

try {
// Send the request that will create the new preference for user's checkout flow
$preference = $client->create($request);

// Useful props you could use from this object is 'init_point' (URL to Checkout Pro) or the 'id'
return $preference;
} catch (MPApiException $error) {
// Here you might return whatever your app needs.
// We are returning null here as an example.
return null;
}
try {
$order = $order_client->create($request, $options);
echo 'Order ID: ' . $order->id;
} catch (MPApiException $e) {
echo 'Status code: ' . $e->getApiResponse()->getStatusCode() . "\n";
var_dump($e->getApiResponse()->getContent());
}
```

In case you need to retrieve the preference by ID:
`OrderClient` maps successful order responses to `MercadoPago\Resources\Order` and search responses to `MercadoPago\Resources\OrderSearch`. Nested transaction data is represented by `MercadoPago\Resources\Order\Transactions`, `MercadoPago\Resources\Order\Payment`, and `MercadoPago\Resources\Order\PaymentMethod`.

```php
$client = new PreferenceClient();
$client->get("123456789");
```
The supported order routes are `/v1/orders`, `/v1/orders/{id}`, `/v1/orders/{order_id}/cancel`, `/v1/orders/{order_id}/process`, `/v1/orders/{order_id}/capture`, and `/v1/orders/{order_id}/refund`. `OrderTransactionClient` operates on `/v1/orders/{order_id}/transactions` and `/v1/orders/{order_id}/transactions/{transaction_id}`. Supply `X-Idempotency-Key` through `RequestOptions` only for operations that declare it: order create, cancel, process, capture, and refund, plus transaction create and update. The transaction delete operation does not declare this header. A full refund can omit the request body, while a partial refund provides the amount in the request body.

## 📚 Documentation

Expand Down
1 change: 1 addition & 0 deletions examples/Order/CancelOrderById.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);

// POST /v1/orders/{order_id}/cancel
$order = $client->cancel($order_id, $request_options);
echo "Order ID: " . $order->id . "\n";
echo "Status: " . $order->status . "\n";
Expand Down
2 changes: 1 addition & 1 deletion examples/Order/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$request_options_capture = new RequestOptions();
$request_options_capture->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);

// Step 7: Capture the order
// Step 7: Capture the order with POST /v1/orders/{order_id}/capture
$order = $client->capture($order->id, $request_options_capture);
echo "\nOrder status after capture: " . $order->status;

Expand Down
2 changes: 1 addition & 1 deletion examples/Order/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);

// Step 6: Make the request
// Step 6: Create the order with POST /v1/orders
$order = $client->create($request, $request_options);
echo "Order ID:" . $order->id;

Expand Down
3 changes: 1 addition & 2 deletions examples/Order/CreateCheckoutPRO.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
$client = new OrderClient();

try {
// Step 4: Build the Checkout PRO request
// processing_mode "manual" triggers the Checkout PRO flow and returns a checkout_url
// Step 4: Build the manual Orders request used by Checkout Pro
$request = [
"type" => "online",
"processing_mode" => "manual",
Expand Down
2 changes: 1 addition & 1 deletion examples/Order/CreateOrderPSE.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <IDEMPOTENCY_KEY>"]);

// Step 6: Make the request
// Step 6: Create the PSE order with POST /v1/orders
$order = $client->create($request, $request_options);
print_r($order);

Expand Down
2 changes: 1 addition & 1 deletion examples/Order/CreateOrderWith3DS.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <IDEMPOTENCY_KEY>"]);

// Step 6: Make the request
// Step 6: Create the 3DS order with POST /v1/orders
$order = $client->create($request, $request_options);

// Step 7: Check if 3DS Challenge is required
Expand Down
5 changes: 3 additions & 2 deletions examples/Order/CreateOrderWithAutomaticPayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
* 2. Recurring charge — subsequent MIT charge without CVV, referencing step 1.
*
* Prerequisites:
* - A customer created via POST /v1/customers → CUSTOMER_ID
* - A payment profile created via POST /v1/customers/{id}/payment-profiles → PAYMENT_PROFILE_ID
* - An existing customer ID and automatic-payment profile ID.
*
* Both charges in this example are created only through POST /v1/orders.
*
* @see https://www.mercadopago.com/developers/en/docs/automatic-payments-orders/overview
*/
Expand Down
1 change: 0 additions & 1 deletion examples/Order/CreateOrderWithIndustryFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"external_reference" => "ext_ref_1234",
"capture_mode" => "automatic_async",
"processing_mode" => "automatic",
"marketplace" => "NONE",
"total_amount" => "1000.00",
"transactions" => [
"payments" => [
Expand Down
1 change: 0 additions & 1 deletion examples/Order/GetOrderById.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Step 1: Require the library from your Composer vendor folder
require_once '../../vendor/autoload.php';

use MercadoPago\Client\Common\RequestOptions;
use MercadoPago\Client\Order\OrderClient;
use MercadoPago\Exceptions\MPApiException;
use MercadoPago\MercadoPagoConfig;
Expand Down
1 change: 1 addition & 0 deletions examples/Order/ProcessOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

$order_id = "<ORDER_ID>";
try {
// POST /v1/orders/{order_id}/process
$request_options = new RequestOptions();
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);

Expand Down
2 changes: 1 addition & 1 deletion examples/Order/RefundPartial.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// Step 6: Create the Order
$order = $order_client->create($create_order_request, $request_options);

// Step 7: Create the request to refund Order partially
// Step 7: Create the body for POST /v1/orders/{order_id}/refund
$refund_request = [
"transactions" => [
[
Expand Down
3 changes: 2 additions & 1 deletion examples/Order/RefundTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
// Step 7: Set a new X-Idempotency-Key
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);

// Step 8: Refund the Order
// Step 8: Refund the full Order through POST /v1/orders/{order_id}/refund.
// A null body requests a total refund.
sleep(3);
$refunded_order = $order_client->refund($order->id, null, $request_options);

Expand Down
2 changes: 1 addition & 1 deletion examples/Order/Transaction/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$client = new OrderTransactionClient();

try {
// Step 4: Create the request
// Step 4: Create the transaction payload for POST /v1/orders/{order_id}/transactions
$request = [
"payments" => [
[
Expand Down
5 changes: 2 additions & 3 deletions examples/Order/Transaction/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
$transaction_id = $order->transactions->payments[0]->id;
$order_id = $order->id;

// Step 5: Delete a transaction
$request_options->setCustomHeaders(["X-Idempotency-Key: <SOME_UNIQUE_VALUE>"]);
$response = $client_transactions->delete($order_id, $transaction_id, $request_options);
// Step 5: Delete the transaction with DELETE /v1/orders/{order_id}/transactions/{transaction_id}
$response = $client_transactions->delete($order_id, $transaction_id);
if ($response->getStatusCode() === 204) {
echo "Transaction deleted successfully. HTTP Status Code: 204\n";
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/Order/Transaction/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
echo "\nPayment ID: " . $order->transactions->payments[0]->id;
echo "\nAmount: " . $order->transactions->payments[0]->amount;

// Step 8: Create the request to update a transaction
// Step 8: Create the payload for PUT /v1/orders/{order_id}/transactions/{transaction_id}
$update_transaction_request = [
"payment_method" => [
"installments" => 3,
Expand Down
Loading
Loading