From ce2c37eb05028b9f1cd292b3252d5ac3e202cd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 10:35:18 +0200 Subject: [PATCH 01/36] Added a composer file and updated gitignore to ignore the vendor folder in case any dependencies are pulled in --- .gitignore | 2 ++ composer.json | 11 +++++++++++ 2 files changed, 13 insertions(+) create mode 100644 composer.json diff --git a/.gitignore b/.gitignore index c6ef218..fa97e9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea + +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..98e8164 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "snowfire/mondido-php-sdk", + "description": "A fork of the Mondido PHP SDK available on composer", + "authors": [ + { + "name": "Leo Sjöberg", + "email": "leo@snowfire.net" + } + ], + "require": {} +} From 4f89bc9a4179fff2d8ebd6b80e0e3146c0156b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 12:15:29 +0200 Subject: [PATCH 02/36] Refactored for PSR-0 compliance --- composer.json | 10 +- composer.lock | 968 ++++++++++++++++++ examples/index.php | 10 +- examples/sample_transaction.php | 12 +- phpunit.php | 28 + phpunit.xml | 25 + src/{http_helper.php => HttpHelper.php} | 36 +- src/Mondido.php | 53 + src/api/ApiBase.php | 30 + src/api/StoredCard.php | 40 + src/api/api_base.php | 23 - src/api/refund.php | 24 +- src/api/stored_card.php | 37 - src/api/transaction.php | 51 +- src/models/BaseModel.php | 11 + src/models/CreditCard.php | 77 ++ src/models/base_model.php | 9 - src/models/credit_card.php | 65 -- src/models/transaction.php | 413 ++++---- src/mondido_sdk.php | 51 - src/request/webhook.php | 14 +- src/settings/configuration.php | 32 +- test/test_base.php | 24 - .../ApiBaseTest.php | 12 +- .../ApiRefundTest.php | 31 +- .../ApiStoredCardTest.php | 33 +- .../ApiTransactionTest.php | 33 +- tests/TestBase.php | 24 + .../webhook_Test.php => tests/WebhookTest.php | 15 +- 29 files changed, 1657 insertions(+), 534 deletions(-) create mode 100644 composer.lock create mode 100755 phpunit.php create mode 100755 phpunit.xml rename src/{http_helper.php => HttpHelper.php} (63%) create mode 100644 src/Mondido.php create mode 100644 src/api/ApiBase.php create mode 100644 src/api/StoredCard.php delete mode 100644 src/api/api_base.php delete mode 100644 src/api/stored_card.php create mode 100644 src/models/BaseModel.php create mode 100644 src/models/CreditCard.php delete mode 100644 src/models/base_model.php delete mode 100644 src/models/credit_card.php delete mode 100644 src/mondido_sdk.php delete mode 100644 test/test_base.php rename test/api_base_Test.php => tests/ApiBaseTest.php (70%) rename test/api_refund_Test.php => tests/ApiRefundTest.php (69%) rename test/api_stored_card_Test.php => tests/ApiStoredCardTest.php (71%) rename test/api_transaction_Test.php => tests/ApiTransactionTest.php (69%) create mode 100644 tests/TestBase.php rename test/webhook_Test.php => tests/WebhookTest.php (64%) diff --git a/composer.json b/composer.json index 98e8164..5f91565 100644 --- a/composer.json +++ b/composer.json @@ -7,5 +7,13 @@ "email": "leo@snowfire.net" } ], - "require": {} + "require": {}, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "autoload": { + "psr-4": { + "Mondido\\": "src/" + } + } } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..4a09669 --- /dev/null +++ b/composer.lock @@ -0,0 +1,968 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "a52ff9053f55fa7be248f6035cdce2e7", + "packages": [], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14 21:17:01" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "2.0.4", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "suggest": { + "dflydev/markdown": "~1.0", + "erusev/parsedown": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "phpDocumentor": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "mike.vanriel@naenius.com" + } + ], + "time": "2015-02-03 12:10:50" + }, + { + "name": "phpspec/prophecy", + "version": "v1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373", + "reference": "3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "phpdocumentor/reflection-docblock": "~2.0", + "sebastian/comparator": "~1.1" + }, + "require-dev": { + "phpspec/phpspec": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2015-04-27 22:15:08" + }, + { + "name": "phpunit/php-code-coverage", + "version": "2.1.8", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "6044546998c7627ab997501a3d0db972b3db9790" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6044546998c7627ab997501a3d0db972b3db9790", + "reference": "6044546998c7627ab997501a3d0db972b3db9790", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.2", + "phpunit/php-token-stream": "~1.3", + "sebastian/environment": "~1.0", + "sebastian/version": "~1.0" + }, + "require-dev": { + "ext-xdebug": ">=2.1.4", + "phpunit/phpunit": "~4" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.2.1", + "ext-xmlwriter": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2015-07-13 11:25:58" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "a923bb15680d0089e2316f7a4af8f437046e96bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a923bb15680d0089e2316f7a4af8f437046e96bb", + "reference": "a923bb15680d0089e2316f7a4af8f437046e96bb", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2015-04-02 05:19:05" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21 13:50:34" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "83fe1bdc5d47658b727595c14da140da92b3d66d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/83fe1bdc5d47658b727595c14da140da92b3d66d", + "reference": "83fe1bdc5d47658b727595c14da140da92b3d66d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2015-06-13 07:35:30" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "7a9b0969488c3c54fd62b4d504b3ec758fd005d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/7a9b0969488c3c54fd62b4d504b3ec758fd005d9", + "reference": "7a9b0969488c3c54fd62b4d504b3ec758fd005d9", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2015-06-19 03:43:16" + }, + { + "name": "phpunit/phpunit", + "version": "4.7.7", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "9b97f9d807b862c2de2a36e86690000801c85724" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9b97f9d807b862c2de2a36e86690000801c85724", + "reference": "9b97f9d807b862c2de2a36e86690000801c85724", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpspec/prophecy": "~1.3,>=1.3.1", + "phpunit/php-code-coverage": "~2.1", + "phpunit/php-file-iterator": "~1.4", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": ">=1.0.6", + "phpunit/phpunit-mock-objects": "~2.3", + "sebastian/comparator": "~1.1", + "sebastian/diff": "~1.2", + "sebastian/environment": "~1.2", + "sebastian/exporter": "~1.2", + "sebastian/global-state": "~1.0", + "sebastian/version": "~1.0", + "symfony/yaml": "~2.1|~3.0" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.7.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2015-07-13 11:28:34" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "2.3.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "1c330b1b6e1ea8fd15f2fbea46770576e366855c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/1c330b1b6e1ea8fd15f2fbea46770576e366855c", + "reference": "1c330b1b6e1ea8fd15f2fbea46770576e366855c", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "~1.0,>=1.0.2", + "php": ">=5.3.3", + "phpunit/php-text-template": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2015-07-04 05:41:32" + }, + { + "name": "sebastian/comparator", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1dd8869519a225f7f2b9eb663e225298fade819e", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2015-01-29 16:28:08" + }, + { + "name": "sebastian/diff", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/863df9687835c62aa423a22412d26fa2ebde3fd3", + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "http://www.github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2015-02-22 15:13:53" + }, + { + "name": "sebastian/environment", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5a8c7d31914337b69923db26c4221b81ff5a196e", + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2015-01-01 10:01:08" + }, + { + "name": "sebastian/exporter", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "84839970d05254c73cde183a721c7af13aede943" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/84839970d05254c73cde183a721c7af13aede943", + "reference": "84839970d05254c73cde183a721c7af13aede943", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2015-01-27 07:23:06" + }, + { + "name": "sebastian/global-state", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2014-10-06 09:23:50" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/3989662bbb30a29d20d9faa04a846af79b276252", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2015-01-24 09:48:32" + }, + { + "name": "sebastian/version", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2015-06-21 13:59:46" + }, + { + "name": "symfony/yaml", + "version": "v2.7.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/Yaml.git", + "reference": "4bfbe0ed3909bfddd75b70c094391ec1f142f860" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/4bfbe0ed3909bfddd75b70c094391ec1f142f860", + "reference": "4bfbe0ed3909bfddd75b70c094391ec1f142f860", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2015-07-01 11:25:50" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/examples/index.php b/examples/index.php index 6cdd037..4da401a 100644 --- a/examples/index.php +++ b/examples/index.php @@ -3,18 +3,18 @@ require_once(dirname(__FILE__) . '/../src/mondido_sdk.php'); //get the transaction id from the POST -$transaction = mondido\request\webhook::get(); +$transaction = \Mondido\Request\webhook::get(); //get the id $transaction_id = $transaction['id']; //log to file -mondido\mondido_sdk::logToFile('log.txt',$transaction); +Mondido\mondido_sdk::logToFile('log.txt',$transaction); -$transaction = mondido\api\transaction::get(1986); +$transaction = \Mondido\Api\Transaction::get(1986); print_r($transaction); -$transactions = mondido\api\transaction::index(10,0); +$transactions = \Mondido\Api\Transaction::index(10,0); print_r($transactions); -$refund = mondido\api\refund::create($transaction['id'],'wrong payment',$transaction['amount']); +$refund = \Mondido\Api\Refund::create($transaction['id'],'wrong payment',$transaction['amount']); print_r($refund); \ No newline at end of file diff --git a/examples/sample_transaction.php b/examples/sample_transaction.php index ef17566..921fdc5 100644 --- a/examples/sample_transaction.php +++ b/examples/sample_transaction.php @@ -1,15 +1,15 @@ "", "Amount" => 10, "PaymentRef" => "MyOrderId", - "Payment" => new credit_card(array( + "Payment" => new CreditCard(array( "Holder" => "PHPSDKTest", "Cvv" => "200", "Expiry" => "0116", @@ -30,5 +30,5 @@ "ErrorUrl" => "" )); -$response = mondido\api\transaction::create($transaction); +$response = \Mondido\Api\Transaction::create($transaction); print_r($response); \ No newline at end of file diff --git a/phpunit.php b/phpunit.php new file mode 100755 index 0000000..25c90c8 --- /dev/null +++ b/phpunit.php @@ -0,0 +1,28 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/src/http_helper.php b/src/HttpHelper.php similarity index 63% rename from src/http_helper.php rename to src/HttpHelper.php index e57c806..6552d39 100644 --- a/src/http_helper.php +++ b/src/HttpHelper.php @@ -1,41 +1,45 @@ -array( - 'method'=>"GET", + 'http' => array( + 'method' => "GET", 'header' => "Authorization: Basic " . base64_encode("$uname:$pass") ) ); $context = stream_context_create($opts); $file = file_get_contents($url, false, $context); - return json_decode($file, TRUE); + return json_decode($file, true); } - public static function delete($uname,$pass,$url){ + public static function delete($uname, $pass, $url) + { $opts = array( - 'http'=>array( - 'method'=>"DELETE", + 'http' => array( + 'method' => "DELETE", 'header' => "Authorization: Basic " . base64_encode("$uname:$pass") ) ); $context = stream_context_create($opts); $file = file_get_contents($url, false, $context); - return json_decode($file, TRUE); + return json_decode($file, true); } - public static function post($uname,$pass,$url,$data){ + public static function post($uname, $pass, $url, $data) + { $body = http_build_query($data); $opts = array( - 'http'=>array( - 'method'=>"POST", + 'http' => array( + 'method' => "POST", 'header' => array( "Authorization: Basic " . base64_encode("$uname:$pass"), - "Content-type: application/x-www-form-urlencoded", - "Content-Length: " . strlen($body) + "Content-type: application/x-www-form-urlencoded", + "Content-Length: " . strlen($body) ), 'content' => $body ) @@ -54,7 +58,7 @@ public static function post($uname,$pass,$url,$data){ //execute post $result = curl_exec($ch); - return json_decode($result, TRUE); + return json_decode($result, true); } } \ No newline at end of file diff --git a/src/Mondido.php b/src/Mondido.php new file mode 100644 index 0000000..c7fda2c --- /dev/null +++ b/src/Mondido.php @@ -0,0 +1,53 @@ +getAllAttributes(); $card_fields = $transaction->getPayment()->getAllAttributes(); unset($transaction_fields["payment"]); $params = array_merge($transaction_fields, $card_fields); - $params["amount"] = (string) number_format( (float) $params["amount"], 2, '.', ''); + $params["amount"] = (string)number_format((float)$params["amount"], 2, '.', ''); // Remove Null and Update Booleans - foreach( $params as $attr => $value ){ - if($value == null) + foreach ($params as $attr => $value) { + if ($value == null) { unset($params[$attr]); - else if(is_bool($params[$attr])){ - if($params[$attr]) - $params[$attr] = 'true'; - else - $params[$attr] = 'false'; + } else { + if (is_bool($params[$attr])) { + if ($params[$attr]) { + $params[$attr] = 'true'; + } else { + $params[$attr] = 'false'; + } + } } } - $remote_url = self::getApiUrl().'transactions'; - return http_helper::post(self::getUsername(),self::getPassword(),$remote_url,$params); + $remote_url = self::getApiUrl() . 'transactions'; + return HttpHelper::post(self::getUsername(), self::getPassword(), $remote_url, $params); } } \ No newline at end of file diff --git a/src/models/BaseModel.php b/src/models/BaseModel.php new file mode 100644 index 0000000..e8f44de --- /dev/null +++ b/src/models/BaseModel.php @@ -0,0 +1,11 @@ + $value) { + $methodName = "set" . $attribute; + $this->$methodName($value); + } + } + + public function getCvv() + { + return $this->card_cvv; + } + + public function setCvv($cvv) + { + $this->card_cvv = $cvv; + } + + public function getNumber() + { + return $this->card_number; + } + + public function setNumber($number) + { + $this->card_number = $number; + } + + public function getExpiry() + { + return $this->card_expiry; + } + + public function setExpiry($expiry) + { + $this->card_expiry = $expiry; + } + + public function getHolder() + { + return $this->card_holder; + } + + public function setHolder($holder) + { + $this->card_holder = $holder; + } + + public function getType() + { + return $this->card_type; + } + + public function setType($type) + { + $this->card_type = $type; + } + + public function getAllAttributes() + { + return get_object_vars($this); + } +} \ No newline at end of file diff --git a/src/models/base_model.php b/src/models/base_model.php deleted file mode 100644 index 3f26392..0000000 --- a/src/models/base_model.php +++ /dev/null @@ -1,9 +0,0 @@ - $value) - { - $methodName = "set" . $attribute; - $this->$methodName($value); - } - } - - public function getCvv(){ - return $this->card_cvv; - } - - public function setCvv($cvv){ - $this->card_cvv = $cvv; - } - - public function getNumber(){ - return $this->card_number; - } - - public function setNumber($number){ - $this->card_number = $number; - } - - public function getExpiry(){ - return $this->card_expiry; - } - - public function setExpiry($expiry){ - $this->card_expiry = $expiry; - } - - public function getHolder(){ - return $this->card_holder; - } - - public function setHolder($holder){ - $this->card_holder = $holder; - } - - public function getType(){ - return $this->card_type; - } - - public function setType($type){ - $this->card_type = $type; - } - - public function getAllAttributes(){ - return get_object_vars($this); - } -} \ No newline at end of file diff --git a/src/models/transaction.php b/src/models/transaction.php index 417838b..f274987 100644 --- a/src/models/transaction.php +++ b/src/models/transaction.php @@ -1,188 +1,229 @@ - $value) - { - $methodName = "set" . $attribute; - $this->$methodName($value); - } - - if(!isset($arguments["MerchantId"])) - $this->setMerchantId( configuration::$app_settings['username'] ); - - if(!isset($arguments["Hash"])) - $this->setHash(); - } - - public function getMerchantId(){ - return $this->merchant_id; - } - - public function setMerchantId($merchantId){ - $this->merchant_id = $merchantId; - } - - public function getAmount(){ - return $this->amount; - } - - public function setAmount($amount){ - $this->amount = $amount; - } - - public function getPaymentRef(){ - return $this->payment_ref; - } - - public function setPaymentRef($paymentRef){ - $this->payment_ref = $paymentRef; - } - - - public function getPayment(){ - return $this->payment; - } - - public function setPayment($payment){ - $this->payment = $payment; - } - - public function getTest(){ - return $this->test; - } - - public function setTest($test){ - $this->test = $test; - } - - public function getMetadata(){ - return $this->metadata; - } - - public function setMetadata($metadata){ - $this->metadata = $metadata; - } - - public function getCurrency(){ - return $this->currency; - } - - public function setCurrency($currency){ - $this->currency = $currency; - } - - public function getStoreCard(){ - return $this->store_card; - } - - public function setStoreCard($storeCard){ - $this->store_card = $storeCard; - } - - public function getPlanId(){ - return $this->plan_id; - } - - public function setPlanId($planId){ - $this->plan_id = $planId; - } - - public function getCustomerRef(){ - return $this->customer_ref; - } - - public function setCustomerRef($customerRef){ - $this->customer_ref = $customerRef; - } - - public function getHash(){ - return $this->hash; - } - - public function setHash($options=array("secret" => null, "algorithm" => null)){ - if(!$options["secret"]) $secret = configuration::$app_settings['secret']; - if(!$options["algorithm"]) $algorithm = configuration::$app_settings['algorithm']; - $this->hash = $this->calculateHash($secret, $algorithm); - } - - public function getWebhook(){ - return $this->webhook; - } - - public function setWebhook($webhook){ - $this->webhook = $webhook; - } - - public function getEncrypted(){ - return $this->encrypted; - } - - public function setEncrypted($encrypted){ - $this->encrypted = $encrypted; - } - - public function getProcess(){ - return $this->process; - } - - public function setProcess($process){ - $this->process = $process; - } - - public function getSuccessUrl(){ - return $this->success_url; - } - - public function setSuccessUrl($successUrl){ - $this->success_url = $successUrl; - } - - public function getErrorUrl(){ - return $this->error_url; - } - - public function setErrorUrl($errorUrl){ - $this->error_url = $errorUrl; - } - - # Custom - - private function calculateHash($secret, $algorithm){ - $recipe = (string) $this->getMerchantId(); - $recipe .= (string )$this->getPaymentRef(); - $recipe .= (string) $this->getCustomerRef(); - $recipe .= (string) number_format( (float) $this->getAmount(), 2, '.', ''); - $recipe .= (string) $this->getCurrency(); - $recipe .= ($this->getTest()) ? "test" : ""; - $recipe .= $secret; - - return $algorithm($recipe); - } - - public function getAllAttributes(){ - return get_object_vars($this); - } + $value) { + $methodName = "set" . $attribute; + $this->$methodName($value); + } + + if (!isset($arguments["MerchantId"])) { + $this->setMerchantId(Configuration::$app_settings['username']); + } + + if (!isset($arguments["Hash"])) { + $this->setHash(); + } + } + + public function getMerchantId() + { + return $this->merchant_id; + } + + public function setMerchantId($merchantId) + { + $this->merchant_id = $merchantId; + } + + public function getAmount() + { + return $this->amount; + } + + public function setAmount($amount) + { + $this->amount = $amount; + } + + public function getPaymentRef() + { + return $this->payment_ref; + } + + public function setPaymentRef($paymentRef) + { + $this->payment_ref = $paymentRef; + } + + + public function getPayment() + { + return $this->payment; + } + + public function setPayment($payment) + { + $this->payment = $payment; + } + + public function getTest() + { + return $this->test; + } + + public function setTest($test) + { + $this->test = $test; + } + + public function getMetadata() + { + return $this->metadata; + } + + public function setMetadata($metadata) + { + $this->metadata = $metadata; + } + + public function getCurrency() + { + return $this->currency; + } + + public function setCurrency($currency) + { + $this->currency = $currency; + } + + public function getStoreCard() + { + return $this->store_card; + } + + public function setStoreCard($storeCard) + { + $this->store_card = $storeCard; + } + + public function getPlanId() + { + return $this->plan_id; + } + + public function setPlanId($planId) + { + $this->plan_id = $planId; + } + + public function getCustomerRef() + { + return $this->customer_ref; + } + + public function setCustomerRef($customerRef) + { + $this->customer_ref = $customerRef; + } + + public function getHash() + { + return $this->hash; + } + + public function setHash($options = array("secret" => null, "algorithm" => null)) + { + if (!$options["secret"]) { + $secret = Configuration::$app_settings['secret']; + } + if (!$options["algorithm"]) { + $algorithm = Configuration::$app_settings['algorithm']; + } + $this->hash = $this->calculateHash($secret, $algorithm); + } + + public function getWebhook() + { + return $this->webhook; + } + + public function setWebhook($webhook) + { + $this->webhook = $webhook; + } + + public function getEncrypted() + { + return $this->encrypted; + } + + public function setEncrypted($encrypted) + { + $this->encrypted = $encrypted; + } + + public function getProcess() + { + return $this->process; + } + + public function setProcess($process) + { + $this->process = $process; + } + + public function getSuccessUrl() + { + return $this->success_url; + } + + public function setSuccessUrl($successUrl) + { + $this->success_url = $successUrl; + } + + public function getErrorUrl() + { + return $this->error_url; + } + + public function setErrorUrl($errorUrl) + { + $this->error_url = $errorUrl; + } + + # Custom + + private function calculateHash($secret, $algorithm) + { + $recipe = (string)$this->getMerchantId(); + $recipe .= (string )$this->getPaymentRef(); + $recipe .= (string)$this->getCustomerRef(); + $recipe .= (string)number_format((float)$this->getAmount(), 2, '.', ''); + $recipe .= (string)$this->getCurrency(); + $recipe .= ($this->getTest()) ? "test" : ""; + $recipe .= $secret; + + return $algorithm($recipe); + } + + public function getAllAttributes() + { + return get_object_vars($this); + } } \ No newline at end of file diff --git a/src/mondido_sdk.php b/src/mondido_sdk.php deleted file mode 100644 index 60f8037..0000000 --- a/src/mondido_sdk.php +++ /dev/null @@ -1,51 +0,0 @@ - 'https://api.mondido.com/v1/', - "username" => '148', - "password" => '', - "secret" => '', - "algorithm" => 'md5' +class Configuration +{ + public static $app_settings = array( + "api_url" => 'https://api.mondido.com/v1/', + "username" => '148', + "password" => '', + "secret" => '', + "algorithm" => 'md5' ); } \ No newline at end of file diff --git a/test/test_base.php b/test/test_base.php deleted file mode 100644 index 0f6a4ab..0000000 --- a/test/test_base.php +++ /dev/null @@ -1,24 +0,0 @@ -assertEquals('https://api.mondido.com/v1/', $url); } public function testPassword() { echo "Testing api_base::getPassword()\n"; - $val = mondido\api\api_base::getPassword(); + $val = \Mondido\Api\ApiBase::getPassword(); $this->assertEquals('custom00', $val); } public function testUsername() { echo "Testing api_base::getUsername()\n"; - $val = mondido\api\api_base::getUsername(); + $val = \Mondido\Api\ApiBase::getUsername(); $this->assertEquals('3', $val); } diff --git a/test/api_refund_Test.php b/tests/ApiRefundTest.php similarity index 69% rename from test/api_refund_Test.php rename to tests/ApiRefundTest.php index 1b549f9..23e4968 100644 --- a/test/api_refund_Test.php +++ b/tests/ApiRefundTest.php @@ -1,8 +1,8 @@ $ref, "currency" => "eur", "test" => "true", - "hash" => md5(configuration::$app_settings['username'].$ref."10.00".configuration::$app_settings['secret']) + "hash" => md5(Configuration::$app_settings['username'] . $ref . "10.00" . Configuration::$app_settings['secret']) ); echo "Testing refund, setting up a transaction\n"; - self::$trans = transaction::create($payment); + self::$trans = Transaction::create($payment); $data = array( "transaction_id" => self::$trans['id'], "amount" => "10.00", "reason" => "oops" ); echo "Testing refund, setting up a refund\n"; - self::$refund = refund::create($data); + self::$refund = Refund::create($data); } - public function testGetRefund(){ + public function testGetRefund() + { echo "Testing refund::get\n"; - $res = refund::get(self::$refund['id']); + $res = Refund::get(self::$refund['id']); $this->assertEquals($res['id'], self::$refund['id']); } - public function testCreateRefund(){ + public function testCreateRefund() + { echo "Testing refund::create\n"; $ref = rand(10, 100000); @@ -62,7 +63,7 @@ public function testCreateRefund(){ "reason" => $ref ); - $res = refund::create($data); + $res = Refund::create($data); $this->assertEquals($res['reason'], $ref); } diff --git a/test/api_stored_card_Test.php b/tests/ApiStoredCardTest.php similarity index 71% rename from test/api_stored_card_Test.php rename to tests/ApiStoredCardTest.php index c84ab86..f06912a 100644 --- a/test/api_stored_card_Test.php +++ b/tests/ApiStoredCardTest.php @@ -1,6 +1,6 @@ "true" ); echo "Testing stored_card, setting up a stored_card\n"; - self::$card = stored_card::create($data); + self::$card = StoredCard::create($data); } - public function testGetStoredcard(){ + public function testGetStoredcard() + { echo "Testing stored_card::get\n"; - $res = stored_card::get(self::$card['id']); + $res = StoredCard::get(self::$card['id']); print_r($res); $this->assertEquals($res['id'], self::$card['id']); } - public function testGetStoredcardsLimitOffset(){ + public function testGetStoredcardsLimitOffset() + { echo "Testing stored_card::index\n"; - $res = stored_card::index(2,0); + $res = StoredCard::index(2, 0); print_r($res); $this->assertEquals(2, count($res)); } - public function testDeleteStoredcards(){ + public function testDeleteStoredcards() + { echo "Testing stored_card::delete\n"; - $res = stored_card::delete(self::$card['id']); + $res = StoredCard::delete(self::$card['id']); print_r($res); $this->assertEquals('deleted', $res['status']); } - public function testCreateStoredCard(){ + public function testCreateStoredCard() + { echo "Testing stored_card::create\n"; $ref = rand(10, 100000); @@ -68,7 +71,7 @@ public function testCreateStoredCard(){ "test" => "true" ); - $res = stored_card::create($data); + $res = StoredCard::create($data); print_r($res); $this->assertEquals($res['card_holder'], $ref); diff --git a/test/api_transaction_Test.php b/tests/ApiTransactionTest.php similarity index 69% rename from test/api_transaction_Test.php rename to tests/ApiTransactionTest.php index 397be9b..6948364 100644 --- a/test/api_transaction_Test.php +++ b/tests/ApiTransactionTest.php @@ -7,41 +7,44 @@ * To change this template use File | Settings | File Templates. */ -namespace mondido\test; -use mondido\api\transaction; -use mondido\settings\configuration; -use mondido\models\transaction as transaction_model; -use mondido\models\credit_card; +use Mondido\Api\Transaction; +use Mondido\Settings\Configuration; +use Mondido\Models\Transaction as transaction_model; +use Mondido\Models\CreditCard; -require_once(dirname(__FILE__) . '/test_base.php'); +require('TestBase.php'); -class api_transaction_Test extends test_base { +class ApiTransactionTest extends TestBase +{ - public function testGetTransaction(){ + public function testGetTransaction() + { echo "Testing transaction::get\n"; $tid = 29621; - $transaction = transaction::get($tid); + $transaction = Transaction::get($tid); print_r($transaction); $this->assertEquals($tid, $transaction['id']); } - public function testGetTransactionsLimitOffset(){ + public function testGetTransactionsLimitOffset() + { echo "Testing transaction::index\n"; - $transactions = transaction::index(10,0); + $transactions = Transaction::index(10, 0); print_r($transactions); $this->assertEquals(10, count($transactions)); } - public function testCreateTransaction(){ + public function testCreateTransaction() + { echo "Testing transaction::create\n"; - $ref = "MyOrderId" . (string) rand(10, 100000); + $ref = "MyOrderId" . (string)rand(10, 100000); $transaction = new transaction_model(array( #"MerchantId" => "", "Amount" => 10, "PaymentRef" => $ref, - "Payment" => new credit_card(array( + "Payment" => new CreditCard(array( "Holder" => "PHP SDK Test", "Cvv" => "200", "Expiry" => "0116", @@ -67,7 +70,7 @@ public function testCreateTransaction(){ "ErrorUrl" => "" )); - $response = transaction::create($transaction); + $response = Transaction::create($transaction); print_r($response); $this->assertEquals($ref, $response['payment_ref']); diff --git a/tests/TestBase.php b/tests/TestBase.php new file mode 100644 index 0000000..76279ab --- /dev/null +++ b/tests/TestBase.php @@ -0,0 +1,24 @@ + Date: Tue, 21 Jul 2015 16:32:13 +0200 Subject: [PATCH 03/36] Added a helper to convert strings to snake_case to ensure compatibility with the api even if the user makes a mistake --- src/helpers.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/helpers.php diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..41d5a0e --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,13 @@ + Date: Tue, 21 Jul 2015 16:34:39 +0200 Subject: [PATCH 04/36] Made the API base have the username, password, and secret attributes in the constructor, so that it is available to all our api classes, and referenced the change in the api classes. Also made all methods non-static as to work with with the new concept of a non-static SDK, with the Mondido object being instantiated from the beginning. --- src/api/ApiBase.php | 40 ++++++++++++++++++++++++++++++++-------- src/api/StoredCard.php | 26 +++++++++++++------------- src/api/refund.php | 13 ++++++------- src/api/transaction.php | 31 +++++++++++++++---------------- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/api/ApiBase.php b/src/api/ApiBase.php index 451aa09..2c75768 100644 --- a/src/api/ApiBase.php +++ b/src/api/ApiBase.php @@ -13,18 +13,42 @@ class ApiBase { - public static function getApiUrl() - { - return Configuration::$app_settings['api_url']; - } - public static function getUsername() + protected $username; + protected $password; + protected $secret; + protected $apiUrl; + + /** + * StoredCard constructor. + * @param $username + * @param $password + * @param $apiUrl + */ + public function __construct($username, $password, $secret, $apiUrl) { - return Configuration::$app_settings['username']; + $this->username = $username; + $this->password = $password; + $this->secret = $secret; + $this->apiUrl = $apiUrl; } - public static function getPassword() + public function endpoint($url) { - return Configuration::$app_settings['password']; + return $this->apiUrl . $url; } +// public static function getApiUrl() +// { +// return Configuration::$app_settings['api_url']; +// } +// +// public static function getUsername() +// { +// return Configuration::$app_settings['username']; +// } +// +// public static function getPassword() +// { +// return Configuration::$app_settings['password']; +// } } \ No newline at end of file diff --git a/src/api/StoredCard.php b/src/api/StoredCard.php index 821b9ae..73e1f96 100644 --- a/src/api/StoredCard.php +++ b/src/api/StoredCard.php @@ -12,29 +12,29 @@ */ class StoredCard extends ApiBase { - //returns a sc - public static function get($id) + + public function get($id) { - $remote_url = self::getApiUrl() . 'stored_cards/' . $id; - return HttpHelper::get(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('stored_cards/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); } - public static function index($limit, $offset) + public function index($limit, $offset) { - $remote_url = self::getApiUrl() . 'stored_cards/?limit=' . $limit . '&offset=' . $offset; - return HttpHelper::get(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('stored_cards/?limit=' . $limit . '&offset=' . $offset); + return HttpHelper::get($this->username, $this->password, $remote_url); } - public static function create($card) + public function create($card) { - $remote_url = self::getApiUrl() . 'stored_cards'; - return HttpHelper::post(self::getUsername(), self::getPassword(), $remote_url, $card); + $remote_url = $this->endpoint('stored_cards'); + return HttpHelper::post($this->username, $this->password, $remote_url, $card); } - public static function delete($id) + public function delete($id) { - $remote_url = self::getApiUrl() . 'stored_cards/' . $id; - return HttpHelper::delete(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('stored_cards/' . $id); + return HttpHelper::delete($this->username, $this->password, $remote_url); } } \ No newline at end of file diff --git a/src/api/refund.php b/src/api/refund.php index a0179e7..a0154cb 100644 --- a/src/api/refund.php +++ b/src/api/refund.php @@ -11,16 +11,15 @@ */ class Refund extends ApiBase { - - public static function get($id) + public function get($id) { - $remote_url = self::getApiUrl() . 'refunds/' . $id; - return HttpHelper::get(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('refunds/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); } - public static function create($data) + public function create($data) { - $remote_url = self::getApiUrl() . 'refunds'; - return HttpHelper::post(self::getUsername(), self::getPassword(), $remote_url, $data); + $remote_url = $this->endpoint('refunds'); + return HttpHelper::post($this->username, $this->password, $remote_url, $data); } } \ No newline at end of file diff --git a/src/api/transaction.php b/src/api/transaction.php index 29badda..b4d00c1 100644 --- a/src/api/transaction.php +++ b/src/api/transaction.php @@ -3,34 +3,33 @@ use Mondido\HttpHelper; -/** - * Created by JetBrains PhpStorm. - * User: robertpohl - * Date: 12/06/14 - * Time: 13:48 - * To change this template use File | Settings | File Templates. - */ class Transaction extends ApiBase { //returns a transaction - public static function get($id) + public function get($id) { - $remote_url = self::getApiUrl() . 'transactions/' . $id; - return HttpHelper::get(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('transactions/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); } /* * list transactions with a offset and a limit */ - public static function index($limit, $offset) + public function index($limit, $offset) { - $remote_url = self::getApiUrl() . 'transactions/?limit=' . $limit . '&offset=' . $offset; - return HttpHelper::get(self::getUsername(), self::getPassword(), $remote_url); + $remote_url = $this->endpoint('transactions/?limit=' . $limit . '&offset=' . $offset); + return HttpHelper::get($this->username, $this->password, $remote_url); } - public static function create(\Mondido\Models\Transaction $transaction) + public function create($transaction) { + // Make a check to see if we passed in an actual transaction, or just values for the transaction + if (!($transaction instanceof \Mondido\Models\Transaction)) { + $transaction = new \Mondido\Models\Transaction($this->username, $this->secret, $transaction); + } + $transaction_fields = $transaction->getAllAttributes(); +// $card_fields = $transaction->getPayment()->getAllAttributes(); // <-- We can't call a method on an array. getPayment returns an array(or previously nothing) $card_fields = $transaction->getPayment()->getAllAttributes(); unset($transaction_fields["payment"]); @@ -52,8 +51,8 @@ public static function create(\Mondido\Models\Transaction $transaction) } } - $remote_url = self::getApiUrl() . 'transactions'; - return HttpHelper::post(self::getUsername(), self::getPassword(), $remote_url, $params); + $remote_url = $this->endpoint('transactions'); + return HttpHelper::post($this->username, $this->password, $remote_url, $params); } } \ No newline at end of file From 03e8364170551db5aecf346bc026d1370cc1136c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:37:15 +0200 Subject: [PATCH 05/36] Made the Mondido object useful by letting it be instantiated to access the api with a given username, password and secret. Also added some helper methods which allows the user to access a few of the private attributes on the instance. Also added access methods to access the different apis: transaction, refund and storedCard --- src/Mondido.php | 72 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/src/Mondido.php b/src/Mondido.php index c7fda2c..aff0abf 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -1,29 +1,73 @@ setIfExists('username', $username); + $this->setIfExists('password', $password); + $this->setIfExists('secret', $secret); + $this->setIfExists('apiUrl', $apiUrl); + $this->setIfExists('algorithm', $algorithm); + } + + private function setIfExists($attribute, $value) + { + if ($value) { + $this->$attribute = $value; + } else { + $this->$attribute = Configuration::$app_settings[snakify($attribute)]; + } + } + + public function refund() + { + return new Refund($this->username, $this->password, $this->secret, $this->apiUrl); + } + + public function storedCard() + { + return new storedCard($this->username, $this->password, $this->secret, $this->apiUrl); + } + + public function transaction() + { + return new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); + } + + public function getApiUrl() + { + return $this->apiUrl; + } + + public function getPassword() + { + return $this->password; + } + + public function getUsername() { + return $this->username; } + public function getSecret() + { + return $this->secret; + } /* *Log data to file From e82e2eaa0897c59c59aea1f51c3d78cb8930878e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:39:14 +0200 Subject: [PATCH 06/36] Added the attributes as arrays instead, with a secondary array for allowed attributes to ensure that we don't get any data which shouldn't be there. When creating a credit card, you can now also pass aliases, e.g 'cvv' instead of 'card_cvv' --- src/models/CreditCard.php | 57 ++++++++++++++--- src/models/transaction.php | 124 ++++++++++++++++++++++++++++--------- 2 files changed, 143 insertions(+), 38 deletions(-) diff --git a/src/models/CreditCard.php b/src/models/CreditCard.php index acc2fff..4dfd4e1 100644 --- a/src/models/CreditCard.php +++ b/src/models/CreditCard.php @@ -4,22 +4,63 @@ class CreditCard extends BaseModel { - private $card_cvv; - private $card_number; - private $card_expiry; - private $card_holder; - private $card_type; +// private $card_cvv; +// private $card_number; +// private $card_expiry; +// private $card_holder; +// private $card_type; + + protected $attributes = array(); + + protected $allowedAttributes = array( + 'card_cvv', + 'card_number', + 'card_expiry', + 'card_holder', + 'card_type', + ); + + protected $aliases = array( + 'cvv', + 'number', + 'expiry', + 'holder', + 'type', + ); public function __construct($arguments) { parent::__construct(); foreach ($arguments as $attribute => $value) { - $methodName = "set" . $attribute; - $this->$methodName($value); + $attribute = snakify($attribute); + + $aliasedAttributes = array_combine($this->aliases, $this->allowedAttributes); + + if (in_array($attribute, $this->aliases)) { + // The user passed an alias instead of the fully qualified attribute name, so we'll convert it + $attribute = $aliasedAttributes[$attribute]; + } + + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } } } + /** + * is utilized for reading data from inaccessible members. + * + * @param $name string + * @return mixed + * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members + */ + public function __get($name) + { + return $this->attributes[snakify($name)]; + } + + public function getCvv() { return $this->card_cvv; @@ -72,6 +113,6 @@ public function setType($type) public function getAllAttributes() { - return get_object_vars($this); + return $this->attributes; } } \ No newline at end of file diff --git a/src/models/transaction.php b/src/models/transaction.php index f274987..55afb1e 100644 --- a/src/models/transaction.php +++ b/src/models/transaction.php @@ -4,41 +4,105 @@ class Transaction extends BaseModel { - private $merchant_id; - private $amount; - private $payment_ref; - private $payment; - private $test; - private $metadata; - private $currency; - private $store_card; - private $plan_id; - private $customer_ref; - private $hash; - private $webhook; - private $encrypted; - private $process; - private $success_url; - private $error_url; - - public function __construct($arguments) +// private $merchant_id; +// private $amount; +// private $payment_ref; +// private $payment; +// private $test; +// private $metadata; +// private $currency; +// private $store_card; +// private $plan_id; +// private $customer_ref; +// private $hash; +// private $webhook; +// private $encrypted; +// private $process; +// private $success_url; +// private $error_url; + private $attributes = array(); + + protected $allowedAttributes = [ + 'merchant_id', + 'amount', + 'payment_ref', + 'payment', + 'test', + 'metadata', + 'currency', + 'store_card', + 'plan_id', + 'customer_ref', + 'hash', + 'webhook', + 'encrypted', + 'process', + 'success_url', + 'error_url', + ]; + + private $cardAttributes = [ + "card_number", + "card_holder", + "card_expiry", + "card_cvv", + "card_type", + ]; + + public function __construct($merchantId, $secret, $arguments, $paymentRef = null, $customerRef = null) { parent::__construct(); + $this->attributes['merchant_id'] = $merchantId; + $this->attributes['payment_ref'] = $paymentRef; + $this->attributes['customer_ref'] = $customerRef; + $this->attributes['secret'] = $secret; + + $payment = []; + foreach ($arguments as $attribute => $value) { - $methodName = "set" . $attribute; - $this->$methodName($value); +// $methodName = "set" . $attribute; +// $this->$methodName($value); + $attribute = snakify($attribute); + if(in_array($attribute, $this->cardAttributes)) { + $payment[$attribute] = $value; + } + + if(in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } } - if (!isset($arguments["MerchantId"])) { - $this->setMerchantId(Configuration::$app_settings['username']); + if (! ($this->paymentRef && $this->customerRef)) { + throw new \Exception("You must provide both a payment reference and a customer reference to make a transaction"); } - if (!isset($arguments["Hash"])) { - $this->setHash(); + if (!empty($payment) && is_array($payment)) { + $this->attributes['payment'] = new CreditCard($payment); + } + +// if (!isset($arguments["MerchantId"])) { +// $this->setMerchantId(Configuration::$app_settings['username']); +// } + + if (!isset($arguments["hash"])) { + $this->setHash($secret); } } + /** + * is utilized for reading data from inaccessible members. + * + * @param $name string + * @return mixed + * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members + */ + public function __get($name) + { + return $this->attributes[snakify($name)]; + } + + public function getMerchantId() { return $this->merchant_id; @@ -69,7 +133,6 @@ public function setPaymentRef($paymentRef) $this->payment_ref = $paymentRef; } - public function getPayment() { return $this->payment; @@ -145,15 +208,15 @@ public function getHash() return $this->hash; } - public function setHash($options = array("secret" => null, "algorithm" => null)) + public function setHash($secret, $algorithm = null) { - if (!$options["secret"]) { + if (!$secret) { $secret = Configuration::$app_settings['secret']; } - if (!$options["algorithm"]) { + if (!$algorithm) { $algorithm = Configuration::$app_settings['algorithm']; } - $this->hash = $this->calculateHash($secret, $algorithm); + $this->attributes['hash'] = $this->calculateHash($secret, $algorithm); } public function getWebhook() @@ -223,7 +286,8 @@ private function calculateHash($secret, $algorithm) public function getAllAttributes() { - return get_object_vars($this); +// return call_user_func('get_object_vars', $this); + return $this->attributes; } } \ No newline at end of file From b6490364f7cafd324b057528ef66f6dd3534df82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:39:42 +0200 Subject: [PATCH 07/36] updated composer to autoload our base testcase in dev, and to always autoload the helper file --- composer.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5f91565..296addc 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,14 @@ "autoload": { "psr-4": { "Mondido\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] + }, + "autoload-dev": { + "classmap": [ + "tests/TestBase.php" + ] } } From 59104388ffdeccf34a52410e820639bbebd46566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:40:10 +0200 Subject: [PATCH 08/36] Explicitly exclude the test base in phpunit --- phpunit.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml b/phpunit.xml index 775e8a5..72c7dd7 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -15,6 +15,7 @@ ./tests + /tests/TestBase.php From 4770320a5a5a16be2bca06b1d2a4151b9a22d63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:41:22 +0200 Subject: [PATCH 09/36] Converted tests to use the new api and made some logical and syntactical corrections --- tests/ApiBaseTest.php | 6 +++--- tests/ApiRefundTest.php | 28 ++++++++++++++-------------- tests/ApiStoredCardTest.php | 19 +++++++++---------- tests/ApiTransactionTest.php | 33 +++++++++++++++++++++++---------- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/tests/ApiBaseTest.php b/tests/ApiBaseTest.php index d2b5ba3..ad9ac79 100644 --- a/tests/ApiBaseTest.php +++ b/tests/ApiBaseTest.php @@ -16,21 +16,21 @@ public function testGetApiUrl() { echo "Testing api_base::getApiUrl()\n"; - $url = \Mondido\Api\ApiBase::getApiUrl(); + $url = $this->api->getApiUrl(); $this->assertEquals('https://api.mondido.com/v1/', $url); } public function testPassword() { echo "Testing api_base::getPassword()\n"; - $val = \Mondido\Api\ApiBase::getPassword(); + $val = $this->api->getPassword(); $this->assertEquals('custom00', $val); } public function testUsername() { echo "Testing api_base::getUsername()\n"; - $val = \Mondido\Api\ApiBase::getUsername(); + $val = $this->api->getUsername(); $this->assertEquals('3', $val); } diff --git a/tests/ApiRefundTest.php b/tests/ApiRefundTest.php index 23e4968..93d888b 100644 --- a/tests/ApiRefundTest.php +++ b/tests/ApiRefundTest.php @@ -10,16 +10,16 @@ * Time: 23:37 * To change this template use File | Settings | File Templates. */ -require('TestBase.php'); class ApiRefundTest extends TestBase { - public static $refund; - public static $trans; + public $refund; + public $trans; - public static function setUpBeforeClass() + protected function setUp() { + parent::setUp(); $ref = rand(10, 100000); $payment = array( "card_number" => "4111111111111111", @@ -27,29 +27,30 @@ public static function setUpBeforeClass() "card_expiry" => "0116", "card_cvv" => "200", "card_type" => "VISA", - "amount" => "10.00", + "amount" => "20.00", + "customer_ref" => 1, "payment_ref" => $ref, "currency" => "eur", "test" => "true", - "hash" => md5(Configuration::$app_settings['username'] . $ref . "10.00" . Configuration::$app_settings['secret']) +// "hash" => md5($this->api->getUsername() . $ref . "10.00" . $this->api->getSecret()) ); echo "Testing refund, setting up a transaction\n"; - self::$trans = Transaction::create($payment); + $this->trans = $this->api->transaction()->create($payment); $data = array( - "transaction_id" => self::$trans['id'], + "transaction_id" => $this->trans['id'], "amount" => "10.00", "reason" => "oops" ); echo "Testing refund, setting up a refund\n"; - self::$refund = Refund::create($data); + $this->refund = $this->api->refund()->create($data); } public function testGetRefund() { echo "Testing refund::get\n"; - $res = Refund::get(self::$refund['id']); - $this->assertEquals($res['id'], self::$refund['id']); + $res = $this->api->refund()->get($this->refund['id']); + $this->assertEquals($res['id'], $this->refund['id']); } public function testCreateRefund() @@ -58,13 +59,12 @@ public function testCreateRefund() $ref = rand(10, 100000); $data = array( - "transaction_id" => self::$trans['id'], + "transaction_id" => $this->trans['id'], "amount" => "10.00", "reason" => $ref ); - $res = Refund::create($data); - + $res = $this->api->refund()->create($data); $this->assertEquals($res['reason'], $ref); } diff --git a/tests/ApiStoredCardTest.php b/tests/ApiStoredCardTest.php index f06912a..aea7cc5 100644 --- a/tests/ApiStoredCardTest.php +++ b/tests/ApiStoredCardTest.php @@ -8,15 +8,15 @@ * Time: 23:37 * To change this template use File | Settings | File Templates. */ -require('TestBase.php'); class ApiStoredCardTest extends TestBase { - public static $card; + private $card; - public static function setUpBeforeClass() + protected function setUp() { + parent::setUp(); $data = array( "card_number" => "4111111111111111", "card_holder" => "php sdk", @@ -26,23 +26,22 @@ public static function setUpBeforeClass() "currency" => "eur", "test" => "true" ); - echo "Testing stored_card, setting up a stored_card\n"; - self::$card = StoredCard::create($data); + $this->card = $this->api->storedCard()->create($data); } public function testGetStoredcard() { echo "Testing stored_card::get\n"; - $res = StoredCard::get(self::$card['id']); + $res = $this->api->storedCard()->get($this->card['id']); print_r($res); - $this->assertEquals($res['id'], self::$card['id']); + $this->assertEquals($res['id'], $this->card['id']); } public function testGetStoredcardsLimitOffset() { echo "Testing stored_card::index\n"; - $res = StoredCard::index(2, 0); + $res = $this->api->storedCard()->index(2, 0); print_r($res); $this->assertEquals(2, count($res)); } @@ -50,7 +49,7 @@ public function testGetStoredcardsLimitOffset() public function testDeleteStoredcards() { echo "Testing stored_card::delete\n"; - $res = StoredCard::delete(self::$card['id']); + $res = $this->api->storedCard()->delete($this->card['id']); print_r($res); $this->assertEquals('deleted', $res['status']); @@ -71,7 +70,7 @@ public function testCreateStoredCard() "test" => "true" ); - $res = StoredCard::create($data); + $res = $this->api->storedCard()->create($data); print_r($res); $this->assertEquals($res['card_holder'], $ref); diff --git a/tests/ApiTransactionTest.php b/tests/ApiTransactionTest.php index 6948364..6ed8d96 100644 --- a/tests/ApiTransactionTest.php +++ b/tests/ApiTransactionTest.php @@ -12,7 +12,6 @@ use Mondido\Models\Transaction as transaction_model; use Mondido\Models\CreditCard; -require('TestBase.php'); class ApiTransactionTest extends TestBase { @@ -20,17 +19,31 @@ class ApiTransactionTest extends TestBase public function testGetTransaction() { echo "Testing transaction::get\n"; - $tid = 29621; + $ref = rand(10, 100000); + $testTransaction = $this->api->transaction()->create(array( + "card_number" => "4111111111111111", + "card_holder" => "php sdk", + "card_expiry" => "0116", + "card_cvv" => "200", + "card_type" => "VISA", + "amount" => "20.00", + "customer_ref" => 1, + "payment_ref" => $ref, + "currency" => "eur", + "test" => "true", + )); + $tid = $testTransaction['id']; - $transaction = Transaction::get($tid); + $transaction = $this->api->transaction()->get($tid); print_r($transaction); $this->assertEquals($tid, $transaction['id']); + $this->assertEquals($testTransaction, $transaction); } public function testGetTransactionsLimitOffset() { echo "Testing transaction::index\n"; - $transactions = Transaction::index(10, 0); + $transactions = $this->api->transaction()->index(10, 0); print_r($transactions); $this->assertEquals(10, count($transactions)); } @@ -40,10 +53,10 @@ public function testCreateTransaction() echo "Testing transaction::create\n"; $ref = "MyOrderId" . (string)rand(10, 100000); - $transaction = new transaction_model(array( - #"MerchantId" => "", + $transaction = array( "Amount" => 10, "PaymentRef" => $ref, + "customerRef" => 1, "Payment" => new CreditCard(array( "Holder" => "PHP SDK Test", "Cvv" => "200", @@ -58,8 +71,6 @@ public function testCreateTransaction() "Currency" => "usd", "StoreCard" => true, "PlanId" => '', - #"CustomerRef" => "", - #"Hash" => "", "Webhook" => json_encode(array( "trigger" => "payment_success", "email" => "myname@domain.com" @@ -68,11 +79,13 @@ public function testCreateTransaction() "Process" => true, "SuccessUrl" => "", "ErrorUrl" => "" - )); + ); - $response = Transaction::create($transaction); + $response = $this->api->transaction()->create($transaction); print_r($response); + + $this->assertEquals($ref, $response['payment_ref']); } From b012d171dc65c58fed8274966846701462e35de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:48:07 +0200 Subject: [PATCH 10/36] ignore the json file created during tests --- .gitignore | 1 + wh.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 wh.json diff --git a/.gitignore b/.gitignore index fa97e9a..de02b52 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vendor/ +wh.json diff --git a/wh.json b/wh.json deleted file mode 100644 index 77b8523..0000000 --- a/wh.json +++ /dev/null @@ -1 +0,0 @@ -{"id":443,"created_at":"2013-12-17T13:41:07Z","merchant_id":3,"amount":"480.0","payment_ref":null,"ref":null,"card_holder":"Annika Manns","card_number":"*****************1111","test":false,"metadata":{"user":{"email":"justmyemail@mondido.com"},"products":["awesomeness","coolness"]},"currency":"SEK","status":"approved","card_type":"VISA","transaction_type":null,"template_id":10,"error":null,"cost":{"percentual_fee":0,"fixed_fee":0,"percentual_exchange_fee":0,"total":"0.0"},"success_url":"https:\/\/mondido.com\/payment-ok?status=A","error_url":"https:\/\/mondido.com\/payment-error","stored_card":null,"customer":null,"subscription":null,"refunds":[],"webhooks":[]} \ No newline at end of file From 8213d735d0b2ebfdf190a006c23b229b07242ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:48:31 +0200 Subject: [PATCH 11/36] make the TestBase instantiate the api with given credentials --- tests/TestBase.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/TestBase.php b/tests/TestBase.php index 76279ab..0dc345f 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -21,4 +21,13 @@ class TestBase extends PHPUnit_Framework_TestCase { + protected $api; + + protected function setUp() + { + $merchantId = null; + $password = null; + $secret = null; + $this->api = new \Mondido\Mondido($merchantId, $password, $secret); + } } \ No newline at end of file From c3f45f07b2d37def7e2b2987acd652d7d2d980da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 16:48:49 +0200 Subject: [PATCH 12/36] Make sure we create a new transaction instead of getting a fixed one --- tests/WebhookTest.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/WebhookTest.php b/tests/WebhookTest.php index 140bbd7..ee9e7a9 100644 --- a/tests/WebhookTest.php +++ b/tests/WebhookTest.php @@ -10,7 +10,6 @@ use Mondido\Request\webhook; use Mondido\Api\Transaction; -require('TestBase.php'); class WebhookTest extends TestBase { @@ -21,7 +20,19 @@ class WebhookTest extends TestBase public function testGetwebhook() { echo "Testing webhook::get\n"; - $transaction = Transaction::get(443); + $ref = mt_rand(10, 100000); + $transaction = $this->api->transaction()->create(array( + "card_number" => "4111111111111111", + "card_holder" => "php sdk", + "card_expiry" => "0116", + "card_cvv" => "200", + "card_type" => "VISA", + "amount" => "20.00", + "customer_ref" => 1, + "payment_ref" => $ref, + "currency" => "eur", + "test" => "true", + )); $path = 'wh.json'; file_put_contents($path, json_encode($transaction)); From f2f9f5e64734bdb085a93d7c51abc4d8ff592851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 17:09:49 +0200 Subject: [PATCH 13/36] Updated read and made compliant with PHP 5.3 by using the explicit arrays --- README.md | 49 ++++++++++++++++++++++++++++---------- src/models/transaction.php | 10 ++++---- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e060ac3..77440dd 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,48 @@ -php_sdk 1.2 +#Mondido PHP SDK 2.0 by Snowfire ======= -The PHP SDK for Mondido Payments +To get started with the SDK, simply run `composer require snowfire/mondido-php-sdk` and include the composer-generated `autoload.php`. +Every time you wish to use the api through the SDK, you must first instantiate the `Mondido\Mondido` class with your merchant id, password and secret. If you plan to use this in more than one place, it is recommended to place this instantiation in a constructor, trait or similar. -```php +You can then access the different api features through your main instance. + +## Example + +``` api = new Mondido\Mondido($merchantId, $password, $secret); + } + + public function recordPayment($data) + { + $transaction = $this->api->transaction()->create($data); + } + } ``` +For complete API documentation, please visit [Mondido](https://doc.mondido.com/api). + + The unit tests require PHPUnit to be installed and run `phpunit test/` *Changelog* +- 2.0, Transitioned to non-static api +- 2.0, Refactored all code for PSR-0 +- 2.0, Fixed tests +- 2.0, Bug and syntax fixes - 1.2, Updated Hash recipe, refactor models, etc. \ No newline at end of file diff --git a/src/models/transaction.php b/src/models/transaction.php index 55afb1e..ea8a2ed 100644 --- a/src/models/transaction.php +++ b/src/models/transaction.php @@ -22,7 +22,7 @@ class Transaction extends BaseModel // private $error_url; private $attributes = array(); - protected $allowedAttributes = [ + protected $allowedAttributes = array( 'merchant_id', 'amount', 'payment_ref', @@ -39,15 +39,15 @@ class Transaction extends BaseModel 'process', 'success_url', 'error_url', - ]; + ); - private $cardAttributes = [ + private $cardAttributes = array( "card_number", "card_holder", "card_expiry", "card_cvv", "card_type", - ]; + ); public function __construct($merchantId, $secret, $arguments, $paymentRef = null, $customerRef = null) { @@ -58,7 +58,7 @@ public function __construct($merchantId, $secret, $arguments, $paymentRef = null $this->attributes['customer_ref'] = $customerRef; $this->attributes['secret'] = $secret; - $payment = []; + $payment = array(); foreach ($arguments as $attribute => $value) { // $methodName = "set" . $attribute; From 1eb3fab0e46e4752e61f64b57153c09fa86e0366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 20:35:37 +0200 Subject: [PATCH 14/36] added a method to generate a form that will send a post request to mondido's hosted payment when submitted --- src/Mondido.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Mondido.php b/src/Mondido.php index aff0abf..e976cf4 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -13,6 +13,8 @@ class Mondido private $secret; private $apiUrl; private $algorithm; + + private $hostedWindowUrl = 'https://pay.mondido.com/v1/form'; /* * parse POST JSON data from WebHook */ @@ -34,6 +36,23 @@ private function setIfExists($attribute, $value) } } + public function generatePostForm($payload) + { + $form = '
'; + $form .= ''; + + foreach ($payload as $dataType => $data) { + if (is_array($data)) { + $data = json_encode($data); + } + $form .= ''; + } + + + $form .= '
'; + return $form; + } + public function refund() { return new Refund($this->username, $this->password, $this->secret, $this->apiUrl); From 1f54cebb84f467f87755c325e274da1a32c74d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 20:49:50 +0200 Subject: [PATCH 15/36] Added the javascript for submitting the form --- src/Mondido.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Mondido.php b/src/Mondido.php index e976cf4..cdf1c32 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -38,7 +38,7 @@ private function setIfExists($attribute, $value) public function generatePostForm($payload) { - $form = '
'; + $form = ''; $form .= ''; foreach ($payload as $dataType => $data) { @@ -48,8 +48,15 @@ public function generatePostForm($payload) $form .= ''; } - + $form .= ''; $form .= '
'; + + // The JS to make this work + $form .= + ''; + return $form; } From 79898198a5cf4023387225054f075f17f72ad77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 21 Jul 2015 21:34:28 +0200 Subject: [PATCH 16/36] generate hash for forms. They can also be generated manually by the client --- src/Mondido.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Mondido.php b/src/Mondido.php index cdf1c32..b7bbd87 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -36,8 +36,46 @@ private function setIfExists($attribute, $value) } } + public function generateHash($paymentRef, $customerRef, $amount, $currency, $test = null) + { + $recipe = (string) $this->username; + $recipe .= (string )$paymentRef; + $recipe .= (string)$customerRef; + $recipe .= (string)number_format((float)$amount, 2, '.', ''); + $recipe .= (string)$currency; + $recipe .= $test ? "test" : ""; + $recipe .= $this->secret; + + $algorithm = $this->algorithm; + + return $algorithm($recipe); + } + public function generatePostForm($payload) { + if (!isset($payload['hash'])) { + $test = null; + if (isset($payload['test'])) { + $test = $payload['test']; + } + + $paymentRef = $payload['payment_ref']; + $customerRef = $payload['customer_ref']; + $amount = $payload['amount']; + $currency = $payload['currency']; + + if (! ( + isset ($paymentRef) && + isset ($customerRef) && + isset ($amount) && + isset ($currency) + )) { + throw new \Exception('You need to define payment and customer reference, amount and currency to generate a correct hash'); + } + + $payload['hash'] = $this->generateHash($paymentRef, $customerRef, $amount, $currency, $test); + } + $form = '
'; $form .= ''; @@ -56,7 +94,7 @@ public function generatePostForm($payload) ''; - + return $form; } From e4006522338a16de6c4ac88a7cae83e27af4ed6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Wed, 22 Jul 2015 09:22:12 +0200 Subject: [PATCH 17/36] Make sure we format the amount correctly before placing it in the form --- src/Mondido.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Mondido.php b/src/Mondido.php index b7bbd87..ad1bf72 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -53,6 +53,12 @@ public function generateHash($paymentRef, $customerRef, $amount, $currency, $tes public function generatePostForm($payload) { + if (!isset ($payload['amount'])) { + throw new \Exception('You need to specify an amount'); + } + + $payload['amount'] = number_format((float)$payload['amount'], 2, '.', ''); + if (!isset($payload['hash'])) { $test = null; if (isset($payload['test'])) { From b4d717fd0bcc3eab73ae86a40f3b1fbc23e89075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 11:23:42 +0200 Subject: [PATCH 18/36] Refactored and added the customer api --- src/HttpHelper.php | 23 +++++++++++++ src/api/Customer.php | 69 ++++++++++++++++++++++++++++++++++++++ src/api/transaction.php | 1 - src/models/BaseModel.php | 14 ++++++++ src/models/Customer.php | 33 ++++++++++++++++++ src/models/transaction.php | 6 ---- 6 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src/api/Customer.php create mode 100644 src/models/Customer.php diff --git a/src/HttpHelper.php b/src/HttpHelper.php index 6552d39..86a80d9 100644 --- a/src/HttpHelper.php +++ b/src/HttpHelper.php @@ -45,6 +45,10 @@ public static function post($uname, $pass, $url, $data) ) ); +// $context = stream_context_create($opts); +// $result = file_get_contents($url, false, $context); +// return json_decode($result, true); + $ch = curl_init(); @@ -61,4 +65,23 @@ public static function post($uname, $pass, $url, $data) return json_decode($result, true); } + public static function put($uname, $pass, $url, $data) + { + $body = http_build_query($data); + + $ch = curl_init(); + + //set the url, number of POST vars, POST data + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pass"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + //execute post + $result = curl_exec($ch); + + return json_decode($result, true); + } + } \ No newline at end of file diff --git a/src/api/Customer.php b/src/api/Customer.php new file mode 100644 index 0000000..fb1ad93 --- /dev/null +++ b/src/api/Customer.php @@ -0,0 +1,69 @@ +endpoint('customers/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function index() + { + $remote_url = $this->endpoint('customers'); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function create($customer) + { + if (!($customer instanceof \Mondido\Models\Customer)) { + $customer = new \Mondido\Models\Customer($this->username, $this->secret, $transaction); + } + + $params = $customer->getAllAttributes(); + + foreach ($params as $attr => $value) { + if ($value == null) { + unset($params[$attr]); + } else { + if (is_bool($params[$attr])) { + if ($params[$attr]) { + $params[$attr] = 'true'; + } else { + $params[$attr] = 'false'; + } + } + } + } + + $remote_url = $this->endpoint('customers'); + return HttpHelper::post($this->username, $this->password, $remote_url, $params); + } + + public function update($customer) + { + if (!($customer instanceof \Mondido\Models\Customer)) { + $customer = new \Mondido\Models\Customer($this->username, $this->secret, $customer); + } + + $remote_url = $this->endpoint('customers/' . $customer->id); + return HttpHelper::put($this->username, $this->password, $remote_url, $customer->getAllAttributes()); + } + + public function delete($id) + { + $remote_url = $this->endpoint('customers/' . $id); + return HttpHelper::delete($this->username, $this->password, $remote_url); + } +} \ No newline at end of file diff --git a/src/api/transaction.php b/src/api/transaction.php index b4d00c1..be0c0af 100644 --- a/src/api/transaction.php +++ b/src/api/transaction.php @@ -29,7 +29,6 @@ public function create($transaction) } $transaction_fields = $transaction->getAllAttributes(); -// $card_fields = $transaction->getPayment()->getAllAttributes(); // <-- We can't call a method on an array. getPayment returns an array(or previously nothing) $card_fields = $transaction->getPayment()->getAllAttributes(); unset($transaction_fields["payment"]); diff --git a/src/models/BaseModel.php b/src/models/BaseModel.php index e8f44de..18a3a3a 100644 --- a/src/models/BaseModel.php +++ b/src/models/BaseModel.php @@ -8,4 +8,18 @@ public function __construct() // ... } + public function getAllAttributes() + { + return $this->attributes; + } + + public function __get($attribute) + { + if (isset($this->attributes[$attribute])) { + return $this->attributes[$attribute]; + } + + return null; + } + } diff --git a/src/models/Customer.php b/src/models/Customer.php new file mode 100644 index 0000000..faf70f3 --- /dev/null +++ b/src/models/Customer.php @@ -0,0 +1,33 @@ + $value) { + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } else { + $this->attributes['metadata'][$attribute] = $value; + } + } + + $this->attributes = $attributes; + } + + +} \ No newline at end of file diff --git a/src/models/transaction.php b/src/models/transaction.php index ea8a2ed..957399f 100644 --- a/src/models/transaction.php +++ b/src/models/transaction.php @@ -284,10 +284,4 @@ private function calculateHash($secret, $algorithm) return $algorithm($recipe); } - public function getAllAttributes() - { -// return call_user_func('get_object_vars', $this); - return $this->attributes; - } - } \ No newline at end of file From ba77bbb0e6b9b4a3846bab3f8359ac73b3388658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 12:04:16 +0200 Subject: [PATCH 19/36] use the API parts as variables --- src/Mondido.php | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Mondido.php b/src/Mondido.php index ad1bf72..9f1420b 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -1,6 +1,7 @@ setIfExists('secret', $secret); $this->setIfExists('apiUrl', $apiUrl); $this->setIfExists('algorithm', $algorithm); + + $this->refund = new Refund($this->username, $this->password, $this->secret, $this->apiUrl); + $this->storedCard = new storedCard($this->username, $this->password, $this->secret, $this->apiUrl); + $this->transaction = new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); + $this->customer = new Customer($this->username, $this->password, $this->secret, $this->apiUrl); + } + + public function refund() + { + return new Refund($this->username, $this->password, $this->secret, $this->apiUrl); } + public function storedCard() + { + return new storedCard($this->username, $this->password, $this->secret, $this->apiUrl); + } + + public function transaction() + { + return new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); + } private function setIfExists($attribute, $value) { if ($value) { @@ -104,20 +129,6 @@ public function generatePostForm($payload) return $form; } - public function refund() - { - return new Refund($this->username, $this->password, $this->secret, $this->apiUrl); - } - - public function storedCard() - { - return new storedCard($this->username, $this->password, $this->secret, $this->apiUrl); - } - - public function transaction() - { - return new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); - } public function getApiUrl() { From 00734cc8f76138d9cfb34cc9ba25bb71be6fab7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 16:17:40 +0200 Subject: [PATCH 20/36] added a subscription model --- src/models/Subscription.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/models/Subscription.php diff --git a/src/models/Subscription.php b/src/models/Subscription.php new file mode 100644 index 0000000..4c9afe4 --- /dev/null +++ b/src/models/Subscription.php @@ -0,0 +1,15 @@ + Date: Thu, 13 Aug 2015 16:49:27 +0200 Subject: [PATCH 21/36] Added an all method to subscription and a constructor to the base model --- src/api/Subscription.php | 16 ++++++++++++++++ src/models/BaseModel.php | 15 +++++++++++++-- src/models/Subscription.php | 10 +++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/api/Subscription.php diff --git a/src/api/Subscription.php b/src/api/Subscription.php new file mode 100644 index 0000000..d4412d0 --- /dev/null +++ b/src/api/Subscription.php @@ -0,0 +1,16 @@ +endpoint('subscriptions') . '?' . http_build_query($options); + + return HttpHelper::get($this->username, $this->password, $remote_url); + } +} \ No newline at end of file diff --git a/src/models/BaseModel.php b/src/models/BaseModel.php index 18a3a3a..6b479cd 100644 --- a/src/models/BaseModel.php +++ b/src/models/BaseModel.php @@ -3,9 +3,20 @@ class BaseModel { - public function __construct() + protected $allowedAttributes = array(); + protected $attributes = array(); + + public function __construct(array $attributes) { - // ... + foreach ($attributes as $attribute => $value) { + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } else { + $this->attributes['metadata'][$attribute] = $value; + } + } + + $this->attributes = $attributes; } public function getAllAttributes() diff --git a/src/models/Subscription.php b/src/models/Subscription.php index 4c9afe4..3a779a7 100644 --- a/src/models/Subscription.php +++ b/src/models/Subscription.php @@ -11,5 +11,13 @@ class Subscription extends BaseModel { - + protected $allowedAttributes = array( + 'plan_id', + 'stored_card_id', + 'customer_ref', + 'customer_id', + 'quantity', + 'debt', + 'status', + ); } \ No newline at end of file From e4f309159515cdd71f9b437e8c4bd61da668e6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 17:01:23 +0200 Subject: [PATCH 22/36] Added plans API --- src/api/Plan.php | 16 ++++++++++++++++ src/models/Plan.php | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/api/Plan.php create mode 100644 src/models/Plan.php diff --git a/src/api/Plan.php b/src/api/Plan.php new file mode 100644 index 0000000..2a82cc8 --- /dev/null +++ b/src/api/Plan.php @@ -0,0 +1,16 @@ +endpoint('plans') . '?' . http_build_query($options); + + return HttpHelper::get($this->username, $this->password, $remote_url); + } +} \ No newline at end of file diff --git a/src/models/Plan.php b/src/models/Plan.php new file mode 100644 index 0000000..1306770 --- /dev/null +++ b/src/models/Plan.php @@ -0,0 +1,23 @@ + Date: Thu, 13 Aug 2015 17:33:45 +0200 Subject: [PATCH 23/36] add the subscription and plan to the main api --- src/Mondido.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Mondido.php b/src/Mondido.php index 9f1420b..9caad3e 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -2,8 +2,10 @@ namespace Mondido; use Mondido\Api\Customer; +use Mondido\Api\Plan; use Mondido\Api\Refund; use Mondido\Api\StoredCard; +use Mondido\Api\Subscription; use Mondido\Api\Transaction; use Mondido\Settings\Configuration; @@ -36,6 +38,8 @@ public function __construct($username = null, $password = null, $secret = null, $this->storedCard = new storedCard($this->username, $this->password, $this->secret, $this->apiUrl); $this->transaction = new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); $this->customer = new Customer($this->username, $this->password, $this->secret, $this->apiUrl); + $this->subscription = new Subscription($this->username, $this->password, $this->secret, $this->apiUrl); + $this->plan = new Plan($this->username, $this->password, $this->secret, $this->apiUrl); } public function refund() From 9d70135d353eea1309aaf02afb3a12a442aa3882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 18:45:38 +0200 Subject: [PATCH 24/36] Added an update method to the plan API --- src/api/Plan.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/api/Plan.php b/src/api/Plan.php index 2a82cc8..867ed95 100644 --- a/src/api/Plan.php +++ b/src/api/Plan.php @@ -13,4 +13,11 @@ public function all($options = array()) return HttpHelper::get($this->username, $this->password, $remote_url); } + + public function update($id, array $params) + { + $remote_url = $this->endpoint('plans/'.$id); + + return HttpHelper::put($this->username, $this->password, $remote_url, $params); + } } \ No newline at end of file From ba40a13c9f119148d83a1f1d229e2f8d5d6d3fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 13 Aug 2015 18:54:35 +0200 Subject: [PATCH 25/36] plans can't be updated, subscriptions can... --- src/api/Plan.php | 7 ------- src/api/Subscription.php | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/api/Plan.php b/src/api/Plan.php index 867ed95..2a82cc8 100644 --- a/src/api/Plan.php +++ b/src/api/Plan.php @@ -13,11 +13,4 @@ public function all($options = array()) return HttpHelper::get($this->username, $this->password, $remote_url); } - - public function update($id, array $params) - { - $remote_url = $this->endpoint('plans/'.$id); - - return HttpHelper::put($this->username, $this->password, $remote_url, $params); - } } \ No newline at end of file diff --git a/src/api/Subscription.php b/src/api/Subscription.php index d4412d0..1f2b4df 100644 --- a/src/api/Subscription.php +++ b/src/api/Subscription.php @@ -13,4 +13,11 @@ public function all($options = array()) return HttpHelper::get($this->username, $this->password, $remote_url); } + + public function update($id, array $params) + { + $remote_url = $this->endpoint('subscriptions/'.$id); + + return HttpHelper::put($this->username, $this->password, $remote_url, $params); + } } \ No newline at end of file From 8f872ebe37644db8c3c1e3a67a182f64af1b52f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Fri, 14 Aug 2015 14:17:52 +0200 Subject: [PATCH 26/36] Added a method for easier making http requests to the api by automatically injecting user and pass. Also added a method to retrieve a single subscription --- src/HttpHelper.php | 12 ++++++++++-- src/api/ApiBase.php | 23 +++++++++++++++++++++++ src/api/Subscription.php | 11 ++++++----- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/HttpHelper.php b/src/HttpHelper.php index 86a80d9..79b6129 100644 --- a/src/HttpHelper.php +++ b/src/HttpHelper.php @@ -4,8 +4,12 @@ class HttpHelper { - public static function get($uname, $pass, $url) + public static function get($uname, $pass, $url, array $data = null) { + if ($data) { + $url = $url.'?'.http_build_query($data); + } + $opts = array( 'http' => array( 'method' => "GET", @@ -17,8 +21,12 @@ public static function get($uname, $pass, $url) return json_decode($file, true); } - public static function delete($uname, $pass, $url) + public static function delete($uname, $pass, $url, array $data = null) { + if ($data) { + $url = $url . '?' . http_build_query($data); + } + $opts = array( 'http' => array( 'method' => "DELETE", diff --git a/src/api/ApiBase.php b/src/api/ApiBase.php index 2c75768..223ad38 100644 --- a/src/api/ApiBase.php +++ b/src/api/ApiBase.php @@ -9,6 +9,7 @@ namespace Mondido\Api; +use Mondido\HttpHelper; use Mondido\Settings\Configuration; class ApiBase @@ -18,6 +19,10 @@ class ApiBase protected $password; protected $secret; protected $apiUrl; + protected $getMethods = [ + 'get', + 'delete', + ]; /** * StoredCard constructor. @@ -37,6 +42,24 @@ public function endpoint($url) { return $this->apiUrl . $url; } + + public function request($method, $endpoint, array $data = array(), $endpointIsUrl = false) + { + if ( ! $endpointIsUrl) { + $endpoint = $this->endpoint($endpoint); + } + + $request = array('\Mondido\HttpHelper', $method); + + $arguments = [ + $this->username, + $this->password, + $endpoint, + $data, + ]; + + return call_user_func_array($request, $arguments); + } // public static function getApiUrl() // { // return Configuration::$app_settings['api_url']; diff --git a/src/api/Subscription.php b/src/api/Subscription.php index 1f2b4df..2bbb7c1 100644 --- a/src/api/Subscription.php +++ b/src/api/Subscription.php @@ -9,15 +9,16 @@ class Subscription extends ApiBase { public function all($options = array()) { - $remote_url = $this->endpoint('subscriptions') . '?' . http_build_query($options); - - return HttpHelper::get($this->username, $this->password, $remote_url); + return $this->request('get', 'subscriptions', $options); } public function update($id, array $params) { - $remote_url = $this->endpoint('subscriptions/'.$id); + return $this->request('put', 'subscriptions/'.$id, $params); + } - return HttpHelper::put($this->username, $this->password, $remote_url, $params); + public function get($id) + { + return $this->request('get', 'subscriptions/'.$id); } } \ No newline at end of file From de515f157bbeeec454572dae4b365f7e2b1d7ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 18 Aug 2015 17:12:13 +0200 Subject: [PATCH 27/36] Bug fixes --- src/Mondido.php | 3 +++ src/api/Customer.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Mondido.php b/src/Mondido.php index 9caad3e..c8d0747 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -21,6 +21,8 @@ class Mondido public $storedCard; public $transaction; public $customer; + public $subscription; + public $plan; private $hostedWindowUrl = 'https://pay.mondido.com/v1/form'; /* @@ -56,6 +58,7 @@ public function transaction() { return new Transaction($this->username, $this->password, $this->secret, $this->apiUrl); } + private function setIfExists($attribute, $value) { if ($value) { diff --git a/src/api/Customer.php b/src/api/Customer.php index fb1ad93..d191efa 100644 --- a/src/api/Customer.php +++ b/src/api/Customer.php @@ -28,7 +28,7 @@ public function index() public function create($customer) { if (!($customer instanceof \Mondido\Models\Customer)) { - $customer = new \Mondido\Models\Customer($this->username, $this->secret, $transaction); + $customer = new \Mondido\Models\Customer($this->username, $this->secret, $customer); } $params = $customer->getAllAttributes(); From 324d6b62d33827e1afaf0249586634616da8c5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Tue, 18 Aug 2015 18:07:03 +0200 Subject: [PATCH 28/36] bug fix --- src/api/Customer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/Customer.php b/src/api/Customer.php index d191efa..6d242b9 100644 --- a/src/api/Customer.php +++ b/src/api/Customer.php @@ -28,7 +28,7 @@ public function index() public function create($customer) { if (!($customer instanceof \Mondido\Models\Customer)) { - $customer = new \Mondido\Models\Customer($this->username, $this->secret, $customer); + $customer = new \Mondido\Models\Customer($customer); } $params = $customer->getAllAttributes(); @@ -54,7 +54,7 @@ public function create($customer) public function update($customer) { if (!($customer instanceof \Mondido\Models\Customer)) { - $customer = new \Mondido\Models\Customer($this->username, $this->secret, $customer); + $customer = new \Mondido\Models\Customer($customer); } $remote_url = $this->endpoint('customers/' . $customer->id); From 7c9cda80405f6a755f1c4f8d90e20b7af8d5881d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Wed, 19 Aug 2015 15:52:49 +0200 Subject: [PATCH 29/36] Renamed to upper case --- src/Api/ApiBase.php | 77 +++++++++ src/Api/Customer.php | 69 ++++++++ src/Api/Plan.php | 16 ++ src/Api/Refund.php | 25 +++ src/Api/StoredCard.php | 40 +++++ src/Api/Subscription.php | 24 +++ src/Api/Transaction.php | 57 +++++++ src/Models/BaseModel.php | 36 +++++ src/Models/CreditCard.php | 118 ++++++++++++++ src/Models/Customer.php | 33 ++++ src/Models/Plan.php | 23 +++ src/Models/Subscription.php | 23 +++ src/Models/Transaction.php | 287 +++++++++++++++++++++++++++++++++ src/Request/Webhook.php | 27 ++++ src/Settings/Configuration.php | 25 +++ 15 files changed, 880 insertions(+) create mode 100644 src/Api/ApiBase.php create mode 100644 src/Api/Customer.php create mode 100644 src/Api/Plan.php create mode 100644 src/Api/Refund.php create mode 100644 src/Api/StoredCard.php create mode 100644 src/Api/Subscription.php create mode 100644 src/Api/Transaction.php create mode 100644 src/Models/BaseModel.php create mode 100644 src/Models/CreditCard.php create mode 100644 src/Models/Customer.php create mode 100644 src/Models/Plan.php create mode 100644 src/Models/Subscription.php create mode 100644 src/Models/Transaction.php create mode 100644 src/Request/Webhook.php create mode 100644 src/Settings/Configuration.php diff --git a/src/Api/ApiBase.php b/src/Api/ApiBase.php new file mode 100644 index 0000000..223ad38 --- /dev/null +++ b/src/Api/ApiBase.php @@ -0,0 +1,77 @@ +username = $username; + $this->password = $password; + $this->secret = $secret; + $this->apiUrl = $apiUrl; + } + + public function endpoint($url) + { + return $this->apiUrl . $url; + } + + public function request($method, $endpoint, array $data = array(), $endpointIsUrl = false) + { + if ( ! $endpointIsUrl) { + $endpoint = $this->endpoint($endpoint); + } + + $request = array('\Mondido\HttpHelper', $method); + + $arguments = [ + $this->username, + $this->password, + $endpoint, + $data, + ]; + + return call_user_func_array($request, $arguments); + } +// public static function getApiUrl() +// { +// return Configuration::$app_settings['api_url']; +// } +// +// public static function getUsername() +// { +// return Configuration::$app_settings['username']; +// } +// +// public static function getPassword() +// { +// return Configuration::$app_settings['password']; +// } +} \ No newline at end of file diff --git a/src/Api/Customer.php b/src/Api/Customer.php new file mode 100644 index 0000000..6d242b9 --- /dev/null +++ b/src/Api/Customer.php @@ -0,0 +1,69 @@ +endpoint('customers/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function index() + { + $remote_url = $this->endpoint('customers'); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function create($customer) + { + if (!($customer instanceof \Mondido\Models\Customer)) { + $customer = new \Mondido\Models\Customer($customer); + } + + $params = $customer->getAllAttributes(); + + foreach ($params as $attr => $value) { + if ($value == null) { + unset($params[$attr]); + } else { + if (is_bool($params[$attr])) { + if ($params[$attr]) { + $params[$attr] = 'true'; + } else { + $params[$attr] = 'false'; + } + } + } + } + + $remote_url = $this->endpoint('customers'); + return HttpHelper::post($this->username, $this->password, $remote_url, $params); + } + + public function update($customer) + { + if (!($customer instanceof \Mondido\Models\Customer)) { + $customer = new \Mondido\Models\Customer($customer); + } + + $remote_url = $this->endpoint('customers/' . $customer->id); + return HttpHelper::put($this->username, $this->password, $remote_url, $customer->getAllAttributes()); + } + + public function delete($id) + { + $remote_url = $this->endpoint('customers/' . $id); + return HttpHelper::delete($this->username, $this->password, $remote_url); + } +} \ No newline at end of file diff --git a/src/Api/Plan.php b/src/Api/Plan.php new file mode 100644 index 0000000..2a82cc8 --- /dev/null +++ b/src/Api/Plan.php @@ -0,0 +1,16 @@ +endpoint('plans') . '?' . http_build_query($options); + + return HttpHelper::get($this->username, $this->password, $remote_url); + } +} \ No newline at end of file diff --git a/src/Api/Refund.php b/src/Api/Refund.php new file mode 100644 index 0000000..a0154cb --- /dev/null +++ b/src/Api/Refund.php @@ -0,0 +1,25 @@ +endpoint('refunds/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function create($data) + { + $remote_url = $this->endpoint('refunds'); + return HttpHelper::post($this->username, $this->password, $remote_url, $data); + } +} \ No newline at end of file diff --git a/src/Api/StoredCard.php b/src/Api/StoredCard.php new file mode 100644 index 0000000..73e1f96 --- /dev/null +++ b/src/Api/StoredCard.php @@ -0,0 +1,40 @@ +endpoint('stored_cards/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function index($limit, $offset) + { + $remote_url = $this->endpoint('stored_cards/?limit=' . $limit . '&offset=' . $offset); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function create($card) + { + $remote_url = $this->endpoint('stored_cards'); + return HttpHelper::post($this->username, $this->password, $remote_url, $card); + } + + public function delete($id) + { + $remote_url = $this->endpoint('stored_cards/' . $id); + return HttpHelper::delete($this->username, $this->password, $remote_url); + } + +} \ No newline at end of file diff --git a/src/Api/Subscription.php b/src/Api/Subscription.php new file mode 100644 index 0000000..2bbb7c1 --- /dev/null +++ b/src/Api/Subscription.php @@ -0,0 +1,24 @@ +request('get', 'subscriptions', $options); + } + + public function update($id, array $params) + { + return $this->request('put', 'subscriptions/'.$id, $params); + } + + public function get($id) + { + return $this->request('get', 'subscriptions/'.$id); + } +} \ No newline at end of file diff --git a/src/Api/Transaction.php b/src/Api/Transaction.php new file mode 100644 index 0000000..be0c0af --- /dev/null +++ b/src/Api/Transaction.php @@ -0,0 +1,57 @@ +endpoint('transactions/' . $id); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + /* + * list transactions with a offset and a limit + */ + public function index($limit, $offset) + { + $remote_url = $this->endpoint('transactions/?limit=' . $limit . '&offset=' . $offset); + return HttpHelper::get($this->username, $this->password, $remote_url); + } + + public function create($transaction) + { + // Make a check to see if we passed in an actual transaction, or just values for the transaction + if (!($transaction instanceof \Mondido\Models\Transaction)) { + $transaction = new \Mondido\Models\Transaction($this->username, $this->secret, $transaction); + } + + $transaction_fields = $transaction->getAllAttributes(); + $card_fields = $transaction->getPayment()->getAllAttributes(); + + unset($transaction_fields["payment"]); + $params = array_merge($transaction_fields, $card_fields); + $params["amount"] = (string)number_format((float)$params["amount"], 2, '.', ''); + + // Remove Null and Update Booleans + foreach ($params as $attr => $value) { + if ($value == null) { + unset($params[$attr]); + } else { + if (is_bool($params[$attr])) { + if ($params[$attr]) { + $params[$attr] = 'true'; + } else { + $params[$attr] = 'false'; + } + } + } + } + + $remote_url = $this->endpoint('transactions'); + return HttpHelper::post($this->username, $this->password, $remote_url, $params); + } + +} \ No newline at end of file diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php new file mode 100644 index 0000000..6b479cd --- /dev/null +++ b/src/Models/BaseModel.php @@ -0,0 +1,36 @@ + $value) { + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } else { + $this->attributes['metadata'][$attribute] = $value; + } + } + + $this->attributes = $attributes; + } + + public function getAllAttributes() + { + return $this->attributes; + } + + public function __get($attribute) + { + if (isset($this->attributes[$attribute])) { + return $this->attributes[$attribute]; + } + + return null; + } + +} diff --git a/src/Models/CreditCard.php b/src/Models/CreditCard.php new file mode 100644 index 0000000..4dfd4e1 --- /dev/null +++ b/src/Models/CreditCard.php @@ -0,0 +1,118 @@ + $value) { + $attribute = snakify($attribute); + + $aliasedAttributes = array_combine($this->aliases, $this->allowedAttributes); + + if (in_array($attribute, $this->aliases)) { + // The user passed an alias instead of the fully qualified attribute name, so we'll convert it + $attribute = $aliasedAttributes[$attribute]; + } + + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } + } + } + + /** + * is utilized for reading data from inaccessible members. + * + * @param $name string + * @return mixed + * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members + */ + public function __get($name) + { + return $this->attributes[snakify($name)]; + } + + + public function getCvv() + { + return $this->card_cvv; + } + + public function setCvv($cvv) + { + $this->card_cvv = $cvv; + } + + public function getNumber() + { + return $this->card_number; + } + + public function setNumber($number) + { + $this->card_number = $number; + } + + public function getExpiry() + { + return $this->card_expiry; + } + + public function setExpiry($expiry) + { + $this->card_expiry = $expiry; + } + + public function getHolder() + { + return $this->card_holder; + } + + public function setHolder($holder) + { + $this->card_holder = $holder; + } + + public function getType() + { + return $this->card_type; + } + + public function setType($type) + { + $this->card_type = $type; + } + + public function getAllAttributes() + { + return $this->attributes; + } +} \ No newline at end of file diff --git a/src/Models/Customer.php b/src/Models/Customer.php new file mode 100644 index 0000000..faf70f3 --- /dev/null +++ b/src/Models/Customer.php @@ -0,0 +1,33 @@ + $value) { + if (in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } else { + $this->attributes['metadata'][$attribute] = $value; + } + } + + $this->attributes = $attributes; + } + + +} \ No newline at end of file diff --git a/src/Models/Plan.php b/src/Models/Plan.php new file mode 100644 index 0000000..1306770 --- /dev/null +++ b/src/Models/Plan.php @@ -0,0 +1,23 @@ +attributes['merchant_id'] = $merchantId; + $this->attributes['payment_ref'] = $paymentRef; + $this->attributes['customer_ref'] = $customerRef; + $this->attributes['secret'] = $secret; + + $payment = array(); + + foreach ($arguments as $attribute => $value) { +// $methodName = "set" . $attribute; +// $this->$methodName($value); + $attribute = snakify($attribute); + if(in_array($attribute, $this->cardAttributes)) { + $payment[$attribute] = $value; + } + + if(in_array($attribute, $this->allowedAttributes)) { + $this->attributes[$attribute] = $value; + } + } + + if (! ($this->paymentRef && $this->customerRef)) { + throw new \Exception("You must provide both a payment reference and a customer reference to make a transaction"); + } + + if (!empty($payment) && is_array($payment)) { + $this->attributes['payment'] = new CreditCard($payment); + } + +// if (!isset($arguments["MerchantId"])) { +// $this->setMerchantId(Configuration::$app_settings['username']); +// } + + if (!isset($arguments["hash"])) { + $this->setHash($secret); + } + } + + /** + * is utilized for reading data from inaccessible members. + * + * @param $name string + * @return mixed + * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members + */ + public function __get($name) + { + return $this->attributes[snakify($name)]; + } + + + public function getMerchantId() + { + return $this->merchant_id; + } + + public function setMerchantId($merchantId) + { + $this->merchant_id = $merchantId; + } + + public function getAmount() + { + return $this->amount; + } + + public function setAmount($amount) + { + $this->amount = $amount; + } + + public function getPaymentRef() + { + return $this->payment_ref; + } + + public function setPaymentRef($paymentRef) + { + $this->payment_ref = $paymentRef; + } + + public function getPayment() + { + return $this->payment; + } + + public function setPayment($payment) + { + $this->payment = $payment; + } + + public function getTest() + { + return $this->test; + } + + public function setTest($test) + { + $this->test = $test; + } + + public function getMetadata() + { + return $this->metadata; + } + + public function setMetadata($metadata) + { + $this->metadata = $metadata; + } + + public function getCurrency() + { + return $this->currency; + } + + public function setCurrency($currency) + { + $this->currency = $currency; + } + + public function getStoreCard() + { + return $this->store_card; + } + + public function setStoreCard($storeCard) + { + $this->store_card = $storeCard; + } + + public function getPlanId() + { + return $this->plan_id; + } + + public function setPlanId($planId) + { + $this->plan_id = $planId; + } + + public function getCustomerRef() + { + return $this->customer_ref; + } + + public function setCustomerRef($customerRef) + { + $this->customer_ref = $customerRef; + } + + public function getHash() + { + return $this->hash; + } + + public function setHash($secret, $algorithm = null) + { + if (!$secret) { + $secret = Configuration::$app_settings['secret']; + } + if (!$algorithm) { + $algorithm = Configuration::$app_settings['algorithm']; + } + $this->attributes['hash'] = $this->calculateHash($secret, $algorithm); + } + + public function getWebhook() + { + return $this->webhook; + } + + public function setWebhook($webhook) + { + $this->webhook = $webhook; + } + + public function getEncrypted() + { + return $this->encrypted; + } + + public function setEncrypted($encrypted) + { + $this->encrypted = $encrypted; + } + + public function getProcess() + { + return $this->process; + } + + public function setProcess($process) + { + $this->process = $process; + } + + public function getSuccessUrl() + { + return $this->success_url; + } + + public function setSuccessUrl($successUrl) + { + $this->success_url = $successUrl; + } + + public function getErrorUrl() + { + return $this->error_url; + } + + public function setErrorUrl($errorUrl) + { + $this->error_url = $errorUrl; + } + + # Custom + + private function calculateHash($secret, $algorithm) + { + $recipe = (string)$this->getMerchantId(); + $recipe .= (string )$this->getPaymentRef(); + $recipe .= (string)$this->getCustomerRef(); + $recipe .= (string)number_format((float)$this->getAmount(), 2, '.', ''); + $recipe .= (string)$this->getCurrency(); + $recipe .= ($this->getTest()) ? "test" : ""; + $recipe .= $secret; + + return $algorithm($recipe); + } + +} \ No newline at end of file diff --git a/src/Request/Webhook.php b/src/Request/Webhook.php new file mode 100644 index 0000000..489aaf3 --- /dev/null +++ b/src/Request/Webhook.php @@ -0,0 +1,27 @@ + 'https://api.mondido.com/v1/', + "username" => '148', + "password" => '', + "secret" => '', + "algorithm" => 'md5' + ); + +} \ No newline at end of file From 2dec8f97b1aa0fda1fa88655580cc8f5cf177ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 20 Aug 2015 10:16:41 +0200 Subject: [PATCH 30/36] Removed all the badly named --- src/api/ApiBase.php | 77 --------- src/api/Customer.php | 69 -------- src/api/Plan.php | 16 -- src/api/StoredCard.php | 40 ----- src/api/Subscription.php | 24 --- src/api/refund.php | 25 --- src/api/transaction.php | 57 ------- src/models/BaseModel.php | 36 ----- src/models/CreditCard.php | 118 -------------- src/models/Customer.php | 33 ---- src/models/Plan.php | 23 --- src/models/Subscription.php | 23 --- src/models/transaction.php | 287 --------------------------------- src/request/webhook.php | 27 ---- src/settings/configuration.php | 25 --- 15 files changed, 880 deletions(-) delete mode 100644 src/api/ApiBase.php delete mode 100644 src/api/Customer.php delete mode 100644 src/api/Plan.php delete mode 100644 src/api/StoredCard.php delete mode 100644 src/api/Subscription.php delete mode 100644 src/api/refund.php delete mode 100644 src/api/transaction.php delete mode 100644 src/models/BaseModel.php delete mode 100644 src/models/CreditCard.php delete mode 100644 src/models/Customer.php delete mode 100644 src/models/Plan.php delete mode 100644 src/models/Subscription.php delete mode 100644 src/models/transaction.php delete mode 100644 src/request/webhook.php delete mode 100644 src/settings/configuration.php diff --git a/src/api/ApiBase.php b/src/api/ApiBase.php deleted file mode 100644 index 223ad38..0000000 --- a/src/api/ApiBase.php +++ /dev/null @@ -1,77 +0,0 @@ -username = $username; - $this->password = $password; - $this->secret = $secret; - $this->apiUrl = $apiUrl; - } - - public function endpoint($url) - { - return $this->apiUrl . $url; - } - - public function request($method, $endpoint, array $data = array(), $endpointIsUrl = false) - { - if ( ! $endpointIsUrl) { - $endpoint = $this->endpoint($endpoint); - } - - $request = array('\Mondido\HttpHelper', $method); - - $arguments = [ - $this->username, - $this->password, - $endpoint, - $data, - ]; - - return call_user_func_array($request, $arguments); - } -// public static function getApiUrl() -// { -// return Configuration::$app_settings['api_url']; -// } -// -// public static function getUsername() -// { -// return Configuration::$app_settings['username']; -// } -// -// public static function getPassword() -// { -// return Configuration::$app_settings['password']; -// } -} \ No newline at end of file diff --git a/src/api/Customer.php b/src/api/Customer.php deleted file mode 100644 index 6d242b9..0000000 --- a/src/api/Customer.php +++ /dev/null @@ -1,69 +0,0 @@ -endpoint('customers/' . $id); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function index() - { - $remote_url = $this->endpoint('customers'); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function create($customer) - { - if (!($customer instanceof \Mondido\Models\Customer)) { - $customer = new \Mondido\Models\Customer($customer); - } - - $params = $customer->getAllAttributes(); - - foreach ($params as $attr => $value) { - if ($value == null) { - unset($params[$attr]); - } else { - if (is_bool($params[$attr])) { - if ($params[$attr]) { - $params[$attr] = 'true'; - } else { - $params[$attr] = 'false'; - } - } - } - } - - $remote_url = $this->endpoint('customers'); - return HttpHelper::post($this->username, $this->password, $remote_url, $params); - } - - public function update($customer) - { - if (!($customer instanceof \Mondido\Models\Customer)) { - $customer = new \Mondido\Models\Customer($customer); - } - - $remote_url = $this->endpoint('customers/' . $customer->id); - return HttpHelper::put($this->username, $this->password, $remote_url, $customer->getAllAttributes()); - } - - public function delete($id) - { - $remote_url = $this->endpoint('customers/' . $id); - return HttpHelper::delete($this->username, $this->password, $remote_url); - } -} \ No newline at end of file diff --git a/src/api/Plan.php b/src/api/Plan.php deleted file mode 100644 index 2a82cc8..0000000 --- a/src/api/Plan.php +++ /dev/null @@ -1,16 +0,0 @@ -endpoint('plans') . '?' . http_build_query($options); - - return HttpHelper::get($this->username, $this->password, $remote_url); - } -} \ No newline at end of file diff --git a/src/api/StoredCard.php b/src/api/StoredCard.php deleted file mode 100644 index 73e1f96..0000000 --- a/src/api/StoredCard.php +++ /dev/null @@ -1,40 +0,0 @@ -endpoint('stored_cards/' . $id); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function index($limit, $offset) - { - $remote_url = $this->endpoint('stored_cards/?limit=' . $limit . '&offset=' . $offset); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function create($card) - { - $remote_url = $this->endpoint('stored_cards'); - return HttpHelper::post($this->username, $this->password, $remote_url, $card); - } - - public function delete($id) - { - $remote_url = $this->endpoint('stored_cards/' . $id); - return HttpHelper::delete($this->username, $this->password, $remote_url); - } - -} \ No newline at end of file diff --git a/src/api/Subscription.php b/src/api/Subscription.php deleted file mode 100644 index 2bbb7c1..0000000 --- a/src/api/Subscription.php +++ /dev/null @@ -1,24 +0,0 @@ -request('get', 'subscriptions', $options); - } - - public function update($id, array $params) - { - return $this->request('put', 'subscriptions/'.$id, $params); - } - - public function get($id) - { - return $this->request('get', 'subscriptions/'.$id); - } -} \ No newline at end of file diff --git a/src/api/refund.php b/src/api/refund.php deleted file mode 100644 index a0154cb..0000000 --- a/src/api/refund.php +++ /dev/null @@ -1,25 +0,0 @@ -endpoint('refunds/' . $id); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function create($data) - { - $remote_url = $this->endpoint('refunds'); - return HttpHelper::post($this->username, $this->password, $remote_url, $data); - } -} \ No newline at end of file diff --git a/src/api/transaction.php b/src/api/transaction.php deleted file mode 100644 index be0c0af..0000000 --- a/src/api/transaction.php +++ /dev/null @@ -1,57 +0,0 @@ -endpoint('transactions/' . $id); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - /* - * list transactions with a offset and a limit - */ - public function index($limit, $offset) - { - $remote_url = $this->endpoint('transactions/?limit=' . $limit . '&offset=' . $offset); - return HttpHelper::get($this->username, $this->password, $remote_url); - } - - public function create($transaction) - { - // Make a check to see if we passed in an actual transaction, or just values for the transaction - if (!($transaction instanceof \Mondido\Models\Transaction)) { - $transaction = new \Mondido\Models\Transaction($this->username, $this->secret, $transaction); - } - - $transaction_fields = $transaction->getAllAttributes(); - $card_fields = $transaction->getPayment()->getAllAttributes(); - - unset($transaction_fields["payment"]); - $params = array_merge($transaction_fields, $card_fields); - $params["amount"] = (string)number_format((float)$params["amount"], 2, '.', ''); - - // Remove Null and Update Booleans - foreach ($params as $attr => $value) { - if ($value == null) { - unset($params[$attr]); - } else { - if (is_bool($params[$attr])) { - if ($params[$attr]) { - $params[$attr] = 'true'; - } else { - $params[$attr] = 'false'; - } - } - } - } - - $remote_url = $this->endpoint('transactions'); - return HttpHelper::post($this->username, $this->password, $remote_url, $params); - } - -} \ No newline at end of file diff --git a/src/models/BaseModel.php b/src/models/BaseModel.php deleted file mode 100644 index 6b479cd..0000000 --- a/src/models/BaseModel.php +++ /dev/null @@ -1,36 +0,0 @@ - $value) { - if (in_array($attribute, $this->allowedAttributes)) { - $this->attributes[$attribute] = $value; - } else { - $this->attributes['metadata'][$attribute] = $value; - } - } - - $this->attributes = $attributes; - } - - public function getAllAttributes() - { - return $this->attributes; - } - - public function __get($attribute) - { - if (isset($this->attributes[$attribute])) { - return $this->attributes[$attribute]; - } - - return null; - } - -} diff --git a/src/models/CreditCard.php b/src/models/CreditCard.php deleted file mode 100644 index 4dfd4e1..0000000 --- a/src/models/CreditCard.php +++ /dev/null @@ -1,118 +0,0 @@ - $value) { - $attribute = snakify($attribute); - - $aliasedAttributes = array_combine($this->aliases, $this->allowedAttributes); - - if (in_array($attribute, $this->aliases)) { - // The user passed an alias instead of the fully qualified attribute name, so we'll convert it - $attribute = $aliasedAttributes[$attribute]; - } - - if (in_array($attribute, $this->allowedAttributes)) { - $this->attributes[$attribute] = $value; - } - } - } - - /** - * is utilized for reading data from inaccessible members. - * - * @param $name string - * @return mixed - * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members - */ - public function __get($name) - { - return $this->attributes[snakify($name)]; - } - - - public function getCvv() - { - return $this->card_cvv; - } - - public function setCvv($cvv) - { - $this->card_cvv = $cvv; - } - - public function getNumber() - { - return $this->card_number; - } - - public function setNumber($number) - { - $this->card_number = $number; - } - - public function getExpiry() - { - return $this->card_expiry; - } - - public function setExpiry($expiry) - { - $this->card_expiry = $expiry; - } - - public function getHolder() - { - return $this->card_holder; - } - - public function setHolder($holder) - { - $this->card_holder = $holder; - } - - public function getType() - { - return $this->card_type; - } - - public function setType($type) - { - $this->card_type = $type; - } - - public function getAllAttributes() - { - return $this->attributes; - } -} \ No newline at end of file diff --git a/src/models/Customer.php b/src/models/Customer.php deleted file mode 100644 index faf70f3..0000000 --- a/src/models/Customer.php +++ /dev/null @@ -1,33 +0,0 @@ - $value) { - if (in_array($attribute, $this->allowedAttributes)) { - $this->attributes[$attribute] = $value; - } else { - $this->attributes['metadata'][$attribute] = $value; - } - } - - $this->attributes = $attributes; - } - - -} \ No newline at end of file diff --git a/src/models/Plan.php b/src/models/Plan.php deleted file mode 100644 index 1306770..0000000 --- a/src/models/Plan.php +++ /dev/null @@ -1,23 +0,0 @@ -attributes['merchant_id'] = $merchantId; - $this->attributes['payment_ref'] = $paymentRef; - $this->attributes['customer_ref'] = $customerRef; - $this->attributes['secret'] = $secret; - - $payment = array(); - - foreach ($arguments as $attribute => $value) { -// $methodName = "set" . $attribute; -// $this->$methodName($value); - $attribute = snakify($attribute); - if(in_array($attribute, $this->cardAttributes)) { - $payment[$attribute] = $value; - } - - if(in_array($attribute, $this->allowedAttributes)) { - $this->attributes[$attribute] = $value; - } - } - - if (! ($this->paymentRef && $this->customerRef)) { - throw new \Exception("You must provide both a payment reference and a customer reference to make a transaction"); - } - - if (!empty($payment) && is_array($payment)) { - $this->attributes['payment'] = new CreditCard($payment); - } - -// if (!isset($arguments["MerchantId"])) { -// $this->setMerchantId(Configuration::$app_settings['username']); -// } - - if (!isset($arguments["hash"])) { - $this->setHash($secret); - } - } - - /** - * is utilized for reading data from inaccessible members. - * - * @param $name string - * @return mixed - * @link http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members - */ - public function __get($name) - { - return $this->attributes[snakify($name)]; - } - - - public function getMerchantId() - { - return $this->merchant_id; - } - - public function setMerchantId($merchantId) - { - $this->merchant_id = $merchantId; - } - - public function getAmount() - { - return $this->amount; - } - - public function setAmount($amount) - { - $this->amount = $amount; - } - - public function getPaymentRef() - { - return $this->payment_ref; - } - - public function setPaymentRef($paymentRef) - { - $this->payment_ref = $paymentRef; - } - - public function getPayment() - { - return $this->payment; - } - - public function setPayment($payment) - { - $this->payment = $payment; - } - - public function getTest() - { - return $this->test; - } - - public function setTest($test) - { - $this->test = $test; - } - - public function getMetadata() - { - return $this->metadata; - } - - public function setMetadata($metadata) - { - $this->metadata = $metadata; - } - - public function getCurrency() - { - return $this->currency; - } - - public function setCurrency($currency) - { - $this->currency = $currency; - } - - public function getStoreCard() - { - return $this->store_card; - } - - public function setStoreCard($storeCard) - { - $this->store_card = $storeCard; - } - - public function getPlanId() - { - return $this->plan_id; - } - - public function setPlanId($planId) - { - $this->plan_id = $planId; - } - - public function getCustomerRef() - { - return $this->customer_ref; - } - - public function setCustomerRef($customerRef) - { - $this->customer_ref = $customerRef; - } - - public function getHash() - { - return $this->hash; - } - - public function setHash($secret, $algorithm = null) - { - if (!$secret) { - $secret = Configuration::$app_settings['secret']; - } - if (!$algorithm) { - $algorithm = Configuration::$app_settings['algorithm']; - } - $this->attributes['hash'] = $this->calculateHash($secret, $algorithm); - } - - public function getWebhook() - { - return $this->webhook; - } - - public function setWebhook($webhook) - { - $this->webhook = $webhook; - } - - public function getEncrypted() - { - return $this->encrypted; - } - - public function setEncrypted($encrypted) - { - $this->encrypted = $encrypted; - } - - public function getProcess() - { - return $this->process; - } - - public function setProcess($process) - { - $this->process = $process; - } - - public function getSuccessUrl() - { - return $this->success_url; - } - - public function setSuccessUrl($successUrl) - { - $this->success_url = $successUrl; - } - - public function getErrorUrl() - { - return $this->error_url; - } - - public function setErrorUrl($errorUrl) - { - $this->error_url = $errorUrl; - } - - # Custom - - private function calculateHash($secret, $algorithm) - { - $recipe = (string)$this->getMerchantId(); - $recipe .= (string )$this->getPaymentRef(); - $recipe .= (string)$this->getCustomerRef(); - $recipe .= (string)number_format((float)$this->getAmount(), 2, '.', ''); - $recipe .= (string)$this->getCurrency(); - $recipe .= ($this->getTest()) ? "test" : ""; - $recipe .= $secret; - - return $algorithm($recipe); - } - -} \ No newline at end of file diff --git a/src/request/webhook.php b/src/request/webhook.php deleted file mode 100644 index 489aaf3..0000000 --- a/src/request/webhook.php +++ /dev/null @@ -1,27 +0,0 @@ - 'https://api.mondido.com/v1/', - "username" => '148', - "password" => '', - "secret" => '', - "algorithm" => 'md5' - ); - -} \ No newline at end of file From 39cb34d728d5f9ea7b22d112e8b62202bb78be13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Mon, 24 Aug 2015 13:43:19 +0200 Subject: [PATCH 31/36] Added docblocks and a method to create subscriptions --- src/Api/Subscription.php | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Api/Subscription.php b/src/Api/Subscription.php index 2bbb7c1..0381ab1 100644 --- a/src/Api/Subscription.php +++ b/src/Api/Subscription.php @@ -7,18 +7,61 @@ class Subscription extends ApiBase { + /** + * Retrieve all subscriptions. + * + * @param array $options + * @return array + */ public function all($options = array()) { return $this->request('get', 'subscriptions', $options); } + /** + * Update a given subscription. + * + * @param int $id + * @param array $params + * @return array + */ public function update($id, array $params) { return $this->request('put', 'subscriptions/'.$id, $params); } + /** + * Fetch a given subscription. + * + * @param int $id + * @return array + */ public function get($id) { return $this->request('get', 'subscriptions/'.$id); } + + /** + * Create a new subscription. + * @param string $planId + * @param int|string $identifier + * @param string $identifierType + * @return array + */ + public function create($planId, $identifier, $identifierType = 'ref') + { + $types = [ + 'card' => 'stored_card_id', + 'ref' => 'customer_ref', + 'customer' => 'customer_id' + ]; + + $type = isset($types[$identifierType]) ? $types[$identifierType] : $identifierType; + + return $this->request('post', 'subscriptions', [ + 'plan_id' => $planId, + $type => $identifier, + 'quantity' => 1, + ]); + } } \ No newline at end of file From ca0fa029d8a61c61fb9761d540b798d951b52615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Mon, 24 Aug 2015 13:47:45 +0200 Subject: [PATCH 32/36] Use PHP 5.3 compatible array --- src/Api/Subscription.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Api/Subscription.php b/src/Api/Subscription.php index 0381ab1..ffc3c6b 100644 --- a/src/Api/Subscription.php +++ b/src/Api/Subscription.php @@ -58,10 +58,10 @@ public function create($planId, $identifier, $identifierType = 'ref') $type = isset($types[$identifierType]) ? $types[$identifierType] : $identifierType; - return $this->request('post', 'subscriptions', [ + return $this->request('post', 'subscriptions', array( 'plan_id' => $planId, $type => $identifier, 'quantity' => 1, - ]); + )); } } \ No newline at end of file From 96e534c4c051bf47c98b8f08651c944e77ca5c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Mon, 24 Aug 2015 13:49:42 +0200 Subject: [PATCH 33/36] cs fix --- src/Api/Subscription.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Api/Subscription.php b/src/Api/Subscription.php index ffc3c6b..aca67e1 100644 --- a/src/Api/Subscription.php +++ b/src/Api/Subscription.php @@ -2,7 +2,6 @@ namespace Mondido\Api; - use Mondido\HttpHelper; class Subscription extends ApiBase @@ -43,6 +42,7 @@ public function get($id) /** * Create a new subscription. + * * @param string $planId * @param int|string $identifier * @param string $identifierType @@ -50,11 +50,11 @@ public function get($id) */ public function create($planId, $identifier, $identifierType = 'ref') { - $types = [ + $types = array( 'card' => 'stored_card_id', 'ref' => 'customer_ref', - 'customer' => 'customer_id' - ]; + 'customer' => 'customer_id', + ); $type = isset($types[$identifierType]) ? $types[$identifierType] : $identifierType; From f6768bf185ded1b5f17b7b2ab44374a5621cf432 Mon Sep 17 00:00:00 2001 From: Emil Sundberg Date: Mon, 24 Aug 2015 15:26:47 +0200 Subject: [PATCH 34/36] PHP 5.3 compatibility with array syntax adjustments --- src/Api/ApiBase.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Api/ApiBase.php b/src/Api/ApiBase.php index 223ad38..3a7145c 100644 --- a/src/Api/ApiBase.php +++ b/src/Api/ApiBase.php @@ -19,10 +19,10 @@ class ApiBase protected $password; protected $secret; protected $apiUrl; - protected $getMethods = [ + protected $getMethods = array( 'get', 'delete', - ]; + ); /** * StoredCard constructor. @@ -51,12 +51,12 @@ public function request($method, $endpoint, array $data = array(), $endpointIsUr $request = array('\Mondido\HttpHelper', $method); - $arguments = [ + $arguments = array( $this->username, $this->password, $endpoint, $data, - ]; + ); return call_user_func_array($request, $arguments); } @@ -74,4 +74,4 @@ public function request($method, $endpoint, array $data = array(), $endpointIsUr // { // return Configuration::$app_settings['password']; // } -} \ No newline at end of file +} From 573b2479b5d5e466e857939deb994d35c8533d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Sjo=CC=88berg?= Date: Thu, 27 Aug 2015 11:43:30 +0200 Subject: [PATCH 35/36] Allow users to pass the filter directly without wrapping it as an array. E.g using 'name' => 'test', rather than 'filter[name]' => 'test' --- src/Api/Plan.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Api/Plan.php b/src/Api/Plan.php index 2a82cc8..7ddf281 100644 --- a/src/Api/Plan.php +++ b/src/Api/Plan.php @@ -9,8 +9,21 @@ class Plan extends ApiBase { public function all($options = array()) { + foreach ($options as $filter => $value) { + if (substr($filter, 0, 6) != 'filter') { + // The user probably wants to filter the request + // but didn't format it correctly, so we do it. + $options["filter[{$filter}]"] = $value; + unset ($options[$filter]); + } + } $remote_url = $this->endpoint('plans') . '?' . http_build_query($options); return HttpHelper::get($this->username, $this->password, $remote_url); } + + public function get($id) + { + return $this->request('get', 'plans/'.$id); + } } \ No newline at end of file From 79bf46e50668ca30ab2a7bd5925c15107441a2a2 Mon Sep 17 00:00:00 2001 From: Emil Sundberg Date: Mon, 27 Jun 2016 10:54:36 +0200 Subject: [PATCH 36/36] Set testmode parameter value according to docs --- src/Mondido.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Mondido.php b/src/Mondido.php index c8d0747..79238a1 100644 --- a/src/Mondido.php +++ b/src/Mondido.php @@ -117,6 +117,8 @@ public function generatePostForm($payload) $form = ''; $form .= ''; + empty($payload['test']) ? $payload['test'] = "false" : "true"; + foreach ($payload as $dataType => $data) { if (is_array($data)) { $data = json_encode($data); @@ -182,4 +184,4 @@ public static function logToFile($path, $transaction) $log .= 'webhooks: ' . $transaction['webhooks'] . PHP_EOL; file_put_contents($path, $log); } -} \ No newline at end of file +}