Skip to content

Commit

Permalink
V1 to v3 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablorsk authored Nov 26, 2024
1 parent 0ee028a commit 34a3a1b
Show file tree
Hide file tree
Showing 21 changed files with 408 additions and 440 deletions.
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
101 changes: 70 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
```bash
docker run -it --rm --name php82 -e PHP_EXTENSIONS="" -v "$PWD":/usr/src/app pablorsk/laravel-json-api:8.2 bash
```
14 changes: 6 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions resources/rules/phpstan.src.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
3 changes: 0 additions & 3 deletions resources/rules/phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions src/Clients/InstoreOrderV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright (C) 1997-2020 Reyesoft <[email protected]>.
*
* 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;
}
}
43 changes: 43 additions & 0 deletions src/Clients/InstoreQrTramma.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright (C) 1997-2020 Reyesoft <[email protected]>.
*
* 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;
}
}
51 changes: 51 additions & 0 deletions src/Clients/PosClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Copyright (C) 1997-2020 Reyesoft <[email protected]>.
*
* 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;
}
}
42 changes: 42 additions & 0 deletions src/Clients/StoreClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright (C) 1997-2020 Reyesoft <[email protected]>.
*
* 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;
}
}
Loading

0 comments on commit 34a3a1b

Please sign in to comment.