diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..cb2f330 Binary files /dev/null and b/.DS_Store differ diff --git a/CHANGELOG.md b/CHANGELOG.md index 9122d4c..2044e31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.0.0] - 2024-11-25 + +### Changed +- pablorsk/dx-php to mercadopago/dx-php 3. + ## [1.1.4] - 2023-02-15 ### Changed diff --git a/README.md b/README.md index 94c89ca..2013f31 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # MercadoPagoQr -This library helps you generate a QR code to make payments through Mercadopago, even with QR code. Makes use of [endroid/qr-code] -(https://github.com/endroid/qr-code) and [mercadopago/dx-php](https://github.com/mercadopago/dx-php) +This library extend API entrypoints of [mercadopago/dx-php](https://github.com/mercadopago/dx-php), like Instore V2, Qr Tramma, and Store Client. ## Installation @@ -11,46 +10,86 @@ composer require reyesoft/mercadopago ## Usage -### Creating a Pos +Usage it's the same of mercadopago/dx-php, but with some new features and extensions. Like... -```php -$mp = new \MP('client_id', 'client_secret'); -// $mp = new \MP('your_access_token'); +### Creating and handling a Pos -$pos = new MercadoPagoPos($mp, 'MyTestPos'); -$filename = __DIR__ . '/image/mercadopago-qr-code.png'; -$pos->getQrCode()->writeFile($filename); +```php +MercadoPagoConfig::setAccessToken('YOUR_ACCESS_TOKEN']); + +$store = (new StoreClient())->create( + $params->getUserId(), + [ + 'name' => 'Reyesoft Point', + 'business_hours' => [ + 'wednesday' => [ + [ + 'open' => '00:00', + 'close' => '23:59', + ], + ], + ], + 'external_id' => 'your_store_external_id', + 'location' => [ + 'street_number' => '3039', + 'street_name' => 'Caseros', + 'city_name' => 'Belgrano', + 'state_name' => 'Capital Federal', + 'latitude' => -32.8897322, + 'longitude' => -68.8443275, + 'reference' => '3er Piso', + ], + ] +); ``` You can get MercadoPago credentials from https://www.mercadopago.com/mla/account/credentials -### Filling the Pos with a Order +### Showing a Point Of Sale QR ```php -$order_data = [ - 'external_reference' => 'id_interno', - 'notification_url' => 'www.yourserver.com/endpoint', - 'items' => [ - [ - 'title' => 'api_smsc_com_ar', - 'quantity' => 1, - 'currency_id' => 'ARS', - 'unit_price' => 450, - ], - ], -]; +$pos = new MercadoPagoPos('your_pos_external_id'); +$qr_content = $pos->getQrContent(); -$order = $pos->createAnOrder(); -$result = $order->sendData($order_data); +// just an example, this is Endroid\QrCode library +$qr_code = new QrCode($qr_content); +$qr_code->setText($qr_content); +echo $qr_code->getDataUri(); ``` -This generates a qr code like that: - -![mercadopago-qr](https://github.com/reyesoft/mercadopago/blob/master/tests/image/mercadopago-qr-code.png?raw=true "Mercadopago QR generated with MercadoPagoQr library") +### Putting a order in a Point Of Sale -## Support us -Reyesoft is a software industry based in San Rafael, Argentina. You'll find an overview of all our projects on our [website](http://reyesoft.com/). +```php +(new InstoreOrderV2())->create( + 'your_mercadopago_user_id', + 'your_store_external_id', + 'your_pos_external_id', + [ + 'external_reference' => '#' . $your_order_id, + 'title' => 'Order ' . $your_order_id, + 'description' => 'Your proyects', + 'notification_url' => 'https://yourserver.com/endpoint', + 'total_amount' => 100.10, + 'expiration_date' => today()->addDays(7)->format('Y-m-d\TH:i:s.vP'), + 'items' => [ + [ + 'title' => 'Your lovely product', + 'id' => '#' . $your_order_id, + 'external_reference' => '#' . $your_order_id, + 'quantity' => 1, + 'unit_measure' => 'unit', + 'currency_id' => 'ARS', + 'unit_price' => 100.10, + 'total_amount' => 100.10, + 'picture_url' => 'https://yourserver.com/image.jpg', + ], + ], + ] +); +``` -## PHP 7.4 +### Images -> docker run -it --rm --name php74 -e PHP_EXTENSIONS="gd" -v "$PWD":/usr/src/app thecodingmachine/php:7.4-v4-cli bash \ No newline at end of file +```bash +docker run -it --rm --name php82 -e PHP_EXTENSIONS="" -v "$PWD":/usr/src/app pablorsk/laravel-json-api:8.2 bash +``` diff --git a/composer.json b/composer.json index 0027881..c5ef864 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "reyesoft/mercadopago", "description": "This library helps to make payments through MercadoPago, even with QR", "type": "library", - "version": "1.1.4", + "version": "3.0.0", "keywords": ["qrcode", "payments", "mercadopago api", "reyesoft"], "homepage": "https://github.com/reyesoft/mercadopago", "license": "MIT", @@ -16,15 +16,12 @@ } ], "require": { - "php": ">=7.2", - "endroid/qr-code": "^3.2", - "ext-gd": "*", - "pablorsk/dx-php": "2.2.8" + "php": ">=8.2", + "mercadopago/dx-php": "3.0.8" }, "require-dev": { - "reyesoft/ci": "1.2.0", - "phpunit/phpunit": "^7", - "codedungeon/phpunit-result-printer": "0.19.10" + "reyesoft/ci": "^2.0", + "phpunit/phpunit": "^10.2" }, "autoload": { "classmap": [ @@ -40,6 +37,7 @@ } }, "scripts": { + "autofix": "sh autofix.sh", "ci": "bash vendor/reyesoft/ci/parallel.bash -s \"composer ci-double-spaces\" \"composer ci-php-cs-fixer\" \"composer phpstan\"", "ci-double-spaces": [ "sh vendor/reyesoft/ci/tools/find-double-spaces.sh src", diff --git a/resources/rules/phpstan.src.neon b/resources/rules/phpstan.src.neon index bb1ca51..921b4eb 100644 --- a/resources/rules/phpstan.src.neon +++ b/resources/rules/phpstan.src.neon @@ -3,6 +3,7 @@ includes: parameters: ignoreErrors: - # Problem with MercadoPago Dx PHP - - '#Class MercadoPago\\POS referenced with incorrect#' - - '#Access to protected property MercadoPago\\#' + - '#.* no value type specified in iterable type array#' + - '#Short ternary operator is not allowed.*#' + - '#Parameter .* expects string\|null, string\|false given#' + - '#.*but returns MercadoPago\\Net\\MPResource#' \ No newline at end of file diff --git a/resources/rules/phpstan.tests.neon b/resources/rules/phpstan.tests.neon index 2846883..b1299cb 100644 --- a/resources/rules/phpstan.tests.neon +++ b/resources/rules/phpstan.tests.neon @@ -2,6 +2,3 @@ includes: - ../../vendor/phpstan/phpstan-strict-rules/rules.neon - ../../vendor/phpstan/phpstan-phpunit/extension.neon - ../../vendor/phpstan/phpstan-phpunit/rules.neon - -parameters: - bootstrap: %rootDir%/../../../resources/rules/phpstan-bootstrap.php diff --git a/src/Clients/InstoreOrderV2.php b/src/Clients/InstoreOrderV2.php new file mode 100644 index 0000000..6898aa3 --- /dev/null +++ b/src/Clients/InstoreOrderV2.php @@ -0,0 +1,42 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Clients; + +use MercadoPago\Client\Common\RequestOptions; +use MercadoPago\Client\MercadoPagoClient; +use MercadoPago\MercadoPagoConfig; +use MercadoPago\Net\HttpMethod; +use MercadoPago\Net\MPHttpClient; +use MercadoPagoQr\Resources\Pos; + +final class InstoreOrderV2 extends MercadoPagoClient +{ + private const URL_CREATE = '/instore/qr/seller/collectors/%s/stores/%s/pos/%s/orders'; + + public function __construct(?MPHttpClient $MPHttpClient = null) + { + parent::__construct($MPHttpClient ?: MercadoPagoConfig::getHttpClient()); + } + + /** + * @see https://www.mercadopago.com.ar/developers/en/reference/instore_orders_v2/_instore_qr_seller_collectors_user_id_stores_external_store_id_pos_external_pos_id_orders/put + * + * @throws \TypeError this entrypoint return empty response, and is not supported by MercadoPago DX PHP + */ + public function create(int $user_id, string $external_store_id, string $external_pos_id, array $payload, ?RequestOptions $request_options = null): bool + { + // no response + $response = parent::send(sprintf(self::URL_CREATE, $user_id, $external_store_id, $external_pos_id), HttpMethod::PUT, json_encode($payload), null, $request_options); + + return $response->getStatusCode() > 200 && $response->getStatusCode() < 300; + } +} diff --git a/src/Clients/InstoreQrTramma.php b/src/Clients/InstoreQrTramma.php new file mode 100644 index 0000000..49eed99 --- /dev/null +++ b/src/Clients/InstoreQrTramma.php @@ -0,0 +1,43 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Clients; + +use MercadoPago\Client\Common\RequestOptions; +use MercadoPago\Client\MercadoPagoClient; +use MercadoPago\MercadoPagoConfig; +use MercadoPago\Net\HttpMethod; +use MercadoPago\Net\MPHttpClient; +use MercadoPago\Serialization\Serializer; +use MercadoPagoQr\Resources\Pos; +use MercadoPagoQr\Resources\QrTramma; + +final class InstoreQrTramma extends MercadoPagoClient +{ + private const URL_CREATE = '/instore/orders/qr/seller/collectors/%s/pos/%s/qrs'; + + public function __construct(?MPHttpClient $MPHttpClient = null) + { + parent::__construct($MPHttpClient ?: MercadoPagoConfig::getHttpClient()); + } + + /** + * @see https://www.mercadopago.com.ar/developers/en/reference/qr-dynamic/_instore_orders_qr_seller_collectors_user_id_pos_external_pos_id_qrs/post + */ + public function create(int $user_id, string $external_pos_id, array $payload, ?RequestOptions $request_options = null): QrTramma + { + $response = parent::send(sprintf(self::URL_CREATE, $user_id, $external_pos_id), HttpMethod::POST, json_encode($payload), null, $request_options); + $result = Serializer::deserializeFromJson(QrTramma::class, $response->getContent()); + $result->setResponse($response); + + return $result; + } +} diff --git a/src/Clients/PosClient.php b/src/Clients/PosClient.php new file mode 100644 index 0000000..93c878b --- /dev/null +++ b/src/Clients/PosClient.php @@ -0,0 +1,51 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Clients; + +use MercadoPago\Client\Common\RequestOptions; +use MercadoPago\Client\MercadoPagoClient; +use MercadoPago\Exceptions\MPApiException; +use MercadoPago\MercadoPagoConfig; +use MercadoPago\Net\HttpMethod; +use MercadoPago\Net\MPHttpClient; +use MercadoPago\Serialization\Serializer; +use MercadoPagoQr\Resources\Pos; + +final class PosClient extends MercadoPagoClient +{ + private const URL = '/pos'; + + public function __construct(?MPHttpClient $MPHttpClient = null) + { + parent::__construct($MPHttpClient ?: MercadoPagoConfig::getHttpClient()); + } + + /** + * Method responsible for creating card token. + * + * @param array $request card token data + * + * @throws MPApiException if the request fails + * @throws \Exception if the request fails + * + * @return Pos card token created + */ + public function create(array $request, ?RequestOptions $request_options = null): Pos + { + $response = parent::send(self::URL, HttpMethod::POST, json_encode($request), null, $request_options); + $result = Serializer::deserializeFromJson(Pos::class, $response->getContent()); + $result->setResponse($response); + + // @phpstan-ignore-next-line + return $result; + } +} diff --git a/src/Clients/StoreClient.php b/src/Clients/StoreClient.php new file mode 100644 index 0000000..2d59079 --- /dev/null +++ b/src/Clients/StoreClient.php @@ -0,0 +1,42 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Clients; + +use MercadoPago\Client\Common\RequestOptions; +use MercadoPago\Client\MercadoPagoClient; +use MercadoPago\MercadoPagoConfig; +use MercadoPago\Net\HttpMethod; +use MercadoPago\Net\MPHttpClient; +use MercadoPago\Serialization\Serializer; +use MercadoPagoQr\Resources\Store; + +final class StoreClient extends MercadoPagoClient +{ + private const URL_CREATE = '/users/%s/stores'; + + public function __construct(?MPHttpClient $MPHttpClient = null) + { + parent::__construct($MPHttpClient ?: MercadoPagoConfig::getHttpClient()); + } + + /** + * @see https://www.mercadopago.com.ar/developers/en/reference/stores/_users_user_id_stores/post + */ + public function create(int $user_id, array $payload, ?RequestOptions $request_options = null): Store + { + $response = parent::send(sprintf(self::URL_CREATE, $user_id), HttpMethod::POST, json_encode($payload), null, $request_options); + $result = Serializer::deserializeFromJson(Store::class, $response->getContent()); + $result->setResponse($response); + + return $result; + } +} diff --git a/src/MercadoPagoOrder.php b/src/MercadoPagoOrder.php deleted file mode 100644 index e30b83c..0000000 --- a/src/MercadoPagoOrder.php +++ /dev/null @@ -1,87 +0,0 @@ -. - * - * This file is part of CryptoQr. CryptoQr can not be copied and/or - * distributed without the express permission of Reyesoft - */ - -declare(strict_types=1); - -namespace MercadoPagoQr; - - -use MercadoPago\Entities\InstoreOrder; - -/** - * @see https://www.mercadopago.com.ar/developers/en/guides/instore-payments/qr-payments/qr-pos/ "Order object" - */ -class MercadoPagoOrder -{ - use HasMpTrait; - - /** @var MercadoPagoPos */ - protected $pos; - - /** @var InstoreOrder */ - protected $order; - - public function __construct(MercadoPagoPos $pos) - { - $this->pos = $pos; - $this->order = new InstoreOrder(); - } - - public function setExternalReference(string $value): void - { - $this->order->external_reference = $value; - } - - public function setNotificationUrl(string $value): void - { - $this->order->notification_url = $value; - } - - /** - * @param array $value - */ - public function setItems(array $value): void - { - $this->order->items = $value; - } - - /** - * @return array - */ - public function getResponse(): array - { - return $this->order->getAttributes(); - } - - /** - * @param array $data - * @param null $collector_id - * - * @throws \Exception - * - * @return array - */ - public function sendData(array $data, $collector_id = null): array - { - if (isset($data['external_reference'])) { - $this->setExternalReference($data['external_reference']); - } - if (isset($data['notification_url'])) { - $this->setNotificationUrl($data['notification_url']); - } - if (isset($data['items'])) { - $this->setItems($data['items']); - } - - $this->order->external_id = $this->pos->getPosData()->getExternalId(); - - $this->order->save(); - - return $this->getResponse(); - } -} diff --git a/src/MercadoPagoPos.php b/src/MercadoPagoPos.php index 79cdc3f..6003b44 100644 --- a/src/MercadoPagoPos.php +++ b/src/MercadoPagoPos.php @@ -1,4 +1,5 @@ . * @@ -10,8 +11,10 @@ namespace MercadoPagoQr; -use Endroid\QrCode\QrCode; -use MercadoPago\Entities\Pos as Pos; +use MercadoPago\Exceptions\MPApiException; +use MercadoPagoQr\Clients\PosClient; +use MercadoPagoQr\Resources\Pos; +use MercadoPagoQr\Support\HasMpTrait; /** * @see https://www.mercadopago.com.ar/developers/en/guides/instore-payments/qr-payments/qr-pos/ "Create QR" @@ -20,82 +23,36 @@ class MercadoPagoPos { use HasMpTrait; - /** @var QrCode */ - protected $qr_code; - - /** @var MercadoPagoPosData */ - protected $data; - - /** @var Pos */ - protected $pos; - - public function __construct(string $pos_external_id = '') - { - $this->qr_code = new QrCode(); - $this->data = new MercadoPagoPosData($pos_external_id); - $this->pos = new Pos(); - } - - public function getPosData(): MercadoPagoPosData - { - return $this->data; - } - - public function createOrFail(): bool - { - $pos = new POS(); - $pos->name = $this->data->getName(); - $pos->external_id = $this->data->getExternalId(); - $pos->store_id = $this->data->getStoreId(); - $pos->fixed_amount = $this->data->getFixedAmount(); - $pos->category = $this->data->getCategory(); - $pos->save(); - - if ($pos->Error() === null) { - return true; - } - - throw new MercadoPagoQrException( - $pos->Error()->message - . ' (' . $pos->Error()->error . ')' - . ' (' . $pos->Error()->status . ')' - ); + public function __construct(private readonly string $pos_external_id) {} + + /** + * @throws MPApiException + * + * @see https://www.mercadopago.com.ar/developers/en/reference/pos/_pos/post + */ + public static function createOrFail( + string $name, + string $external_id, + bool $fixed_amount = true, + ?string $category = null, // 621102 gastronomia argentina + ?int $store_id = null + ): Pos { + // https://www.mercadopago.com.ar/developers/en/reference/pos/_pos/post + return (new PosClient())->create(array_filter([ + 'name' => $name, + 'external_id' => $external_id, + 'store_id' => $store_id, + 'fixed_amount' => $fixed_amount, + 'category' => $category, + ])); } - public function checkOrCreate(): bool - { - try { - return $this->createOrFail(); - // @todo - // @codeCoverageIgnoreStart - } catch (MercadoPagoQrException $e) { - // created - if (strpos($e->getMessage(), 'point_of_sale_exists') > 0) { - return true; - } else { - throw $e; - } - } catch (\Exception $e) { - throw $e; - } - // @codeCoverageIgnoreEnd - } - - public function getQrCode(string $collector_id = null): QrCode + public function getQrContent(?string $collector_id = null): string { if ($collector_id === null) { $collector_id = $this->getCollectorIdFromMp(); } - $this->qr_code->setText( - 'https://mercadopago.com/s/qr/' . $collector_id . '/' . $this->data->getExternalId() - ); - - return $this->qr_code; - } - - public function createAnOrder(): MercadoPagoOrder - { - return new MercadoPagoOrder($this); + return 'https://mercadopago.com/s/qr/' . $collector_id . '/' . $this->pos_external_id; } } diff --git a/src/MercadoPagoPosData.php b/src/MercadoPagoPosData.php deleted file mode 100644 index e0240b2..0000000 --- a/src/MercadoPagoPosData.php +++ /dev/null @@ -1,120 +0,0 @@ -. - * - * This file is part of CryptoQr. CryptoQr can not be copied and/or - * distributed without the express permission of Reyesoft - */ - -declare(strict_types=1); - -namespace MercadoPagoQr; - -/** - * @see https://www.mercadopago.com.ar/developers/en/guides/instore-payments/qr-payments/qr-pos/ "Create QR" - */ -class MercadoPagoPosData -{ - /** - * @var string - */ - private $name = ''; - /** - * @var string - */ - private $external_id; - /** - * @var bool - */ - private $fixed_amount = true; - /** - * @var string|null - */ - private $category; // 621102 gastronomia argentina - /** - * @var int|null - */ - private $store_id; - - public function __construct(string $pos_external_id = '') - { - $this->external_id = $pos_external_id; - } - - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - - public function getFixedAmount(): bool - { - return $this->fixed_amount; - } - - public function setFixedAmount(bool $fixed_amount): self - { - $this->fixed_amount = $fixed_amount; - - return $this; - } - - public function getCategory(): ?string - { - return $this->category; - } - - public function setCategory(?string $category): self - { - $this->category = $category; - - return $this; - } - - public function getStoreId(): ?int - { - return $this->store_id; - } - - public function setStoreId(?int $store_id): self - { - $this->store_id = $store_id; - - return $this; - } - - public function getExternalId(): string - { - return $this->external_id; - } - - public function setExternalId(string $external_id): self - { - $this->external_id = $external_id; - - return $this; - } - - /** - * @deprecated since 2.0.0 - * @codeCoverageIgnore - * - * @return array - */ - public function getDataArray(): array - { - return [ - 'name' => $this->name, - 'external_id' => $this->external_id, - 'store_id' => $this->store_id, - 'fixed_amount' => $this->fixed_amount, - 'category' => $this->category, - ]; - } -} diff --git a/src/Resources/Pos.php b/src/Resources/Pos.php new file mode 100644 index 0000000..a0dccc6 --- /dev/null +++ b/src/Resources/Pos.php @@ -0,0 +1,44 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Resources; + +use MercadoPago\Serialization\Mapper; + +class Pos +{ + use Mapper; + + public ?int $id; + public ?string $qr; + public ?string $status; + public ?string $date_created; + public ?string $date_last_updated; + public ?string $uuid; + public ?int $user_id; + public ?string $name; + public ?bool $fixed_amount; + public ?int $category; + public ?int $store_id; + public ?string $external_store_id; + public ?string $external_id; + private array $map = [ + 'qr' => 'MercadoPagoQr\Resources\Qr', + ]; + + /** + * Method responsible for getting map of entities. + */ + public function getMap(): array + { + return $this->map; + } +} diff --git a/src/Resources/Qr.php b/src/Resources/Qr.php new file mode 100644 index 0000000..aba2994 --- /dev/null +++ b/src/Resources/Qr.php @@ -0,0 +1,19 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Resources; + +class Qr +{ + public ?string $image; + public ?string $template_document; + public ?string $template_image; +} diff --git a/src/MercadoPagoQrException.php b/src/Resources/QrTramma.php similarity index 66% rename from src/MercadoPagoQrException.php rename to src/Resources/QrTramma.php index ee51a55..0f6123a 100644 --- a/src/MercadoPagoQrException.php +++ b/src/Resources/QrTramma.php @@ -1,4 +1,5 @@ . * @@ -8,8 +9,10 @@ declare(strict_types=1); -namespace MercadoPagoQr; +namespace MercadoPagoQr\Resources; -class MercadoPagoQrException extends \Exception +class QrTramma { + public ?string $qr_data; + public ?string $in_store_order_id; } diff --git a/src/Resources/Store.php b/src/Resources/Store.php new file mode 100644 index 0000000..5061fda --- /dev/null +++ b/src/Resources/Store.php @@ -0,0 +1,22 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Resources; + +class Store +{ + public ?int $id; + public ?string $name; + public ?string $date_created; + public ?array $business_hours; + public ?array $location; + public ?string $external_id; +} diff --git a/src/HasMpTrait.php b/src/Support/HasMpTrait.php similarity index 62% rename from src/HasMpTrait.php rename to src/Support/HasMpTrait.php index 3e5de5b..442fb98 100644 --- a/src/HasMpTrait.php +++ b/src/Support/HasMpTrait.php @@ -1,4 +1,5 @@ . * @@ -8,16 +9,16 @@ declare(strict_types=1); -namespace MercadoPagoQr; +namespace MercadoPagoQr\Support; -use MercadoPago\SDK; +use MercadoPago\MercadoPagoConfig; trait HasMpTrait { protected function getCollectorIdFromMp(): string { - preg_match('/^.*\-([0-9]+)$/', SDK::getAccessToken(), $matches); + preg_match('/^.*\-([0-9]+)$/', MercadoPagoConfig::getAccessToken(), $matches); - return (string) $matches[1]; + return $matches[1] ?? ''; } } diff --git a/src/Support/MercadoPagoQrException.php b/src/Support/MercadoPagoQrException.php new file mode 100644 index 0000000..781673d --- /dev/null +++ b/src/Support/MercadoPagoQrException.php @@ -0,0 +1,16 @@ +. + * + * This file is part of CryptoQr. CryptoQr can not be copied and/or + * distributed without the express permission of Reyesoft + */ + +declare(strict_types=1); + +namespace MercadoPagoQr\Support; + +use MercadoPago\Exceptions\MPApiException; + +class MercadoPagoQrException extends MPApiException {} diff --git a/tests/MercadoPagoQrTest.php b/tests/MercadoPagoQrTest.php index f230b21..d827b52 100644 --- a/tests/MercadoPagoQrTest.php +++ b/tests/MercadoPagoQrTest.php @@ -1,4 +1,5 @@ . * @@ -10,124 +11,18 @@ namespace Tests; -use MercadoPago\SDK; -use MercadoPagoQr\MercadoPagoPos; +use MercadoPago\MercadoPagoConfig; use PHPUnit\Framework\TestCase; /** * @internal - * @covers \MercadoPagoQr\MercadoPagoOrder - * @covers \MercadoPagoQr\MercadoPagoPos + * + * @coversNothing */ final class MercadoPagoQrTest extends TestCase { - // public static $location_mp = null; - public function initializeMercadoPagoSdk(): void { - // $GLOBALS['LIB_LOCATION'] = self::$location_mp; // fix problem on library - // if (self::$mp === null) { - // // self::$mp = new \MP('3282634683852359', $client_secret); - // // self::$mp = new \MP('your_access_token'); - // self::$location_mp = $GLOBALS['LIB_LOCATION']; - // - // } - SDK::setClientId('3282634683852359'); - SDK::setClientSecret('BAB5nUMycs4Nhpy5itEoGHMNrF2fklUR'); - } - - public function testCreateAPosQr(): string - { - $this->initializeMercadoPagoSdk(); - - $pos = new MercadoPagoPos(); - - $pos->getPosData() - ->setExternalId('MyTestPos' . random_int(1, 1000) . time()) - ->setName('My MercadoPago POS of testing') - ->setFixedAmount(false) - ->setCategory(null) - ->setStoreId(null); - - $created = $pos->createOrFail(); - - static::assertTrue($created); - - return $pos->getPosData()->getExternalId(); - } - - /** - * @depends testCreateAPosQr - */ - public function testTryToCreateARepeatedPosQr(string $pos_id): void - { - $this->initializeMercadoPagoSdk(); - - $pos = new MercadoPagoPos(); - $pos->getPosData()->setExternalId($pos_id); - - $this->expectExceptionMessage('Point of sale with corresponding user and id exists'); - $pos->createOrFail(); - } - - public function testCreateTestPos(): void - { - $this->initializeMercadoPagoSdk(); - - $pos = new MercadoPagoPos('MyTestPos'); - - $pos->getPosData() - ->setName('My MercadoPago POS of testing'); - - $result = $pos->checkOrCreate(); - - static::assertTrue($result); - } - - /** - * @depends testCreateTestPos - */ - public function testCreateQr(): void - { - $this->initializeMercadoPagoSdk(); - - $pos = new MercadoPagoPos('MyTestPos'); - $filename = __DIR__ . '/image/mercadopago-qr-code.png'; - $pos->getQrCode()->writeFile($filename); - - $file_content = file_get_contents($filename); - static::assertNotFalse($file_content); - - $image = imagecreatefromstring($file_content); - - static::assertInternalType('resource', $image); - } - - /** - * @depends testCreateTestPos - */ - public function testCreateAnOrderForTestPos(): void - { - $this->initializeMercadoPagoSdk(); - - $pos = new MercadoPagoPos('MyTestPos'); - - $order_data = [ - 'external_reference' => 'id_interno', - 'notification_url' => 'www.yourserver.com/endpoint', - 'items' => [ - [ - 'title' => 'api_smsc_com_ar', - 'quantity' => 1, - 'currency_id' => 'ARS', - 'unit_price' => 450, - ], - ], - ]; - - $order = $pos->createAnOrder(); - $result = $order->sendData($order_data); - - static::assertSame($order->getResponse()['total_amount'], 450); + MercadoPagoConfig::setAccessToken('TEST-1234567890123456-123456-12345678901234567890'); } } diff --git a/tests/image/mercadopago-qr-code.png b/tests/image/mercadopago-qr-code.png deleted file mode 100644 index 450a455..0000000 Binary files a/tests/image/mercadopago-qr-code.png and /dev/null differ