-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hschimpf/1.x-dev
Initial release
- Loading branch information
Showing
67 changed files
with
2,582 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/logs/ | ||
/vendor/ | ||
/.idea/ | ||
/.vscode/ | ||
/tests/.env | ||
|
||
composer.lock | ||
*.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Bancard SDK | ||
Library to implement [Bancard vPOS](https://bancard.com.py/vpos) product. | ||
|
||
## Dependencies | ||
- PHP 8.0+ | ||
|
||
## Usage | ||
TODO ... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "hds-solutions/bancard-sdk", | ||
"description": "Bancard SDK library", | ||
"type": "library", | ||
"license": "GPL-3.0", | ||
"authors": [ | ||
{ | ||
"name": "Hermann D. Schimpf", | ||
"email": "hhschimpf@hds-solutions.net" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.0", | ||
"guzzlehttp/guzzle": "^7.5", | ||
"guzzlehttp/psr7": "^2.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"HDSSolutions\\Bancard\\": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5", | ||
"vlucas/phpdotenv": "^5.5" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"HDSSolutions\\Bancard\\Tests\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"vendor/bin/phpunit --coverage-html tests/coverage" | ||
] | ||
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true, | ||
"config": { | ||
"optimize-autoloader": true, | ||
"preferred-install": { | ||
"*": "dist" | ||
}, | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Bancard SDK Test Suite"> | ||
<directory suffix=".php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
|
||
<php> | ||
|
||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HDSSolutions\Bancard; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use HDSSolutions\Bancard\Traits\BuildsRequests; | ||
use HDSSolutions\Bancard\Traits\HasServices; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
final class Bancard { | ||
use HasServices { | ||
HasServices::init as HasServices_init; | ||
} | ||
use BuildsRequests; | ||
|
||
/** | ||
* Base URI for testing environment | ||
*/ | ||
private const URI_Staging = 'https://vpos.infonet.com.py:8888'; | ||
|
||
/** | ||
* Base URI for production environment | ||
*/ | ||
private const URI_Production = 'https://vpos.infonet.com.py'; | ||
|
||
/** | ||
* @var self Singleton instance | ||
*/ | ||
private static self $singleton; | ||
|
||
/** | ||
* @var bool Flag to control which environment to use | ||
*/ | ||
private static bool $DEV_ENV = true; | ||
|
||
/** | ||
* @var string|null Public Key for communication | ||
*/ | ||
private static ?string $PUBLIC_KEY = null; | ||
|
||
/** | ||
* @var string|null Private Key for building tokens | ||
*/ | ||
private static ?string $PRIVATE_KEY = null; | ||
|
||
/** | ||
* @var Client HTTP Client to Bancard services | ||
*/ | ||
private Client $client; | ||
|
||
/** | ||
* @var RequestInterface|null Latest request sent | ||
*/ | ||
private ?RequestInterface $latest_request = null; | ||
|
||
private function __construct() { | ||
// init HTTP client | ||
$this->client = new Client([ | ||
'base_uri' => self::isProduction() | ||
? self::URI_Production | ||
: self::URI_Staging, | ||
'handler' => $stack = HandlerStack::create(), | ||
]); | ||
// add a middleware to capture requests sent body | ||
$stack->push(Middleware::mapRequest(function(RequestInterface $request) { | ||
// store request made | ||
$this->latest_request = $request; | ||
|
||
return $request; | ||
})); | ||
// init services | ||
$this->HasServices_init(); | ||
} | ||
|
||
public function getLatestRequest(): ?RequestInterface { | ||
return $this->latest_request; | ||
} | ||
|
||
/** | ||
* Returns the registered Public Key | ||
* | ||
* @return string Public Key | ||
*/ | ||
public static function getPublicKey(): string { | ||
return self::$PUBLIC_KEY; | ||
} | ||
|
||
/** | ||
* Returns the registered Private Key | ||
* | ||
* @return string Private Key | ||
*/ | ||
public static function getPrivateKey(): string { | ||
return self::$PRIVATE_KEY; | ||
} | ||
|
||
/** | ||
* @return self Returns singleton instance | ||
*/ | ||
protected static function instance(): self { | ||
return self::$singleton ??= new self(); | ||
} | ||
|
||
/** | ||
* Stores the credentials to use for communication with Bancard services | ||
* | ||
* @param ?string $publicKey Public Key. | ||
* @param ?string $privateKey Private Key | ||
*/ | ||
public static function credentials(?string $publicKey, ?string $privateKey): void { | ||
self::$PUBLIC_KEY = $publicKey; | ||
self::$PRIVATE_KEY = $privateKey; | ||
} | ||
|
||
public static function useDevelop(bool $develop = true): void { | ||
self::$DEV_ENV = $develop; | ||
} | ||
|
||
public static function useProduction(bool $production = true): void { | ||
self::$DEV_ENV = !$production; | ||
} | ||
|
||
public static function isDevelop(): bool { | ||
return self::$DEV_ENV; | ||
} | ||
|
||
public static function isProduction(): bool { | ||
return !self::$DEV_ENV; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HDSSolutions\Bancard\Builders; | ||
|
||
use HDSSolutions\Bancard\Models\Card; | ||
use HDSSolutions\Bancard\Models\PendingCard; | ||
use HDSSolutions\Bancard\Requests\CardDeleteRequest; | ||
use HDSSolutions\Bancard\Requests\CardsNewRequest; | ||
use HDSSolutions\Bancard\Requests\UsersCardsRequest; | ||
|
||
trait CardsRequests { | ||
|
||
public static function newCardsNewRequest(int $user_id, int $card_id, string $phone_no, string $email, ?string $return_url = null): CardsNewRequest { | ||
// build a pending card resource | ||
$pending_card = new PendingCard($user_id, $card_id, $phone_no, $email); | ||
|
||
// return the request for the card | ||
return new CardsNewRequest(self::instance(), $pending_card, $return_url); | ||
} | ||
|
||
public static function newUsersCardsRequest(int $user_id): UsersCardsRequest { | ||
// return the request | ||
return new UsersCardsRequest(self::instance(), $user_id); | ||
} | ||
|
||
public static function newCardDeleteRequest(Card $card): CardDeleteRequest { | ||
// return the request | ||
return new CardDeleteRequest(self::instance(), $card); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HDSSolutions\Bancard\Builders; | ||
|
||
use HDSSolutions\Bancard\Models\PendingPayment; | ||
use HDSSolutions\Bancard\Requests\SingleBuyRequest; | ||
|
||
trait SingleBuyRequests { | ||
|
||
public static function newSingleBuyRequest(int $shop_process_id, float $amount, string $description, string $currency, ?string $return_url = null, ?string $cancel_url = null): SingleBuyRequest { | ||
// build a pending payment resource | ||
$pending_payment = new PendingPayment($shop_process_id, $amount, $description, $currency); | ||
|
||
// return the request for the payment | ||
return new SingleBuyRequest(self::instance(), $pending_payment, $return_url, $cancel_url); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HDSSolutions\Bancard\Builders; | ||
|
||
use HDSSolutions\Bancard\Bancard; | ||
use HDSSolutions\Bancard\Requests\Contracts\ConfirmationRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\BancardRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\CardDeleteRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\CardsNewRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\ChargeRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\PreauthorizationConfirmRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\SingleBuyRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\RollbackRequest; | ||
use HDSSolutions\Bancard\Requests\Contracts\UsersCardsRequest; | ||
|
||
final class TokenBuilder { | ||
|
||
public static function for(BancardRequest $request): string { | ||
// sanitize endpoint name | ||
$method = sprintf("%s_%s", | ||
// POST => post | ||
strtolower($request->getMethod()), | ||
// users/{id}/cards => users_cards | ||
preg_replace(['/\d/', '/\/(\/)*/'], ['', '_'], $request->getEndpoint())); | ||
|
||
// return token for request | ||
return self::$method($request); | ||
} | ||
|
||
private static function post_single_buy(SingleBuyRequest $request): string { | ||
// return a token for a SingleBuy request | ||
return md5(sprintf('%s%u%.2F%s', | ||
Bancard::getPrivateKey(), | ||
$request->shop_process_id, | ||
$request->amount, | ||
$request->currency, | ||
)); | ||
} | ||
|
||
private static function post_single_buy_confirmations(ConfirmationRequest $request): string { | ||
// return a token for Confirmation request | ||
return md5(sprintf('%s%u%s', | ||
Bancard::getPrivateKey(), | ||
$request->shop_process_id, | ||
'get_confirmation', | ||
)); | ||
} | ||
|
||
private static function post_preauthorizations_confirm(PreauthorizationConfirmRequest $request): string { | ||
// return a token for Confirmation request | ||
return md5(sprintf('%s%u%s', | ||
Bancard::getPrivateKey(), | ||
$request->shop_process_id, | ||
'pre-authorization-confirm', | ||
)); | ||
} | ||
|
||
private static function post_single_buy_rollback(RollbackRequest $request): string { | ||
// return a token for Rollback request | ||
return md5(sprintf('%s%u%s%0.2F', | ||
Bancard::getPrivateKey(), | ||
$request->shop_process_id, | ||
'rollback', | ||
0, | ||
)); | ||
} | ||
|
||
private static function post_cards_new(CardsNewRequest $request): string { | ||
// return a token for a CardsNew request | ||
return md5(sprintf('%s%u%u%s', | ||
Bancard::getPrivateKey(), | ||
$request->card_id, | ||
$request->user_id, | ||
'request_new_card', | ||
)); | ||
} | ||
|
||
private static function post_users_cards(UsersCardsRequest $request): string { | ||
// return a token for a UserCards request | ||
return md5(sprintf('%s%u%s', | ||
Bancard::getPrivateKey(), | ||
$request->user_id, | ||
'request_user_cards', | ||
)); | ||
} | ||
|
||
private static function delete_users_cards(CardDeleteRequest $request): string { | ||
// return a token for a UserCards request | ||
return md5(sprintf('%s%s%u%s', | ||
Bancard::getPrivateKey(), | ||
'delete_card', | ||
$request->user_id, | ||
$request->card->alias_token, | ||
)); | ||
} | ||
|
||
private static function post_charge(ChargeRequest $request): string { | ||
// return a token for Charge request | ||
return md5(sprintf('%s%u%s%.2F%s%s', | ||
Bancard::getPrivateKey(), | ||
$request->shop_process_id, | ||
'charge', | ||
$request->amount, | ||
$request->currency, | ||
$request->card->alias_token, | ||
)); | ||
} | ||
|
||
} |
Oops, something went wrong.