Skip to content

Commit

Permalink
Add strong typing in the RecurringPayment related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zaporylie committed Jul 30, 2024
1 parent 56f5a90 commit 2c22a48
Show file tree
Hide file tree
Showing 34 changed files with 995 additions and 357 deletions.
12 changes: 6 additions & 6 deletions examples/payment/01-epayment-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
$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]);
$http_client = new \GuzzleHttp\Client();
$client = new \zaporylie\Vipps\Client($settings['client_id'], [
'http_client' => $http_client,
'vipps_system_name' => 'vipps_zaporylie_example',
'vipps_system_version' => \zaporylie\Vipps\Client::VERSION,
]);
$vipps = new \zaporylie\Vipps\Vipps($client);
$authorization = new \zaporylie\Vipps\Api\Authorization($vipps, $settings['subscription_key']);
$authorization->getToken($settings['client_secret']);
Expand Down
39 changes: 39 additions & 0 deletions examples/recurring_payment/01-recurring-payment-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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();
$client = new \zaporylie\Vipps\Client($settings['client_id'], [
'http_client' => $http_client,
'vipps_system_name' => 'vipps_zaporylie_example',
'vipps_system_version' => \zaporylie\Vipps\Client::VERSION,
]);
$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\v3\RecurringPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']);
$result = $payment->createAgreement((new \zaporylie\Vipps\Model\RecurringPayment\v3\RequestCreateAgreement())
->setPricing((new \zaporylie\Vipps\Model\RecurringPayment\v3\Pricing())
->setCurrency('NOK')
->setType('FLEXIBLE')
->setAmount(1000))
->setMerchantRedirectUrl('https://eoncxehuh2o2qyq.m.pipedream.net')
->setMerchantAgreementUrl('https://eoncxehuh2o2qyq.m.pipedream.net')
->setProductName('Recurring Product that is being purchased'));
echo '<pre>';
var_dump($result);
echo '</pre>';

}
catch (\Exception $e) {
echo '<pre>';
var_dump($e->getMessage());
var_dump($e->getTraceAsString());
echo '</pre>';
}

?>
32 changes: 32 additions & 0 deletions examples/recurring_payment/01-recurring-payment-get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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();
$client = new \zaporylie\Vipps\Client($settings['client_id'], [
'http_client' => $http_client,
'vipps_system_name' => 'vipps_zaporylie_example',
'vipps_system_version' => \zaporylie\Vipps\Client::VERSION,
]);
$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\v3\RecurringPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']);
$result = $payment->getAgreement('agr_KG6k3X3');
echo '<pre>';
var_dump($result);
echo '</pre>';

}
catch (\Exception $e) {
echo '<pre>';
var_dump($e->getMessage());
var_dump($e->getTraceAsString());
echo '</pre>';
}

?>
44 changes: 44 additions & 0 deletions examples/recurring_payment/02-recurring-payment-create-charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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();
$client = new \zaporylie\Vipps\Client($settings['client_id'], [
'http_client' => $http_client,
'vipps_system_name' => 'vipps_zaporylie_example',
'vipps_system_version' => \zaporylie\Vipps\Client::VERSION,
]);
$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\v3\RecurringPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']);
$result = $payment->createCharge('agr_KG6k3X3', (new \zaporylie\Vipps\Model\RecurringPayment\v3\RequestCreateCharge())
->setAmount(1000)
->setDescription('Ok, that worked')
->setDue(new DateTime('+1 day'))
->setTransactionType('DIRECT_CAPTURE'));
echo '<pre>';
var_dump($result);
echo '</pre>';
$result = $payment->createCharge('agr_KG6k3X3', (new \zaporylie\Vipps\Model\RecurringPayment\v3\RequestCreateCharge())
->setAmount(1100)
->setType('UNSCHEDULED')
->setDescription('Ok, that worked too')
->setTransactionType('DIRECT_CAPTURE'));
echo '<pre>';
var_dump($result);
echo '</pre>';

}
catch (\Exception $e) {
echo '<pre>';
var_dump($e->getMessage());
var_dump($e->getTraceAsString());
echo '</pre>';
}

?>
32 changes: 32 additions & 0 deletions examples/recurring_payment/02-recurring-payment-get-charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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();
$client = new \zaporylie\Vipps\Client($settings['client_id'], [
'http_client' => $http_client,
'vipps_system_name' => 'vipps_zaporylie_example',
'vipps_system_version' => \zaporylie\Vipps\Client::VERSION,
]);
$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\v3\RecurringPayment($vipps, $settings['subscription_key'], $settings['merchant_serial_number']);
$result = $payment->getCharge('agr_KG6k3X3','chr-gTHPbNE');
echo '<pre>';
var_dump($result);
echo '</pre>';

}
catch (\Exception $e) {
echo '<pre>';
var_dump($e->getMessage());
var_dump($e->getTraceAsString());
echo '</pre>';
}

?>
78 changes: 50 additions & 28 deletions src/Api/v3/RecurringPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

use zaporylie\Vipps\Api\ApiBase;
use zaporylie\Vipps\Exceptions\Api\InvalidArgumentException;
use zaporylie\Vipps\Model\RecurringPayment\v3\RequestCaptureCharge;
use zaporylie\Vipps\Model\RecurringPayment\v3\RequestCreateAgreement;
use zaporylie\Vipps\Model\RecurringPayment\v3\RequestCreateCharge;
use zaporylie\Vipps\Model\RecurringPayment\v3\RequestRefundCharge;
use zaporylie\Vipps\Model\RecurringPayment\v3\RequestUpdateAgreement;
use zaporylie\Vipps\Model\RecurringPayment\v3\ResponseCreateAgreement;
use zaporylie\Vipps\Model\RecurringPayment\v3\ResponseCreateCharge;
use zaporylie\Vipps\Model\RecurringPayment\v3\ResponseGetAgreement;
use zaporylie\Vipps\Model\RecurringPayment\v3\ResponseGetCharge;
use zaporylie\Vipps\Resource\IdempotencyKeyFactory;
use zaporylie\Vipps\Resource\RecurringPayment\v3\CancelCharge;
use zaporylie\Vipps\Resource\RecurringPayment\v3\CaptureCharge;
use zaporylie\Vipps\Resource\RecurringPayment\v3\CreateAgreement;
Expand Down Expand Up @@ -68,18 +74,17 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function createAgreement(RequestCreateAgreement $request)
{
$resource = new CreateAgreement($this->app, $this->getSubscriptionKey(), $request);
public function createAgreement(RequestCreateAgreement $request, ?string $idempotency_key): ResponseCreateAgreement {
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new CreateAgreement($this->app, $this->getSubscriptionKey(), $idempotency_key, $request);
$response = $resource->call();
return $response;
}

/**
* {@inheritdoc}
*/
public function getAgreements()
{
public function getAgreements(): array {
$resource = new GetAgreements($this->app, $this->getSubscriptionKey());
$response = $resource->call();
return $response;
Expand All @@ -88,8 +93,7 @@ public function getAgreements()
/**
* {@inheritdoc}
*/
public function getAgreement($agreement_id)
{
public function getAgreement(string $agreement_id): ResponseGetAgreement {
$resource = new GetAgreement($this->app, $this->getSubscriptionKey(), $agreement_id);
$response = $resource->call();
return $response;
Expand All @@ -98,18 +102,20 @@ public function getAgreement($agreement_id)
/**
* {@inheritdoc}
*/
public function updateAgreement($agreement_id, RequestUpdateAgreement $request)
{
$resource = new UpdateAgreement($this->app, $this->getSubscriptionKey(), $agreement_id, $request);
public function updateAgreement(
string $agreement_id,
RequestUpdateAgreement $request,
?string $idempotency_key
): void {
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new UpdateAgreement($this->app, $this->getSubscriptionKey(), $agreement_id, $idempotency_key, $request);
$response = $resource->call();
return $response;
}

/**
* {@inheritDoc}
*/
public function getCharges($agreement_id)
{
public function getCharges($agreement_id): array {
$resource = new GetCharges($this->app, $this->getSubscriptionKey(), $agreement_id);
$response = $resource->call();
return $response;
Expand All @@ -118,8 +124,7 @@ public function getCharges($agreement_id)
/**
* {@inheritDoc}
*/
public function getCharge($agreement_id, $charge_id)
{
public function getCharge(string $agreement_id, string $charge_id): ResponseGetCharge {
$resource = new GetCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $charge_id);
$response = $resource->call();
return $response;
Expand All @@ -128,46 +133,63 @@ public function getCharge($agreement_id, $charge_id)
/**
* {@inheritDoc}
*/
public function createCharge($agreement_id, RequestCreateCharge $request)
{
$resource = new CreateCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $request);
public function createCharge(
string $agreement_id,
RequestCreateCharge $request,
?string $idempotency_key
): ResponseCreateCharge {
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new CreateCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $idempotency_key, $request);
$response = $resource->call();
return $response;
}

/**
* {@inheritDoc}
*/
public function cancelCharge($agreement_id, $charge_id)
public function cancelCharge(
string $agreement_id,
string $charge_id,
?string $idempotency_key
):void
{
$resource = new CancelCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $charge_id);
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new CancelCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $charge_id, $idempotency_key);
$response = $resource->call();
return $response;
}

/**
* {@inheritDoc}
*/
public function captureCharge($agreement_id, $charge_id)
{
$resource = new CaptureCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $charge_id);
public function captureCharge(
$agreement_id,
$charge_id,
RequestCaptureCharge $request,
?string $idempotency_key
): void {
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new CaptureCharge($this->app, $this->getSubscriptionKey(), $agreement_id, $charge_id, $idempotency_key, $request);
$response = $resource->call();
return $response;
}

/**
* {@inheritDoc}
*/
public function refundCharge($agreement_id, $charge_id, RequestRefundCharge $requestObject)
{
public function refundCharge(
string $agreement_id,
string $charge_id,
RequestRefundCharge $requestObject,
?string $idempototency_key
): void {
$idempotency_key = $idempotency_key ?? IdempotencyKeyFactory::generate();
$resource = new RefundCharge(
$this->app,
$this->getSubscriptionKey(),
$agreement_id,
$charge_id,
$idempototency_key,
$requestObject
);
$response = $resource->call();
return $response;
}
}
Loading

0 comments on commit 2c22a48

Please sign in to comment.