Skip to content

Commit

Permalink
Merge pull request #1 from hschimpf/1.x-dev
Browse files Browse the repository at this point in the history
Initial release
  • Loading branch information
hschimpf authored Feb 24, 2023
2 parents 844fabe + 571da5d commit 14b54e0
Show file tree
Hide file tree
Showing 67 changed files with 2,582 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/logs/
/vendor/
/.idea/
/.vscode/
/tests/.env

composer.lock
*.cache
9 changes: 9 additions & 0 deletions README.md
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 ...

45 changes: 45 additions & 0 deletions composer.json
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
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
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>
133 changes: 133 additions & 0 deletions src/Bancard.php
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;
}

}
31 changes: 31 additions & 0 deletions src/Builders/CardsRequests.php
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);
}

}
18 changes: 18 additions & 0 deletions src/Builders/SingleBuyRequests.php
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);
}

}
109 changes: 109 additions & 0 deletions src/Builders/TokenBuilder.php
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,
));
}

}
Loading

0 comments on commit 14b54e0

Please sign in to comment.