Skip to content

Commit

Permalink
Merge pull request #1 from arbory/direct-payment-method-redirect
Browse files Browse the repository at this point in the history
Authorize response update to can redirect directly to payment method
  • Loading branch information
sabineabele authored Oct 4, 2024
2 parents e22a9d9 + 12761c6 commit 5e07465
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 1 deletion.
150 changes: 150 additions & 0 deletions src/Common/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace Omnipay\EveryPay\Common;

use Omnipay\Common\ParametersTrait;

class PaymentMethod
{
use ParametersTrait;

public const SOURCE_APPLE_PAY = 'apple_pay';
public const SOURCE_GOOGLE_PAY = 'google_pay';
private const SOURCE_CARD = 'card';

/**
* PaymentMethod constructor.
* @param object $parameters
*/
public function __construct(object $parameters)
{
$this->initialize((array) $parameters);
}

/**
* @return string
*/
public function getSource(): string
{
return $this->getParameter('source');
}

/**
* @return string|null
*/
public function getCountryCode(): ?string
{
return $this->getParameter('country_code');
}

/**
* @return string
*/
public function getName(): string
{
return $this->getParameter('display_name');
}

/**
* @return string
*/
public function getLogoUrl(): string
{
return $this->getParameter('logo_url');
}

/**
* @return string|null
*/
public function getPaymentLink(): ?string
{
return $this->getParameter('payment_link');
}

/**
* @return bool
*/
public function isCard(): bool
{
return $this->getSource() === self::SOURCE_CARD;
}

/**
* @return bool
*/
public function isApplePayAvailable(): bool
{
return $this->getParameter('applepay_available') === true;
}

/**
* @return bool
*/
public function isGooglePayAvailable(): bool
{
return $this->getParameter('googlepay_available') === true;
}

/**
* @param string $value
* @return $this
*/
public function setSource($value): self
{
return $this->setParameter('source', $value);
}

/**
* @param string $value
* @return $this
*/
public function setCountryCode($value): self
{
return $this->setParameter('country_code', $value);
}

/**
* @param string $value
* @return $this
*/
public function setDisplayName($value): self
{
return $this->setParameter('display_name', $value);
}

/**
* @param string $value
* @return $this
*/
public function setLogoUrl($value): self
{
return $this->setParameter('logo_url', $value);
}

/**
* @param string $value
* @return $this
*/
public function setPaymentLink($value): self
{
return $this->setParameter('payment_link', $value);
}

/**
* @param string $value
* @return $this
*/
public function setApplepayAvailable($value): self
{
return $this->setParameter('applepay_available', $value);
}

/**
* @param string $value
* @return $this
*/
public function setGooglepayAvailable($value): self
{
return $this->setParameter('googlepay_available', $value);
}
}
26 changes: 26 additions & 0 deletions src/Interfaces/MobilePaymentResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Omnipay\EveryPay\Interfaces;

interface MobilePaymentResponse
{
/**
* @return bool
*/
public function isApplePay(): bool;

/**
* @return bool
*/
public function isGooglePay(): bool;

/**
* @return array
*/
public function getApplePayData(): array;

/**
* @return array
*/
public function getGooglePayData(): array;
}
97 changes: 96 additions & 1 deletion src/Messages/AuthorizeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace Omnipay\EveryPay\Messages;

use Omnipay\Common\Message\RedirectResponseInterface;
use Omnipay\EveryPay\Common\PaymentMethod;
use Omnipay\EveryPay\Interfaces\MobilePaymentResponse;

/**
* Response
*/
class AuthorizeResponse extends AbstractResponse implements RedirectResponseInterface
class AuthorizeResponse extends AbstractResponse implements RedirectResponseInterface, MobilePaymentResponse
{
public function isRedirect()
{
Expand All @@ -17,4 +19,97 @@ public function getRedirectMethod()
{
return 'GET';
}

public function getRedirectUrl()
{
$paymentMethod = $this->request->getPaymentMethod();
$paymentMethodUrl = $paymentMethod ? $this->getPaymentMethodPaymentUrl($paymentMethod) : null;

return $paymentMethodUrl ?? parent::getRedirectUrl();
}

/**
* @param string $source
* @return string|null
*/
public function getPaymentMethodPaymentUrl(string $source): ?string
{
foreach ($this->getPaymentMethods() as $paymentMethod) {
if ($paymentMethod->getSource() === $source) {
return $paymentMethod->getPaymentLink();
}
}

return null;
}

/**
* @return array|PaymentMethod[]
*/
public function getPaymentMethods(): array
{
return array_map(function (object $paymentMethod) {
return new PaymentMethod($paymentMethod);
}, $this->data->payment_methods);
}

/**
* @return array
*/
public function getApplePayData(): array
{
return [
'mobile_access_token' => $this->getMobileAccessToken(),
'merchant_id' => $this->data->applepay_merchant_identifier,
'api_username' => $this->data->api_username,
'account_name' => $this->data->account_name,
'payment_reference' => $this->data->payment_reference,
'country_code' => $this->data->descriptor_country,
'currency_code' => $this->data->currency,
'payment_link' => $this->data->payment_link,
'total_amount' => $this->data->initial_amount,
'total_label' => (string)$this->data->initial_amount,
];
}

/**
* @return array
*/
public function getGooglePayData(): array
{
return [
'mobile_access_token' => $this->getMobileAccessToken(),
'api_username' => $this->data->api_username,
'account_name' => $this->data->account_name,
'payment_reference' => $this->data->payment_reference,
'country_code' => $this->data->descriptor_country,
'currency_code' => $this->data->currency,
'payment_link' => $this->data->payment_link,
'total_amount' => (string)$this->data->initial_amount,
];
}

/**
* @return bool
*/
public function isApplePay(): bool
{
return $this->getMobileAccessToken() && $this->request->getPaymentMethod() === PaymentMethod::SOURCE_APPLE_PAY;
}

/**
* @return bool
*/
public function isGooglePay(): bool
{
return $this->getMobileAccessToken() && $this->request->getPaymentMethod() === PaymentMethod::SOURCE_GOOGLE_PAY;
}

/**
* @return string|null
*/
public function getMobileAccessToken(): ?string
{
return $this->data->mobile_access_token ?? null;
}
}

0 comments on commit 5e07465

Please sign in to comment.