Skip to content

Commit

Permalink
Merge pull request #55 from zaporylie/epayment
Browse files Browse the repository at this point in the history
Add EPayment API support
  • Loading branch information
zaporylie authored Jul 25, 2024
2 parents a73212c + 766b750 commit 27a300e
Show file tree
Hide file tree
Showing 43 changed files with 3,162 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/build
.idea
composer.lock
/examples/settings.yml
/examples/config.yml
*.p12
*.pem
*.crt
Expand Down
27 changes: 27 additions & 0 deletions examples/README.md
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!
28 changes: 28 additions & 0 deletions examples/authorization/01-token.php
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());
}

?>
35 changes: 35 additions & 0 deletions examples/payment/01-epayment-init.php
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());
}

?>
31 changes: 31 additions & 0 deletions examples/payment/02-epayment-get.php
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());
}

?>
30 changes: 30 additions & 0 deletions examples/payment/03-epayment-adjust-cancel.php
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());
}

?>
34 changes: 34 additions & 0 deletions examples/payment/04-epayment-adjust-modify.php
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());
}

?>
146 changes: 146 additions & 0 deletions src/Api/v1/EPayment.php
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();
}
}
Loading

0 comments on commit 27a300e

Please sign in to comment.