Skip to content

Commit

Permalink
Merge pull request #448 from mercadopago/feature/implementation_merch…
Browse files Browse the repository at this point in the history
…ant_order

Implements Merchant Order resource
  • Loading branch information
eltinMeli committed Sep 13, 2023
2 parents 9725c39 + bf33271 commit 958e9b7
Show file tree
Hide file tree
Showing 22 changed files with 923 additions and 10 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<exclude>./tests/MercadoPago/Client/Customer/CustomerCardClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/User/UserClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/IdentificationType/IdentificationTypeClientITTest.php</exclude>
<exclude>./tests/MercadoPago/Client/MerchantOrder/MerchantOrderClientITTest.php</exclude>
</testsuite>
</testsuites>
</phpunit>
94 changes: 94 additions & 0 deletions src/MercadoPago/Client/MerchantOrder/MerchantOrderClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace MercadoPago\Client\MerchantOrder;

use MercadoPago\Client\MercadoPagoClient;
use MercadoPago\Core\MPRequestOptions;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\HttpMethod;
use MercadoPago\Net\MPSearchRequest;
use MercadoPago\Resources\MerchantOrder;
use MercadoPago\Resources\MerchantOrderSearch;
use MercadoPago\Serialization\Serializer;

/** Client responsible for performing merchant order actions. */
final class MerchantOrderClient extends MercadoPagoClient
{
private const URL = "/merchant_orders";

private const URL_WITH_ID = "/merchant_orders/%s";

private const URL_SEARCH = "/merchant_orders/search";

/** Default constructor. Uses the default http client used by the SDK. */
public function __construct()
{
parent::__construct(MercadoPagoConfig::getHttpClient());
}

/**
* Method responsible for creating merchant order.
* @param array $request merchant order data.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\MerchantOrder merchant order created.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function create(array $request, ?MPRequestOptions $request_options = null): MerchantOrder
{
$response = parent::send(self::URL, HttpMethod::POST, json_encode($request), null, $request_options);
$result = Serializer::deserializeFromJson(MerchantOrder::class, $response->getContent());
$result->setResponse($response);
return $result;
}

/**
* Method responsible for getting merchant order.
* @param int $id merchant order id.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\MerchantOrder merchant order found.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function get(int $id, ?MPRequestOptions $request_options = null): MerchantOrder
{
$response = parent::send(sprintf(self::URL_WITH_ID, strval($id)), HttpMethod::GET, null, null, $request_options);
$result = Serializer::deserializeFromJson(MerchantOrder::class, $response->getContent());
$result->setResponse($response);
return $result;
}

/**
* Method responsible for update merchant order.
* @param int $id merchant order id.
* @param array $request merchant order data.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\MerchantOrder merchant order updated.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function update(int $id, array $request, ?MPRequestOptions $request_options = null): MerchantOrder
{
$response = parent::send(sprintf(self::URL_WITH_ID, strval($id)), HttpMethod::PUT, json_encode($request), null, $request_options);
$result = Serializer::deserializeFromJson(MerchantOrder::class, $response->getContent());
$result->setResponse($response);
return $result;
}

/**
* Method responsible for search merchant orders.
* @param \MercadoPago\Net\MPSearchRequest $request search request.
* @param mixed $request_options request options to be sent.
* @return \MercadoPago\Resources\MerchantOrderSearch search results.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function search(MPSearchRequest $request, ?MPRequestOptions $request_options = null): MerchantOrderSearch
{
$query_params = isset($request) ? $request->getParameters() : null;
$response = parent::send(self::URL_SEARCH, HttpMethod::GET, null, $query_params, $request_options);
$result = Serializer::deserializeFromJson(MerchantOrderSearch::class, $response->getContent());
$result->setResponse($response);
return $result;
}
}
18 changes: 18 additions & 0 deletions src/MercadoPago/Resources/Common/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MercadoPago\Resources\Common;

use MercadoPago\Net\MPResource;

/** Search class. */
class Search extends MPResource
{
/** Search elements. */
public array $elements;

/** Search next offset. */
public ?int $next_offset;

/** Search total. */
public ?int $total;
}
97 changes: 97 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace MercadoPago\Resources;

use MercadoPago\Net\MPResource;
use MercadoPago\Serialization\Mapper;

class MerchantOrder extends MPResource
{
/** Class mapper. */
use Mapper;

/** Order ID. */
public ?int $id;

/** Payment preference identifier associated to the merchant order. */
public ?string $preference_id;

/** Application ID. */
public ?string $application_id;

/** Show the current merchant order state. */
public ?string $status;

/** Country identifier that merchant order belongs to. */
public ?string $site_id;

/** Payer information. */
public array|object|null $payer;

/** Seller information. */
public array|object|null $collector;

/** Sponsor ID. */
public ?string $sponsor_id;

/** Amount paid in this order. */
public ?float $paid_amount;

/** Amount refunded in this Order. */
public ?float $refunded_amount;

/** Shipping fee. */
public ?float $shipping_cost;

/** Date of creation. */
public ?string $date_created;

/** Last modified date. */
public ?string $last_updated;

/** If the Order is expired (true) or not (false). */
public ?bool $cancelled;

/** Payments information. */
public ?array $payments;

/** Items information. */
public ?array $items;

/** Shipments information. */
public ?array $shipments;

/** URL where you'd like to receive a payment notification. */
public ?string $notification_url;

/** Additional information. */
public ?string $additional_info;

/** Reference you can synchronize with your payment system. */
public ?string $external_reference;

/** Origin of the payment. */
public ?string $marketplace;

/** Total amount of the order. */
public ?float $total_amount;

/** Current merchant order status given the payments status. */
public ?string $order_status;

private $map = [
"payer" => "MercadoPago\Resources\MerchantOrder\Payer",
"collector" => "MercadoPago\Resources\MerchantOrder\Collector",
"payments" => "MercadoPago\Resources\MerchantOrder\Payment",
"items" => "MercadoPago\Resources\MerchantOrder\Item",
"shipments" => "MercadoPago\Resources\MerchantOrder\Shipment",
];

/**
* Method responsible for getting map of entities.
*/
public function getMap(): array
{
return $this->map;
}
}
12 changes: 12 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder/Collector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace MercadoPago\Resources\MerchantOrder;

class Collector
{
/** Payer ID. */
public ?int $id;

/** Payer nickname. */
public ?string $nickname;
}
30 changes: 30 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace MercadoPago\Resources\MerchantOrder;

class Item
{
/** Item code. */
public ?string $id;

/** Item name. */
public ?string $title;

/** Item description. */
public ?string $description;

/** Image URL. */
public ?string $picture_url;

/** Category of the item. */
public ?string $category_id;

/** Item's quantity. */
public ?int $quantity;

/** Unit price. */
public ?float $unit_price;

/** Currency ID. ISO_4217 code. */
public ?string $currency_id;
}
12 changes: 12 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder/Payer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace MercadoPago\Resources\MerchantOrder;

class Payer
{
/** Payer ID. */
public ?int $id;

/** Payer nickname. */
public ?string $nickname;
}
42 changes: 42 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace MercadoPago\Resources\MerchantOrder;

class Payment
{
/** Payment ID. */
public ?int $id;

/** Product cost. */
public ?float $transaction_amount;

/** Total amount paid. */
public ?float $total_paid_amount;

/** Shipping fee. */
public ?float $shipping_cost;

/** ID of the currency used in payment. */
public ?string $currency_id;

/** Payment status. */
public ?string $status;

/** Gives more detailed information on the current state or rejection cause. */
public ?string $status_details;

/** Operation type. */
public ?string $operation_type;

/** Approval date. */
public ?string $date_approved;

/** Date of creation. */
public ?string $date_created;

/** Last modified date. */
public ?string $last_modified;

/** Amount refunded in this payment. */
public ?float $amount_refunded;
}
70 changes: 70 additions & 0 deletions src/MercadoPago/Resources/MerchantOrder/ReceiverAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MercadoPago\Resources\MerchantOrder;

use MercadoPago\Serialization\Mapper;

class ReceiverAddress
{
/** Class mapper. */
use Mapper;

/** Receiver address ID. */
public ?int $id;

/** Street name and number of receiver address. */
public ?string $address_line;

/** Apartment. */
public ?string $apartment;

/** City information. */
public array|object|null $city;

/** State information. */
public array|object|null $state;

/** Country information. */
public array|object|null $country;

/** Comment about receiver address. */
public ?string $comment;

/** Contact information. */
public ?string $contact;

/** Postal code. */
public ?string $zip_code;

/** Street name. */
public ?string $street_name;

/** Street number. */
public ?string $street_number;

/** Floor. */
public ?string $floor;

/** Phone. */
public ?string $phone;

/** Latitude. */
public ?string $latitude;

/** Longitude. */
public ?string $longitude;

private $map = [
"city" => "MercadoPago\Resources\MerchantOrder\ReceiverAddressCity",
"state" => "MercadoPago\Resources\MerchantOrder\ReceiverAddressState",
"country" => "MercadoPago\Resources\MerchantOrder\ReceiverAddressCountry",
];

/**
* Method responsible for getting map of entities.
*/
public function getMap(): array
{
return $this->map;
}
}
Loading

0 comments on commit 958e9b7

Please sign in to comment.