Skip to content

Commit

Permalink
Add Transaction/InitializeRequest and Transaction/AuthorizeRequest
Browse files Browse the repository at this point in the history
* Implement Transaction Initialize including test and example

* Correct annotation in InitializeRequest.php

* Fix constructor codestyle InitializeRequest.php

* Fix Code Style with php-cs-fixer

* Add Transaction Authorize Request and Response

* Add Transaction Authorize test and example

* Change Line Endings

* Delete non-existing property RedirectUrl from InitializeResponse.php in Transaction Namespace

* Fix Annotations in AuthorizeResponse

---------

Co-authored-by: Jozo Dra <[email protected]>
Co-authored-by: jdraguljic <[email protected]>
Co-authored-by: sagyemang <[email protected]>
  • Loading branch information
4 people authored May 9, 2023
1 parent 3c0aba4 commit bc56830
Show file tree
Hide file tree
Showing 10 changed files with 839 additions and 1 deletion.
54 changes: 54 additions & 0 deletions example/Transaction/example-authorize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

use Ticketpark\SaferpayJson\Request\Exception\SaferpayErrorException;
use Ticketpark\SaferpayJson\Request\RequestConfig;
use Ticketpark\SaferpayJson\Request\Transaction\AuthorizeRequest;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../credentials.php';

// A token you received after initializing a transaction (see example-initialize.php)

$token = 'xxx';

// -----------------------------
// Step 1:
// Prepare the authorize request
// See https://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Authorize

$requestConfig = new RequestConfig(
$apiKey,
$apiSecret,
$customerId,
true
);

// -----------------------------
// Step 2:
// Create the request with required data

$authorizeRequest = new AuthorizeRequest(
$requestConfig,
$token,
);

// Note: More data can be provided to InitializeRequest with setters,
// for example: $authorizeRequest->setCondition()
// See Saferpay documentation for available options.

// -----------------------------
// Step 3:
// Execute and check for successful response

try {
$response = $authorizeRequest->execute();
} catch (SaferpayErrorException $e) {
die ($e->getErrorResponse()->getErrorMessage());
}

echo 'The transaction has been successful! Transaction id: ' . $response->getTransaction()->getId()."\n";

// -----------------------------
// Step 4:
// Capture the transaction to get the cash flowing.
// See ../Transaction/example-capture.php
79 changes: 79 additions & 0 deletions example/Transaction/example-initialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);

use Ticketpark\SaferpayJson\Request\Exception\SaferpayErrorException;
use Ticketpark\SaferpayJson\Request\RequestConfig;
use Ticketpark\SaferpayJson\Request\Container;
use Ticketpark\SaferpayJson\Request\Transaction\InitializeRequest;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../credentials.php';

// -----------------------------
// Step 1:
// Initialize the required transaction data
// See https://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Initialize

$requestConfig = new RequestConfig(
$apiKey,
$apiSecret,
$customerId,
true
);

$amount = new Container\Amount(
5000, // amount in cents
'CHF'
);

$payment = new Container\Payment($amount);
$payment->setDescription('Order No. 12840');

$returnUrls = new Container\ReturnUrls(
'http://www.mysite.ch/success?orderId=12840',
'http://www.mysite.ch/fail'
);

// -----------------------------
// Step 2:
// Create the request with required data

$initializeRequest = new InitializeRequest(
$requestConfig,
$terminalId,
$payment,
$returnUrls
);

// Note: More data can be provided to InitializeRequest with setters,
// for example: $initializeRequest->setPayer()
// See Saferpay documentation for available options.

// -----------------------------
// Step 3:
// Execute and check for successful response

try {
$response = $initializeRequest->execute();
} catch (SaferpayErrorException $e) {
die ($e->getErrorResponse()->getErrorMessage());
}

// -----------------------------
// Step 4:
// Save the response token, you will need it later to verify the payment (see step 7)
echo 'Payment token: ' . $response->getToken() . "<br>\n";

// -----------------------------
// Step 5:
// Redirect to the payment page
echo 'Redirect to: ' . $response->getRedirect()->getRedirectUrl() ."<br>\n";

// -----------------------------
// Step 6:
// Fill in test payment page. For dummy credit card numbers see
// https://saferpay.github.io/sndbx/paymentmeans.html

// -----------------------------
// Step 7:
// On success page and notification url, assert that the payment has been successful.
// TODO: -> see example-assert.php
102 changes: 102 additions & 0 deletions lib/SaferpayJson/Request/Container/Wallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Ticketpark\SaferpayJson\Request\Container;

use JMS\Serializer\Annotation\SerializedName;

final class Wallet
{
public const PAYMENT_METHOD_ALIPAY = "ALIPAY";
public const PAYMENT_METHOD_AMEX = "AMEX";
public const PAYMENT_METHOD_BANCONTACT = "BANCONTACT";
public const PAYMENT_METHOD_BONUS = "BONUS";
public const PAYMENT_METHOD_DINERS = "DINERS";
public const PAYMENT_METHOD_DIRECTDEBIT = "DIRECTDEBIT";
public const PAYMENT_METHOD_EPRZELEWY = "EPRZELEWY";
public const PAYMENT_METHOD_EPS = "EPS";
public const PAYMENT_METHOD_GIROPAY = "GIROPAY";
public const PAYMENT_METHOD_IDEAL = "IDEAL";
public const PAYMENT_METHOD_INVOICE = "INVOICE";
public const PAYMENT_METHOD_JCB = "JCB";
public const PAYMENT_METHOD_MAESTRO = "MAESTRO";
public const PAYMENT_METHOD_MASTERCARD = "MASTERCARD";
public const PAYMENT_METHOD_MYONE = "MYONE";
public const PAYMENT_METHOD_PAYPAL = "PAYPAL";
public const PAYMENT_METHOD_PAYDIREKT = "PAYDIREKT";
public const PAYMENT_METHOD_POSTCARD = "POSTCARD";
public const PAYMENT_METHOD_POSTFINANCE = "POSTFINANCE";
public const PAYMENT_METHOD_SAFERPAYTEST = "SAFERPAYTEST";
public const PAYMENT_METHOD_SOFORT = "SOFORT";
public const PAYMENT_METHOD_TWINT = "TWINT";
public const PAYMENT_METHOD_UNIONPAY = "UNIONPAY";
public const PAYMENT_METHOD_VISA = "VISA";
public const PAYMENT_METHOD_VPAY = "VPAY";
public const PAYMENT_METHOD_KLARNA = "KLARNA";

/**
* @var string
* @SerializedName("Wallet")
*/
private $type;

/**
* @var array<string>|null
* @SerializedName("PaymentMethods")
*/
private $paymentMethods;

/**
* @var bool|null
* @SerializedName("RequestDeliveryAddress")
*/
private $requestDeliveryAddress;

/**
* @var bool|null
* @SerializedName("EnableAmountAdjustment")
*/
private $enableAmountAdjustment;

public function __construct(string $type)
{
$this->type = $type;
}

public function setPaymentMethods(?array $paymentMethods): Wallet
{
$this->paymentMethods = $paymentMethods;
return $this;
}

public function setRequestDeliveryAddress(?bool $requestDeliveryAddress): Wallet
{
$this->requestDeliveryAddress = $requestDeliveryAddress;
return $this;
}

public function setEnableAmountAdjustment(?bool $enableAmountAdjustment): Wallet
{
$this->enableAmountAdjustment = $enableAmountAdjustment;
return $this;
}

public function getType(): string
{
return $this->type;
}

public function getPaymentMethods(): ?array
{
return $this->paymentMethods;
}

public function getRequestDeliveryAddress(): ?bool
{
return $this->requestDeliveryAddress;
}

public function getEnableAmountAdjustment(): ?bool
{
return $this->enableAmountAdjustment;
}
}
1 change: 0 additions & 1 deletion lib/SaferpayJson/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected function doExecute(): Response
$statusCode = $response->getStatusCode();

if ($statusCode >= 400 && $statusCode < 500) {

/** @var ErrorResponse $errorResponse */
$errorResponse = $this->getSerializer()->deserialize(
(string) $response->getBody(),
Expand Down
94 changes: 94 additions & 0 deletions lib/SaferpayJson/Request/Transaction/AuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Ticketpark\SaferpayJson\Request\Transaction;

use JMS\Serializer\Annotation\SerializedName;
use Ticketpark\SaferpayJson\Request\Container\RegisterAlias;
use Ticketpark\SaferpayJson\Request\Request;
use Ticketpark\SaferpayJson\Request\RequestCommonsTrait;
use Ticketpark\SaferpayJson\Request\RequestConfig;
use Ticketpark\SaferpayJson\Response\Transaction\AuthorizeResponse;

final class AuthorizeRequest extends Request
{
use RequestCommonsTrait;
public const API_PATH = '/Payment/v1/Transaction/Authorize';
public const RESPONSE_CLASS = AuthorizeResponse::class;

/**
* @var string
* @SerializedName("Token")
*/
private $token;

/**
* @var string|null
* @SerializedName("Condition")
*/
private $condition;

/**
* @var string|null
* @SerializedName("VerificationCode")
*/
private $verificationCode;

/**
* @var RegisterAlias|null
* @SerializedName("RegisterAlias")
*/
private $registerAlias;

public function __construct(
RequestConfig $requestConfig,
string $token
) {
$this->token = $token;

parent::__construct($requestConfig);
}

public function getToken(): string
{
return $this->token;
}

public function getCondition(): ?string
{
return $this->condition;
}

public function getVerificationCode(): ?string
{
return $this->verificationCode;
}

public function getRegisterAlias(): ?RegisterAlias
{
return $this->registerAlias;
}

public function setCondition(?string $condition): void
{
$this->condition = $condition;
}

public function setVerificationCode(?string $verificationCode): void
{
$this->verificationCode = $verificationCode;
}

public function setRegisterAlias(?RegisterAlias $registerAlias): void
{
$this->registerAlias = $registerAlias;
}
public function execute(): AuthorizeResponse
{
/** @var AuthorizeResponse $response */
$response = $this->doExecute();

return $response;
}
}
Loading

0 comments on commit bc56830

Please sign in to comment.