Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vipps Checkout #46

Open
wants to merge 21 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions src/Api/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace zaporylie\Vipps\Api;

use zaporylie\Vipps\Model\Checkout\Logistics;
use zaporylie\Vipps\Model\Checkout\MerchantInfo;
use zaporylie\Vipps\Model\Checkout\PaymentTransaction;
use zaporylie\Vipps\Model\Checkout\PrefillCustomer;
use zaporylie\Vipps\Model\Checkout\RequestAmount;
use zaporylie\Vipps\Model\Checkout\RequestInitiateSession;
use zaporylie\Vipps\Model\Checkout\ResponseCancelSession;
use zaporylie\Vipps\Model\Checkout\ResponseGetSessionDetails;
use zaporylie\Vipps\Model\Checkout\RequestCancelSession;
use zaporylie\Vipps\Model\Checkout\ResponseInitiateSession;
use zaporylie\Vipps\Resource\Checkout\CancelSession;
use zaporylie\Vipps\Resource\Checkout\GetSessionDetails;
use zaporylie\Vipps\Resource\Checkout\InitiateSession;

/**
* Class Checkout.
*
* @package Vipps\Api
*/
class Checkout extends ApiBase implements CheckoutInterface {

/**
* {@inheritdoc}
*/
public function initiateSession(
string $client_secret,
ivanruzak marked this conversation as resolved.
Show resolved Hide resolved
string $callback_prefix,
string $return_url,
string $callback_auth_token,
RequestAmount $amount,
array $options = [],
bool $contact_fields = TRUE,
bool $address_fields = TRUE
ivanruzak marked this conversation as resolved.
Show resolved Hide resolved
): ResponseInitiateSession
{
$request = (new RequestInitiateSession())
->setMerchantInfo(
(new MerchantInfo())
->setCallbackPrefix($callback_prefix)
->setReturnUrl($return_url)
->setCallbackAuthorizationToken($callback_auth_token)
)
->setPaymentTransaction(
(new PaymentTransaction())
->setAmount($amount)
)
->setLogistics(new Logistics())
->setPrefillCustomer(new PrefillCustomer())
->setContactFields($contact_fields)
->setAddressFields($address_fields);

// Set other options.
foreach ($options as $option => $value) {
switch ($option) {
case 'termsAndConditionsUrl':
$request->getMerchantInfo()->setTermsAndConditionsUrl($value);
break;
// Payment transaction options.
case 'reference':
$request->getPaymentTransaction()->setReference($value);
break;
case 'paymentDescription':
$request->getPaymentTransaction()->setDescription($value);
break;
// Logistics options.
case 'dynamicOptionsCallback':
$request->getLogistics()->setDynamicOptionsCallback($value);
break;
case 'fixedOptions':
$request->getLogistics()->setFixedOptions($value);
break;
// Prefill customer options.
case 'firstName':
$request->getPrefillCustomer()->setFirstName($value);
break;
case 'lastName':
$request->getPrefillCustomer()->setLastName($value);
break;
case 'email':
$request->getPrefillCustomer()->setEmail($value);
break;
case 'phoneNumber':
$request->getPrefillCustomer()->setPhoneNumber($value);
break;
case 'streetAddress':
$request->getPrefillCustomer()->setStreetAddress($value);
break;
case 'city':
$request->getPrefillCustomer()->setCity($value);
break;
case 'postalCode':
$request->getPrefillCustomer()->setPostalCode($value);
break;
case 'country':
$request->getPrefillCustomer()->setCountry($value);
break;
// Customer interaction.
case 'customerInteraction':
$request->setCustomerInteraction($value);
break;
// User flow.
case 'userFlow':
$request->setUserFlow($value);
break;
}
}

$resource = new InitiateSession(
$this->app,
$this->getSubscriptionKey(),
$client_secret,
$request
);
$resource->setPath($resource->getPath());
return $resource->call();
}


/**
* {@inheritdoc}
*/
public function getSessionDetails(string $client_secret, string $session_id): ResponseGetSessionDetails
{
$resource = new GetSessionDetails(
$this->app,
$this->getSubscriptionKey(),
$client_secret,
$session_id
);
$resource->setPath($resource->getPath());
return $resource->call();
}

/**
* {@inheritdoc}
*/
public function cancelSession(string $client_secret, string $session_id): ResponseCancelSession {
$request = (new RequestCancelSession())->setSessionId($session_id);
$resource = new CancelSession(
$this->app,
$this->getSubscriptionKey(),
$client_secret,
$request
);
$resource->setPath($resource->getPath());
return $resource->call();
}
}
74 changes: 74 additions & 0 deletions src/Api/CheckoutInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace zaporylie\Vipps\Api;

use zaporylie\Vipps\Model\Checkout\RequestAmount;
use zaporylie\Vipps\Model\Checkout\ResponseCancelSession;
use zaporylie\Vipps\Model\Checkout\ResponseGetSessionDetails;
use zaporylie\Vipps\Model\Checkout\ResponseInitiateSession;

/**
* Interface CheckoutInterface.
*
* @package Vipps\Api
*/
interface CheckoutInterface
{

/**
* @param string $client_secret
* Client secret.
* @param string $callback_prefix
* Callback prefix.
* @param string $return_url
* Return URL.
* @param string $callback_auth_token
* Callback authorization token.
* @param \zaporylie\Vipps\Model\Checkout\RequestAmount $amount
* Request amount.
* @param $options array
* Options.
* @param bool $contact_fields
* Contact fields.
* @param bool $address_fields
* Address fields.
*
* @return \zaporylie\Vipps\Model\Checkout\ResponseInitiateSession
*
* @see https://vippsas.github.io/vipps-checkout-api/#/Session/post_v2_session
*/
public function initiateSession(
string $client_secret,
string $callback_prefix,
string $return_url,
string $callback_auth_token,
RequestAmount $amount,
array $options = [],
bool $contact_fields = TRUE,
bool $address_fields = TRUE
): ResponseInitiateSession;

/**
* @param string $client_secret
* Client secret.
* @param string $session_id
* Session id.
*
* @return \zaporylie\Vipps\Model\Checkout\ResponseGetSessionDetails
*
* @see https://vippsas.github.io/vipps-checkout-api/#/Session/get_v2_session__sessionId_
*/
public function getSessionDetails(string $client_secret, string $session_id): ResponseGetSessionDetails;

/**
* @param string $client_secret
* Client secret.
* @param string $session_id
* Session id.
*
* @return \zaporylie\Vipps\Model\Checkout\ResponseCancelSession
*
* @see https://vippsas.github.io/vipps-checkout-api/#/Session/post_v2_session_cancel
*/
public function cancelSession(string $client_secret, string $session_id): ResponseCancelSession;
}
46 changes: 46 additions & 0 deletions src/Model/Checkout/AmountBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace zaporylie\Vipps\Model\Checkout;

use JMS\Serializer\Annotation as Serializer;

/**
* Class AmountBase.
*
* @package Vipps\Model\Checkout
*/
abstract class AmountBase
{

/**
* @var string
* @Serializer\Type("string")
*/
protected $currency;

/**
* @var float
* @Serializer\Type("double")
*/
protected $value;

/**
* Gets amount currency.
*
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}

/**
* Gets amount value.
*
* @return float
*/
public function getValue(): float
{
return $this->value;
}
}
30 changes: 30 additions & 0 deletions src/Model/Checkout/BillingDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace zaporylie\Vipps\Model\Checkout;

use JMS\Serializer\Annotation as Serializer;

/**
* Class BillingDetails.
*
* @package Vipps\Model\Checkout
*/
class BillingDetails extends CustomerDetailsBase
{

/**
* @var string
* @Serializer\Type("string")
*/
protected $region;

/**
* Gets region.
*
* @return string
*/
public function getRegion(): string
{
return $this->region;
}
}
Loading