-
Notifications
You must be signed in to change notification settings - Fork 17
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 #55 from zaporylie/epayment
Add EPayment API support
- Loading branch information
Showing
43 changed files
with
3,162 additions
and
76 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
/build | ||
.idea | ||
composer.lock | ||
/examples/settings.yml | ||
/examples/config.yml | ||
*.p12 | ||
*.pem | ||
*.crt | ||
|
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,27 @@ | ||
Test SDK with examples | ||
========== | ||
|
||
You can test different features supported by this SDK by simply starting PHP server: | ||
|
||
```bash | ||
$ php -S 0.0.0.0.8123 | ||
``` | ||
|
||
and visiting one of the php files in your browser, ex. http://127.0.0.1:8123/examples/authorization/01-token.php | ||
|
||
But before you start make sure to create a config file in the /examples directory, ie. | ||
|
||
```bash | ||
$ touch examples/config.yml | ||
``` | ||
|
||
and add required configuration | ||
|
||
```bash | ||
mode: test | ||
client_id: | ||
client_secret: | ||
subscription_key: | ||
merchant_serial_number: | ||
``` | ||
Happy testing! |
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,28 @@ | ||
<?php | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$yaml = new \Symfony\Component\Yaml\Parser(); | ||
$settings = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/../config.yml')); | ||
|
||
try { | ||
$http_client = new \GuzzleHttp\Client( | ||
['headers' => [ | ||
'Merchant-Serial-Number' => $settings['merchant_serial_number'], | ||
]] | ||
); | ||
$client = new \zaporylie\Vipps\Client($settings['client_id'], ['http_client' => $http_client]); | ||
$vipps = new \zaporylie\Vipps\Vipps($client); | ||
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']); | ||
$result = $authorization->getToken($settings['client_secret']); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
|
||
} | ||
catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
var_dump($e->getTraceAsString()); | ||
} | ||
|
||
?> |
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,35 @@ | ||
<?php | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$yaml = new \Symfony\Component\Yaml\Parser(); | ||
$settings = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/../config.yml')); | ||
|
||
try { | ||
$http_client = new \GuzzleHttp\Client( | ||
['headers' => [ | ||
'Merchant-Serial-Number' => $settings['merchant_serial_number'], | ||
]] | ||
); | ||
$client = new \zaporylie\Vipps\Client($settings['client_id'], ['http_client' => $http_client]); | ||
$vipps = new \zaporylie\Vipps\Vipps($client); | ||
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']); | ||
$authorization->getToken($settings['client_secret']); | ||
$payment = new \zaporylie\Vipps\Api\v1\EPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']); | ||
$result = $payment->createPayment((new \zaporylie\Vipps\Model\EPayment\v1\CreatePaymentRequest()) | ||
->setAmount((new \zaporylie\Vipps\Model\EPayment\v1\Amount())->setValue(1000)->setCurrency('NOK')) | ||
->setPaymentMethod((new \zaporylie\Vipps\Model\EPayment\v1\PaymentMethod())->setType('WALLET')) | ||
->setReference('test-12121212-3') | ||
->setUserFlow('WEB_REDIRECT') | ||
->setReturnUrl('https://eoncxehuh2o2qyq.m.pipedream.net'), 'costam2'); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
|
||
} | ||
catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
var_dump($e->getTraceAsString()); | ||
} | ||
|
||
?> |
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 | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$yaml = new \Symfony\Component\Yaml\Parser(); | ||
$settings = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/../config.yml')); | ||
|
||
try { | ||
$http_client = new \GuzzleHttp\Client( | ||
['headers' => [ | ||
'Merchant-Serial-Number' => $settings['merchant_serial_number'], | ||
]] | ||
); | ||
$client = new \zaporylie\Vipps\Client($settings['client_id'], ['http_client' => $http_client]); | ||
$vipps = new \zaporylie\Vipps\Vipps($client); | ||
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']); | ||
$authorization->getToken($settings['client_secret']); | ||
$payment = new \zaporylie\Vipps\Api\v1\EPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']); | ||
$result = $payment->getPayment('test-12121212-3'); | ||
$result = $payment->getPaymentEvents('test-12121212-3'); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
|
||
} | ||
catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
var_dump($e->getTraceAsString()); | ||
} | ||
|
||
?> |
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,30 @@ | ||
<?php | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$yaml = new \Symfony\Component\Yaml\Parser(); | ||
$settings = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/../config.yml')); | ||
|
||
try { | ||
$http_client = new \GuzzleHttp\Client( | ||
['headers' => [ | ||
'Merchant-Serial-Number' => $settings['merchant_serial_number'], | ||
]] | ||
); | ||
$client = new \zaporylie\Vipps\Client($settings['client_id'], ['http_client' => $http_client]); | ||
$vipps = new \zaporylie\Vipps\Vipps($client); | ||
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']); | ||
$authorization->getToken($settings['client_secret']); | ||
$payment = new \zaporylie\Vipps\Api\v1\EPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']); | ||
$result = $payment->cancelPayment('test-12121212-3', new \zaporylie\Vipps\Model\EPayment\v1\CancelModificationRequest(), 'costam'); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
|
||
} | ||
catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
var_dump($e->getTraceAsString()); | ||
} | ||
|
||
?> |
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,34 @@ | ||
<?php | ||
|
||
include __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$yaml = new \Symfony\Component\Yaml\Parser(); | ||
$settings = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/../config.yml')); | ||
|
||
try { | ||
$http_client = new \GuzzleHttp\Client( | ||
['headers' => [ | ||
'Merchant-Serial-Number' => $settings['merchant_serial_number'], | ||
]] | ||
); | ||
$client = new \zaporylie\Vipps\Client($settings['client_id'], ['http_client' => $http_client]); | ||
$vipps = new \zaporylie\Vipps\Vipps($client); | ||
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']); | ||
$authorization->getToken($settings['client_secret']); | ||
$payment = new \zaporylie\Vipps\Api\v1\EPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']); | ||
$result = $payment->capturePayment('test-12121212-3', (new \zaporylie\Vipps\Model\EPayment\v1\CaptureModificationRequest())->setModificationAmount((new \zaporylie\Vipps\Model\EPayment\v1\Amount())->setCurrency('NOK')->setValue(101)), 'costam3'); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
$result = $payment->refundPayment('test-12121212-3', (new \zaporylie\Vipps\Model\EPayment\v1\RefundModificationRequest())->setModificationAmount((new \zaporylie\Vipps\Model\EPayment\v1\Amount())->setCurrency('NOK')->setValue(100)), 'costam3'); | ||
echo '<pre>'; | ||
var_dump($result); | ||
echo '</pre>'; | ||
|
||
} | ||
catch (\Exception $e) { | ||
var_dump($e->getMessage()); | ||
var_dump($e->getTraceAsString()); | ||
} | ||
|
||
?> |
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,146 @@ | ||
<?php | ||
|
||
namespace zaporylie\Vipps\Api\v1; | ||
|
||
use zaporylie\Vipps\Api\ApiBase; | ||
use zaporylie\Vipps\Exceptions\Api\InvalidArgumentException; | ||
use zaporylie\Vipps\Model\EPayment\v1\CancelModificationRequest; | ||
use zaporylie\Vipps\Model\EPayment\v1\CaptureModificationRequest; | ||
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentRequest; | ||
use zaporylie\Vipps\Model\EPayment\v1\CreatePaymentResponse; | ||
use zaporylie\Vipps\Model\EPayment\v1\GetPaymentResponse; | ||
use zaporylie\Vipps\Model\EPayment\v1\PaymentAdjustResponse; | ||
use zaporylie\Vipps\Model\EPayment\v1\RefundModificationRequest; | ||
use zaporylie\Vipps\Resource\EPayment\v1\CapturePayment; | ||
use zaporylie\Vipps\Resource\EPayment\v1\CreatePayment; | ||
use zaporylie\Vipps\Resource\EPayment\v1\GetPayment; | ||
use zaporylie\Vipps\Resource\EPayment\v1\GetPaymentEvents; | ||
use zaporylie\Vipps\Resource\EPayment\v1\CancelPayment; | ||
use zaporylie\Vipps\Resource\EPayment\v1\RefundPayment; | ||
use zaporylie\Vipps\Resource\IdempotencyKeyFactory; | ||
use zaporylie\Vipps\VippsInterface; | ||
|
||
/** | ||
* Class EPayment | ||
* | ||
* @package Vipps\Api | ||
*/ | ||
class EPayment extends ApiBase implements EPaymentInterface | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $merchantSerialNumber; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $version; | ||
|
||
/** | ||
* Gets merchantSerialNumber value. | ||
* | ||
* @return string | ||
*/ | ||
public function getMerchantSerialNumber() | ||
{ | ||
if (empty($this->merchantSerialNumber)) { | ||
throw new InvalidArgumentException('Missing merchant serial number'); | ||
} | ||
return $this->merchantSerialNumber; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getVersion() | ||
{ | ||
return $this->version; | ||
} | ||
|
||
/** | ||
* Payment constructor. | ||
* | ||
* Payments API needs one extra param - merchant serial number. | ||
* | ||
* @param \zaporylie\Vipps\VippsInterface $app | ||
* @param string $subscription_key | ||
* @param $merchant_serial_number | ||
*/ | ||
public function __construct( | ||
VippsInterface $app, | ||
$subscription_key, | ||
$merchant_serial_number | ||
) { | ||
parent::__construct($app, $subscription_key); | ||
$this->merchantSerialNumber = $merchant_serial_number; | ||
$this->version = 'v1'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function createPayment(CreatePaymentRequest $request, ?string $idempotency_key): CreatePaymentResponse | ||
{ | ||
// Ensure idempotency key is set. | ||
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate(); | ||
// @todo: Validate data. | ||
$resource = new CreatePayment($this->app, $this->getSubscriptionKey(), $idempotency_key, $request); | ||
return $resource->call(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPayment(string $reference): GetPaymentResponse | ||
{ | ||
$resource = new GetPayment($this->app, $this->getSubscriptionKey(), $reference); | ||
return $resource->call(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPaymentEvents(string $reference): array | ||
{ | ||
$resource = new GetPaymentEvents($this->app, $this->getSubscriptionKey(), $reference); | ||
return $resource->call(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function cancelPayment( | ||
string $reference, | ||
CancelModificationRequest $request, | ||
?string $idempotency_key | ||
): PaymentAdjustResponse { | ||
// Ensure idempotency key is set. | ||
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate(); | ||
$resource = new CancelPayment($this->app, $this->getSubscriptionKey(), $idempotency_key, $reference, $request); | ||
return $resource->call(); | ||
} | ||
|
||
public function capturePayment( | ||
string $reference, | ||
CaptureModificationRequest $request, | ||
?string $idempotency_key | ||
): PaymentAdjustResponse { | ||
// Ensure idempotency key is set. | ||
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate(); | ||
$resource = new CapturePayment($this->app, $this->getSubscriptionKey(), $idempotency_key, $reference, $request); | ||
return $resource->call(); | ||
} | ||
|
||
public function refundPayment( | ||
string $reference, | ||
RefundModificationRequest $request, | ||
?string $idempotency_key | ||
): PaymentAdjustResponse { | ||
// Ensure idempotency key is set. | ||
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate(); | ||
$resource = new RefundPayment($this->app, $this->getSubscriptionKey(), $idempotency_key, $reference, $request); | ||
return $resource->call(); | ||
} | ||
} |
Oops, something went wrong.