Skip to content

Commit

Permalink
Google Pay methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janlanger committed Aug 2, 2021
1 parent 069ff4c commit 7e343e2
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Call/ApplePay/StartApplePayRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeImmutable;
use SlevomatCsobGateway\Api\ApiClient;
use SlevomatCsobGateway\Call\InvalidJsonPayloadException;
use SlevomatCsobGateway\Call\PaymentResponse;
use SlevomatCsobGateway\Call\PaymentStatus;
use SlevomatCsobGateway\Call\ResultCode;
Expand Down
63 changes: 63 additions & 0 deletions src/Call/GooglePay/GooglePayInfoRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace SlevomatCsobGateway\Call\GooglePay;

use DateTimeImmutable;
use SlevomatCsobGateway\Api\ApiClient;
use SlevomatCsobGateway\Call\ResultCode;
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;

class GooglePayInfoRequest
{

/** @var string */
private $merchantId;

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


public function send(ApiClient $apiClient): GooglePayInfoResponse
{
$requestData = [
'merchantId' => $this->merchantId,
];

$response = $apiClient->post(
'googlepay/info',
$requestData,
new SignatureDataFormatter([
'merchantId' => null,
'dttm' => null,
]),
new SignatureDataFormatter([
'dttm' => null,
'resultCode' => null,
'resultMessage' => null,
'checkoutParams' => [
'apiVersion' => null,
'apiVersionMinor' => null,
'allowedCardNetworks' => [],
'allowedCardAuthMethods' => [],
'googlePayMerchantId' => null,
'merchantName' => null,
'totalPriceStatus' => null,
],
])
);

/** @var mixed[] $data */
$data = $response->getData();
$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);

return new GooglePayInfoResponse(
$responseDateTime,
ResultCode::get($data['resultCode']),
$data['resultMessage'] ?? '',
$data['checkoutParams'] ?? []
);
}

}
60 changes: 60 additions & 0 deletions src/Call/GooglePay/GooglePayInfoResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace SlevomatCsobGateway\Call\GooglePay;

use DateTimeImmutable;
use SlevomatCsobGateway\Call\ResultCode;

class GooglePayInfoResponse
{

/** @var DateTimeImmutable */
private $responseDateTime;

/** @var ResultCode */
private $resultCode;

/** @var string */
private $resultMessage;

/** @var mixed[] */
private $checkoutParams;

/**
* @param DateTimeImmutable $responseDateTime
* @param ResultCode $resultCode
* @param string $resultMessage
* @param mixed[] $checkoutParams
*/
public function __construct(DateTimeImmutable $responseDateTime, ResultCode $resultCode, string $resultMessage, array $checkoutParams)
{
$this->responseDateTime = $responseDateTime;
$this->resultCode = $resultCode;
$this->resultMessage = $resultMessage;
$this->checkoutParams = $checkoutParams;
}

public function getResponseDateTime(): DateTimeImmutable
{
return $this->responseDateTime;
}

public function getResultCode(): ResultCode
{
return $this->resultCode;
}

public function getResultMessage(): string
{
return $this->resultMessage;
}

/**
* @return mixed[]
*/
public function getCheckoutParams(): array
{
return $this->checkoutParams;
}

}
108 changes: 108 additions & 0 deletions src/Call/GooglePay/InitGooglePayRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php declare(strict_types = 1);

namespace SlevomatCsobGateway\Call\GooglePay;

use DateTimeImmutable;
use SlevomatCsobGateway\Api\ApiClient;
use SlevomatCsobGateway\Call\PaymentResponse;
use SlevomatCsobGateway\Call\PaymentStatus;
use SlevomatCsobGateway\Call\ResultCode;
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
use SlevomatCsobGateway\Price;
use SlevomatCsobGateway\Validator;
use function base64_encode;

class InitGooglePayRequest
{

/** @var string */
private $merchantId;

/** @var string */
private $orderId;

/** @var string|null */
private $clientIp;

/** @var Price */
private $totalPrice;

/** @var bool */
private $closePayment;

/** @var string|null */
private $merchantData;

public function __construct(
string $merchantId,
string $orderId,
string $clientIp,
Price $totalPrice,
bool $closePayment,
?string $merchantData
)
{
Validator::checkOrderId($orderId);
if ($merchantData !== null) {
Validator::checkMerchantData($merchantData);
}

$this->merchantId = $merchantId;
$this->orderId = $orderId;
$this->clientIp = $clientIp;
$this->totalPrice = $totalPrice;
$this->closePayment = $closePayment;
$this->merchantData = $merchantData;
}

public function send(ApiClient $apiClient): PaymentResponse
{
$requestData = [
'merchantId' => $this->merchantId,
'orderNo' => $this->orderId,
'totalAmount' => $this->totalPrice->getAmount(),
'currency' => $this->totalPrice->getCurrency()->getValue(),
'closePayment' => $this->closePayment,
'clientIp' => $this->clientIp,
];

if ($this->merchantData !== null) {
$requestData['merchantData'] = base64_encode($this->merchantData);
}

$response = $apiClient->post(
'googlepay/init',
$requestData,
new SignatureDataFormatter([
'merchantId' => null,
'orderNo' => null,
'dttm' => null,
'clientIp' => null,
'totalAmount' => null,
'currency' => null,
'closePayment' => null,
'merchantData' => null,
]),
new SignatureDataFormatter([
'payId' => null,
'dttm' => null,
'resultCode' => null,
'resultMessage' => null,
'paymentStatus' => null,
])
);

/** @var mixed[] $data */
$data = $response->getData();
$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);

return new PaymentResponse(
$data['payId'],
$responseDateTime,
ResultCode::get($data['resultCode']),
$data['resultMessage'],
isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
);
}

}
88 changes: 88 additions & 0 deletions src/Call/GooglePay/StartGooglePayRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types = 1);

namespace SlevomatCsobGateway\Call\GooglePay;

use DateTimeImmutable;
use SlevomatCsobGateway\Api\ApiClient;
use SlevomatCsobGateway\Call\InvalidJsonPayloadException;
use SlevomatCsobGateway\Call\PaymentResponse;
use SlevomatCsobGateway\Call\PaymentStatus;
use SlevomatCsobGateway\Call\ResultCode;
use SlevomatCsobGateway\Crypto\SignatureDataFormatter;
use SlevomatCsobGateway\Validator;
use function base64_encode;

class StartGooglePayRequest
{

/** @var string */
private $merchantId;

/** @var string */
private $payId;

/** @var array|mixed[] */
private $payload;

/**
* @param string $merchantId
* @param string $payId
* @param mixed[] $payload Complete payload from Google Pay JS API, containing paymentMethodData.tokenizationData.token
*/
public function __construct(
string $merchantId,
string $payId,
array $payload
)
{
Validator::checkPayId($payId);

$this->merchantId = $merchantId;
$this->payId = $payId;
$this->payload = $payload;
}

public function send(ApiClient $apiClient): PaymentResponse
{
$payloadData = $this->payload['paymentMethodData']['tokenizationData']['token'] ?? null;
if ($payloadData === null) {
throw new InvalidJsonPayloadException('Missing token in Google Pay payload.');
}
$requestData = [
'merchantId' => $this->merchantId,
'payId' => $this->payId,
'payload' => base64_encode((string) $payloadData),
];

$response = $apiClient->post(
'googlepay/start',
$requestData,
new SignatureDataFormatter([
'merchantId' => null,
'payId' => null,
'payload' => null,
'dttm' => null,
]),
new SignatureDataFormatter([
'payId' => null,
'dttm' => null,
'resultCode' => null,
'resultMessage' => null,
'paymentStatus' => null,
])
);

/** @var mixed[] $data */
$data = $response->getData();
$responseDateTime = DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']);

return new PaymentResponse(
$data['payId'],
$responseDateTime,
ResultCode::get($data['resultCode']),
$data['resultMessage'],
isset($data['paymentStatus']) ? PaymentStatus::get($data['paymentStatus']) : null
);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace SlevomatCsobGateway\Call\ApplePay;
namespace SlevomatCsobGateway\Call;

use Exception;

Expand Down
40 changes: 40 additions & 0 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use SlevomatCsobGateway\Call\ClosePaymentRequest;
use SlevomatCsobGateway\Call\CustomerInfoRequest;
use SlevomatCsobGateway\Call\EchoRequest;
use SlevomatCsobGateway\Call\GooglePay\GooglePayInfoRequest;
use SlevomatCsobGateway\Call\GooglePay\InitGooglePayRequest;
use SlevomatCsobGateway\Call\GooglePay\StartGooglePayRequest;
use SlevomatCsobGateway\Call\InitPaymentRequest;
use SlevomatCsobGateway\Call\MallPay\CancelMallPayRequest;
use SlevomatCsobGateway\Call\MallPay\InitMallPayRequest;
Expand Down Expand Up @@ -377,4 +380,41 @@ public function createMallPayRefundRequest(
);
}

public function createGooglePayInfoRequest(): GooglePayInfoRequest
{
return new GooglePayInfoRequest($this->merchantId);
}

public function createGooglePayInitRequest(
string $orderId,
string $clientIp,
Price $totalPrice,
bool $closePayment,
?string $merchantData
): InitGooglePayRequest
{
return new InitGooglePayRequest(
$this->merchantId,
$orderId,
$clientIp,
$totalPrice,
$closePayment,
$merchantData
);
}

/**
* @param string $payId
* @param mixed[] $payload Complete payload from Google Pay JS API, containing paymentMethodData.tokenizationData.token
* @return StartGooglePayRequest
*/
public function createGooglePayStartRequest(string $payId, array $payload): StartGooglePayRequest
{
return new StartGooglePayRequest(
$this->merchantId,
$payId,
$payload
);
}

}
Loading

0 comments on commit 7e343e2

Please sign in to comment.