Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
34 changes: 21 additions & 13 deletions src/Message/AcceptNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use Omnipay\Common\Message\NotificationInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

class AcceptNotification extends PurchaseRequest implements NotificationInterface {
class AcceptNotification extends PurchaseRequest implements NotificationInterface
{
protected $data;

protected $transaction;

public function getData() {
public function getData()
{
return $this->data;
}

public function sendData($data) {
public function sendData($data)
{
$sessionId = $this->httpRequest->query->get('sessionId') ?? $this->httpRequest->request->get('sessionId') ?? '';

if (empty($sessionId)) {
Expand All @@ -32,8 +35,7 @@ public function sendData($data) {

try {
$httpResponse = $this->httpClient->request('GET', $this->getEndpoint('sessions/' . $sessionId), $headers);
}
catch (\Exception $exception) {
} catch (\Exception $exception) {
throw new InvalidRequestException($exception->getMessage());
}

Expand All @@ -45,31 +47,37 @@ public function sendData($data) {
return $this;
}

public function getTransaction() {
public function getTransaction()
{
return $this->transaction;
}

public function getTransactionReference() {
public function getTransactionReference()
{
return $this->getTransaction()['id'] ?? '';
}

public function getTransactionStatus() {
if ($this->getTransaction() && $this->getAuthorised() && $this->getResponseText() === 'APPROVED') {
public function getTransactionStatus()
{
if ($this->getTransaction() && $this->getAuthorised() && $this->getResponseText() === 'APPROVED') {
return static::STATUS_COMPLETED;
}

return static::STATUS_FAILED;
}

public function getAuthorised() {
public function getAuthorised()
{
return $this->getTransaction()['authorised'] ?? false;
}

public function getResponseText() {
public function getResponseText()
{
return strtoupper($this->getTransaction()['responseText']) ?? '';
}

public function getMessage() {
public function getMessage()
{
return $this->getResponseText() ?? '';
}

Expand Down Expand Up @@ -107,4 +115,4 @@ public function getTransactionResult()
{
return $this->getTransaction() ?? [];
}
}
}
11 changes: 6 additions & 5 deletions src/Message/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public function getLanguage()

/**
* @param $list
* Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac', 'unionpay', 'oxipay', 'visacheckout', 'wechat']
* Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac',
* 'unionpay', 'oxipay', 'visacheckout', 'wechat']
*
* @return PurchaseRequest
*/
Expand All @@ -91,8 +92,8 @@ public function setPaymentMethods($list)
'oxipay', 'visacheckout', 'wechat'
];

foreach ( $list as $method ) {
if ( !in_array($method, $options) ) {
foreach ($list as $method) {
if (!in_array($method, $options)) {
throw new InvalidRequestException("Unknown payment method: {$method}");
}
}
Expand Down Expand Up @@ -160,7 +161,7 @@ public function setStoredCardIndicator($value)
'resubmission', 'reauthorisation', 'delayedcharges', 'noshow'
];

if ( ! in_array($value, $options) ) {
if (! in_array($value, $options)) {
throw new InvalidRequestException("Invalid option '{$value}' set for StoredCardIndicator.");
}

Expand Down Expand Up @@ -192,7 +193,7 @@ public function setRecurringFrequency($value)
'twomonthly', 'threemonthly', 'fourmonthly', 'sixmonthly', 'annually'
];

if ( ! in_array($value, $options) ) {
if (! in_array($value, $options)) {
throw new InvalidRequestException("Invalid option '{$value}' set for RecurringFrequency.");
}

Expand Down
12 changes: 9 additions & 3 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ class CompletePurchaseRequest extends PurchaseRequest
public function getData()
{
return [
'sessionId' => $this->getParameter('sessionId') ?? $this->httpRequest->query->get('sessionId') ?? $this->httpRequest->request->get('sessionId') ?? '',
'username' => $this->getParameter('username') ?? $this->httpRequest->query->get('username') ?? $this->httpRequest->request->get('username') ?? '',
'sessionId' => $this->getParameter('sessionId')
?? $this->httpRequest->query->get('sessionId')
?? $this->httpRequest->request->get('sessionId')
?? '',
'username' => $this->getParameter('username')
?? $this->httpRequest->query->get('username')
?? $this->httpRequest->request->get('username')
?? '',
];
}
public function sendData($data)
{
if ( !$data['sessionId'] ) {
if (!$data['sessionId']) {
throw new InvalidRequestException('Session id is required');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getCardType()

public function getCardReference()
{
if ( !in_array($this->getTransactionMethod(), ['card', 'visacheckout']) ) {
if (!in_array($this->getTransactionMethod(), ['card', 'visacheckout'])) {
return null;
}

Expand Down
132 changes: 105 additions & 27 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,149 @@
*/
class PurchaseRequest extends BaseRequest
{
public function initialize(array $parameters = [])
{
return parent::initialize($parameters);
}

public function getData()
{
$this->validate('apiUsername', 'apiKey', 'amount', 'currency');

$data = [];

$data['type'] = $this->getType();
$data['amount'] = $this->getAmount();
$data['currency'] = $this->getCurrency();
$data['callbackUrls'] = [];
$this->validate('apiUsername', 'apiKey', 'amount', 'currency', 'card');
$card = $this->getCard();

$data = [
'type' => $this->getType(),
'amount' => $this->getAmount(),
'currency' => $this->getCurrency(),
'callbackUrls' => [],
'customer' => [
'email' => null,
'shipping' => [],
'billing' => []
],
];

if ( (bool) $this->getCreateToken() ) {
if ((bool) $this->getCreateToken()) {
$data['storeCard'] = true;
}

if ( $this->getStoredCardIndicator() ) {
if ($this->getStoredCardIndicator()) {
$data['storedCardIndicator'] = $this->getStoredCardIndicator();
}

if ( $this->getRecurringExpiry() ) {
if ($this->getRecurringExpiry()) {
$data['recurringExpiry'] = $this->getRecurringExpiry();
}

if ( $this->getRecurringFrequency() ) {
if ($this->getRecurringFrequency()) {
$data['recurringFrequency'] = $this->getRecurringFrequency();
}

if ( $this->getToken() || $this->getCardReference() ) {
if ($this->getToken() || $this->getCardReference()) {
$data['cardId'] = $this->getToken() ?? $this->getCardReference();
}

if ( is_array($this->getPaymentMethods()) ) {
if (is_array($this->getPaymentMethods())) {
$data['methods'] = $this->getPaymentMethods();
}

if ( is_array($this->getCardTypes()) ) {
if (is_array($this->getCardTypes())) {
$data['cardTypes'] = $this->getCardTypes();
}

if ( is_array($this->getMetadata()) ) {
if (is_array($this->getMetadata())) {
$data['metaData'] = $this->getMetadata();
}

$merchantReference = $this->getMerchantReference() ?? $this->getDescription();

if ( $merchantReference ) {
if ($merchantReference) {
$data['merchantReference'] = $merchantReference;
}

if ( $this->getReturnUrl() ) {
if ($this->getReturnUrl()) {
$data['callbackUrls']['approved'] = $this->getReturnUrl();
}

if ( $this->getDeclineUrl() ) {
if ($this->getDeclineUrl()) {
$data['callbackUrls']['declined'] = $this->getDeclineUrl();
}

if ( $this->getCancelUrl() ) {
if ($this->getCancelUrl()) {
$data['callbackUrls']['cancelled'] = $this->getCancelUrl();
}

if ( $this->getNotifyUrl() ) {
if ($this->getNotifyUrl()) {
$data['notificationUrl'] = $this->getNotifyUrl();
}

if ($card->getEmail()) {
$data['customer']['email'] = $card->getEmail();
}

if ($card->getBillingName()) {
$data['customer']['billing']['name'] = $card->getBillingName();
}

if ($card->getBillingAddress1()) {
$data['customer']['billing']['address1'] = $card->getBillingAddress1();
}

if ($card->getBillingAddress2()) {
$data['customer']['billing']['address2'] = $card->getBillingAddress2();
}

if ($card->getBillingCity()) {
$data['customer']['billing']['city'] = $card->getBillingCity();
}

if ($card->getBillingCountry()) {
$data['customer']['billing']['countryCode'] = $card->getBillingCountry();
}

if ($card->getBillingPostcode()) {
$data['customer']['billing']['postalCode'] = $card->getBillingPostcode();
}

if ($card->getBillingPhone()) {
$data['customer']['billing']['phoneNumber'] = $card->getBillingPhone();
}

if ($card->getBillingState()) {
$data['customer']['billing']['state'] = $card->getBillingState();
}

if ($card->getShippingName()) {
$data['customer']['shipping']['name'] = $card->getShippingName();
}

if ($card->getShippingAddress1()) {
$data['customer']['shipping']['address1'] = $card->getShippingAddress1();
}

if ($card->getShippingAddress2()) {
$data['customer']['shipping']['address2'] = $card->getShippingAddress2();
}

if ($card->getShippingCity()) {
$data['customer']['shipping']['city'] = $card->getShippingCity();
}

if ($card->getShippingCountry()) {
$data['customer']['shipping']['countryCode'] = $card->getShippingCountry();
}

if ($card->getShippingPostcode()) {
$data['customer']['shipping']['postalCode'] = $card->getShippingPostcode();
}

if ($card->getShippingPhone()) {
$data['customer']['shipping']['phoneNumber'] = $card->getShippingPhone();
}

if ($card->getShippingState()) {
$data['customer']['shipping']['state'] = $card->getShippingState();
}

if (empty($data['customer']['shipping'])) {
unset($data['customer']['shipping']);
}

return $data;
}

Expand All @@ -90,7 +163,12 @@ public function sendData($data)
'Authorization' => 'Basic ' . $this->getAuthorization(),
];

$httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
$httpResponse = $this->httpClient->request(
'POST',
$this->getEndpoint('sessions'),
$headers,
json_encode($data)
);

try {
$responseData = json_decode($httpResponse->getBody()->getContents());
Expand Down
19 changes: 12 additions & 7 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
/**
* Windcave HPP Redirect Response
*/
class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface {
class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface
{

public function isSuccessful() {
public function isSuccessful()
{
return false;
}

public function isRedirect() {
public function isRedirect()
{
return true;
}

public function getRedirectUrl() {
foreach ( $this->data->links ?? [] as $link ) {
public function getRedirectUrl()
{
foreach ($this->data->links ?? [] as $link) {
if ($link->rel === 'hpp') {
return $link->href;
}
Expand All @@ -29,7 +33,8 @@ public function getRedirectUrl() {
throw new InvalidResponseException('Invalid response from windcave server');
}

public function getRedirectData() {
public function getRedirectData()
{
return [];
}
}
}
Loading