Skip to content

Commit

Permalink
Update to 3DSv2 and implement recovery, cancel and refund (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol committed Jul 24, 2019
1 parent 1689a16 commit f265bd8
Show file tree
Hide file tree
Showing 38 changed files with 3,312 additions and 338 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: build/clover.xml
json_path: build/coveralls.json
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ composer.phar
# composer.lock
tests/Credentials.php
coverage
build
.php_cs.cache
23 changes: 23 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
filter:
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

tools:
external_code_coverage:
timeout: 600
runs: 4
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- 7.2

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-source
- composer require --dev scrutinizer/ocular --no-interaction
- composer require --dev php-coveralls/php-coveralls --no-interaction

script:
- vendor/bin/phpunit

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover build/clover.xml
- php vendor/bin/php-coveralls
113 changes: 110 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
# Monetico PHP SDK

This library aims to facilitate the usage of Monetico Payment Service
[![Latest Version](https://img.shields.io/packagist/v/DansMaCulotte/monetico-php.svg?style=flat-square)](https://packagist.org/packages/dansmaculotte/monetico-php)
[![Total Downloads](https://img.shields.io/packagist/dt/DansMaCulotte/monetico-php.svg?style=flat-square)](https://packagist.org/packages/dansmaculotte/monetico-php)
[![Build Status](https://img.shields.io/travis/DansMaCulotte/monetico-php/master.svg?style=flat-square)](https://travis-ci.org/dansmaculotte/monetico-php)
[![Quality Score](https://img.shields.io/scrutinizer/g/DansMaCulotte/monetico-php.svg?style=flat-square)](https://scrutinizer-ci.com/g/dansmaculotte/monetico-php)
[![Code Coverage](https://img.shields.io/coveralls/github/DansMaCulotte/monetico-php.svg?style=flat-square)](https://coveralls.io/github/dansmaculotte/monetico-php)

This library aims to facilitate the usage of Monetico Service Methods

## Installation

### Requirements

- PHP 7.0
- PHP 7.2

You can install the package via composer:

``` bash
```bash
composer require dansmaculotte/monetico-php
```

## Usage

### Monetico

```php
use DansMaCulotte\Monetico\Monetico;

Expand All @@ -29,8 +37,13 @@ $monetico = new Monetico(
);
```

### Payment

```php
use DansMaCulotte\Monetico\Payment\Payment;
use DansMaCulotte\Monetico\Resources\AddressBilling;
use DansMaCulotte\Monetico\Resources\AddressShipping;
use DansMaCulotte\Monetico\Resources\Client;

$payment = new Payment(array(
'reference' => 'ABCDEF123',
Expand All @@ -42,6 +55,15 @@ $payment = new Payment(array(
'datetime' => Carbon::create(2019, 1, 1),
));

$addressBilling = new AddressBilling('7 rue melingue', 'Caen', '14000', 'France');
$payment->setAddressBilling($addressBilling);

$addressShipping = new AddressShipping('7 rue melingue', 'Caen', '14000', 'France');
$payment->setAddressShipping($addressShipping);

$client = new Client('MR', 'John', 'Stark', 'Snow');
$payment->setClient($client);

$url = $monetico->getPaymentUrl();
$fields = $monetico->getPaymentFields($payment);
```
Expand All @@ -59,6 +81,91 @@ $result = $monetico->validateSeal($response);
$receipt = new Receipt($result);
```

### Recovery

```php
Use DansMaCulotte\Monetico\Recovery\Recovery;
use DansMaCulotte\Monetico\Recovery\Response;

$recovery = new Recovery([
'reference' => 'AXCDEF123',
'language' => 'FR',
'amount' => 42.42,
'amountToRecover' => 0,
'amountRecovered' => 0,
'amountLeft' => 42.42,
'currency' => 'EUR',
'orderDate' => Carbon::create(2019, 07, 17),
'dateTime' => Carbon::create(2019, 07, 17),
]);

$url = $monetico->getRecoveryUrl();
$fields = $monetico->getRecoveryFields($recovery);

$client = new GuzzleHttp\Client();
$data = $client->request('POST', $url, $fields);

// $data = json_decode($data, true);

$response = new Response($data);
```

### Cancel

```php
use DansMaCulotte\Monetico\Cancel\Cancel;
use DansMaCulotte\Monetico\Cancel\Response;

$cancel = new Cancel([
'dateTime' => Carbon::create(2019, 2, 1),
'orderDate' => Carbon::create(2019, 1, 1),
'reference' => 'ABC123',
'language' => 'FR',
'currency' => 'EUR',
'amount' => 100,
'amountRecovered' => 0,
]);

$url = $monetico->getCancelUrl();
$fields = $monetico->getCancelFields($recovery);

$client = new GuzzleHttp\Client();
$data = $client->request('POST', $url, $fields);

// $data = json_decode($data, true);

$response = new Response($data);
```

### Refund

```php
use DansMaCulotte\Monetico\Refund\Refund;
use DansMaCulotte\Monetico\Refund\Response;

$refund = new Refund([
'datetime' => Carbon::create(2019, 2, 1),
'orderDatetime' => Carbon::create(2019, 1, 1),
'recoveryDatetime' => Carbon::create(2019, 1, 1),
'authorizationNumber' => '1222',
'reference' => 'ABC123',
'language' => 'FR',
'currency' => 'EUR',
'amount' => 100,
'refundAmount' => 50,
'maxRefundAmount' => 80,
]);

$url = $monetico->getRefundUrl();
$fields = $monetico->getRefundFields($recovery);

$client = new GuzzleHttp\Client();
$data = $client->request('POST', $url, $fields);

// $data = json_decode($data, true);

$response = new Response($data);
```
## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Gaël Reyrol",
"email": "gael@dansmaculotte.fr"
},
{
"name": "Martin Potel",
"email": "martin@dansmaculotte.fr"
}
],
"autoload": {
Expand All @@ -19,7 +23,8 @@
}
},
"require": {
"php": "^7.0"
"php": "^7.2",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.2",
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
<testsuites>
<testsuite name="Unit Tests">
Expand All @@ -16,4 +20,9 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>
</phpunit>
28 changes: 28 additions & 0 deletions src/BaseMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DansMaCulotte\Monetico;

trait BaseMethod
{
public function generateSeal($securityKey, $fields)
{
ksort($fields);

$query = http_build_query($fields, null, '*');
$query = urldecode($query);

return strtoupper(hash_hmac(
'sha1',
$query,
$securityKey
));
}

public function generateFields($seal, $fields)
{
return array_merge(
$fields,
['MAC' => $seal]
);
}
}
39 changes: 39 additions & 0 deletions src/Cancel/Cancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace DansMaCulotte\Monetico\Cancel;

use DansMaCulotte\Monetico\Exceptions\Exception;
use DansMaCulotte\Monetico\Recovery\Recovery;
use DateTime;

class Cancel extends Recovery
{
public function __construct(array $data = [])
{
$data['amountLeft'] = 0;
$data['amountToRecover'] = 0;
parent::__construct($data);
}

/**
* @throws Exception
*/
public function validate()
{
if (!$this->dateTime instanceof DateTime) {
throw Exception::invalidDatetime();
}

if (!$this->orderDate instanceof DateTime) {
throw Exception::invalidOrderDate();
}

if (strlen($this->reference) > 12) {
throw Exception::invalidReference($this->reference);
}

if (strlen($this->language) != 2) {
throw Exception::invalidLanguage($this->language);
}
}
}
9 changes: 9 additions & 0 deletions src/Cancel/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DansMaCulotte\Monetico\Cancel;

use DansMaCulotte\Monetico\Recovery\Response as RecoveryResponse;

class Response extends RecoveryResponse
{
}

0 comments on commit f265bd8

Please sign in to comment.