Skip to content

Commit db3dad0

Browse files
authored
Initial release (#1)
* extend abstract gateway, add username & secret as params * change namespace to Omnipay\EveryPay * add accountId to Parameters * add purchase parameter setters / getters * add hmac signing, card request message * implement purchaes redirect flow * override client ip getter and setter * add completePurchase method and validate everypay response * add support for card tokens and transaction reference * fix class name * support passing in card tokens * check payment state before amount * ability to handle backend api charge * fix token security param * pass emails to checkout * pass ip to purchase * fix typo in email
1 parent 3ae29ac commit db3dad0

22 files changed

+717
-19
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
EVERY_PAY_API_USERNAME=
2+
EVERY_PAY_API_SECRET=
3+
EVERY_PAY_ACCOUNT_ID=

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/vendor
2+
3+
.env

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
},
2121
"autoload": {
2222
"psr-4": {
23-
"Swiftmade\\EveryPay\\": "src/"
23+
"Omnipay\\EveryPay\\": "src/"
2424
}
2525
},
2626
"autoload-dev": {
2727
"classmap": [
28-
"tests/TestCase.php"
28+
"tests/Environment.php"
2929
]
3030
}
3131
}

src/Common/CardToken.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Common;
3+
4+
use Omnipay\Common\CreditCard;
5+
6+
class CardToken extends CreditCard
7+
{
8+
protected $token;
9+
10+
public static function make($payload)
11+
{
12+
$card = new CardToken([
13+
'brand' => $payload['cc_type'],
14+
'name' => $payload['cc_holder_name'],
15+
'number' => $payload['cc_last_four_digits'],
16+
'expiryYear' => $payload['cc_year'],
17+
'expiryMonth' => $payload['cc_month'],
18+
]);
19+
20+
$card->setToken($payload['cc_token']);
21+
return $card;
22+
}
23+
24+
public function setBrand($brand)
25+
{
26+
return $this->setParameter('brand', $brand);
27+
}
28+
29+
public function getBrand()
30+
{
31+
return $this->getParameter('brand');
32+
}
33+
34+
public function setToken($token)
35+
{
36+
$this->token = $token;
37+
}
38+
39+
public function getToken()
40+
{
41+
return $this->token;
42+
}
43+
}

src/Concerns/CustomRedirectHtml.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Concerns;
3+
4+
use Symfony\Component\HttpFoundation\Response as HttpResponse;
5+
use Symfony\Component\HttpFoundation\RedirectResponse as HttpRedirectResponse;
6+
7+
trait CustomRedirectHtml
8+
{
9+
public function getRedirectResponse()
10+
{
11+
$this->validateRedirect();
12+
13+
if ('GET' === $this->getRedirectMethod()) {
14+
return HttpRedirectResponse::create($this->getRedirectUrl());
15+
}
16+
17+
$hiddenFields = '';
18+
foreach ($this->getRedirectData() as $key => $value) {
19+
$hiddenFields .= sprintf(
20+
'<input type="hidden" name="%1$s" value="%2$s" />',
21+
htmlentities($key, ENT_QUOTES, 'UTF-8', false),
22+
htmlentities($value, ENT_QUOTES, 'UTF-8', false)
23+
) . "\n";
24+
}
25+
26+
$output = '<!DOCTYPE html>
27+
<html>
28+
<head>
29+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
30+
<title>Redirecting...</title>
31+
</head>
32+
<body onload="document.forms[0].submit();">
33+
<form action="%1$s" method="post">
34+
%2$s
35+
</form>
36+
</body>
37+
</html>';
38+
$output = sprintf(
39+
$output,
40+
htmlentities($this->getRedirectUrl(), ENT_QUOTES, 'UTF-8', false),
41+
$hiddenFields
42+
);
43+
44+
return HttpResponse::create($output);
45+
}
46+
}

src/Concerns/Parameters.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Concerns;
3+
4+
trait Parameters
5+
{
6+
public function getDefaultParameters()
7+
{
8+
return [
9+
'username' => getenv('EVERY_PAY_API_USERNAME'), // api_username
10+
'secret' => getenv('EVERY_PAY_API_SECRET'), // api_secret
11+
'accountId' => getenv('EVERY_PAY_ACCOUNT_ID'), // processing account
12+
'testMode' => true,
13+
'locale' => 'et',
14+
];
15+
}
16+
17+
public function getUsername()
18+
{
19+
return $this->getParameter('username');
20+
}
21+
22+
public function setUsername($username)
23+
{
24+
return $this->setParameter('username', $username);
25+
}
26+
27+
public function getSecret()
28+
{
29+
return $this->getParameter('secret');
30+
}
31+
32+
public function setSecret($secret)
33+
{
34+
return $this->setParameter('secret', $secret);
35+
}
36+
37+
public function getAccountId()
38+
{
39+
return $this->getParameter('accountId');
40+
}
41+
42+
public function setAccountId($accountId)
43+
{
44+
return $this->setParameter('accountId', $accountId);
45+
}
46+
47+
public function getLocale()
48+
{
49+
return $this->getParameter('locale');
50+
}
51+
52+
public function setLocale($locale)
53+
{
54+
return $this->setParameter('locale', $locale);
55+
}
56+
57+
public function getClientIp()
58+
{
59+
return $this->getParameter('user_ip');
60+
}
61+
62+
public function setClientIp($value)
63+
{
64+
return $this->setParameter('user_ip', $value);
65+
}
66+
}

src/EveryPayGateway.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Exceptions/MismatchException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Exceptions;
3+
4+
use Exception;
5+
6+
class MismatchException extends Exception implements PaymentException
7+
{
8+
public function getStatus()
9+
{
10+
return 'invalid';
11+
}
12+
}

src/Exceptions/PaymentException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Exceptions;
3+
4+
interface PaymentException
5+
{
6+
public function getStatus();
7+
8+
public function getMessage();
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Exceptions;
3+
4+
use Exception;
5+
6+
class PaymentFailedException extends Exception implements PaymentException
7+
{
8+
public function getStatus()
9+
{
10+
return 'failed';
11+
}
12+
}

src/Gateway.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Omnipay\EveryPay;
3+
4+
use Omnipay\Common\AbstractGateway;
5+
use Omnipay\EveryPay\Support\SignedData;
6+
use Omnipay\EveryPay\Messages\PurchaseRequest;
7+
use Omnipay\EveryPay\Messages\CompletePurchaseRequest;
8+
9+
class Gateway extends AbstractGateway
10+
{
11+
use Concerns\Parameters;
12+
13+
public function getName()
14+
{
15+
return 'Every Pay';
16+
}
17+
18+
public function signData(array $data)
19+
{
20+
return SignedData::make($data, $this->getSecret());
21+
}
22+
23+
public function purchase(array $parameters = [])
24+
{
25+
return $this->createRequest(PurchaseRequest::class, $parameters);
26+
}
27+
28+
public function completePurchase(array $parameters = [])
29+
{
30+
return $this->createRequest(CompletePurchaseRequest::class, $parameters);
31+
}
32+
}

src/Messages/AbstractRequest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Messages;
3+
4+
use Omnipay\EveryPay\Concerns\Parameters;
5+
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
6+
7+
/**
8+
* Abstract Request
9+
*
10+
*/
11+
abstract class AbstractRequest extends BaseAbstractRequest
12+
{
13+
use Parameters;
14+
15+
protected $liveEndpoint = 'https://pay.every-pay.eu/transactions/';
16+
protected $testEndpoint = 'https://igw-demo.every-pay.com/transactions/';
17+
18+
public function getEndpoint()
19+
{
20+
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
21+
}
22+
23+
protected function getBaseData()
24+
{
25+
return [
26+
'api_username' => $this->getUsername(),
27+
'account_id' => $this->getAccountId(),
28+
'nonce' => uniqid(true),
29+
'timestamp' => time(),
30+
'customer_url' => $this->getCustomerUrl(),
31+
'callback_url' => $this->getCallbackUrl(),
32+
];
33+
}
34+
35+
protected function createResponse($data)
36+
{
37+
return $this->response = new Response($this, $data);
38+
}
39+
40+
public function setCustomerUrl($url)
41+
{
42+
return $this->setParameter('customerUrl', $url);
43+
}
44+
45+
public function getCustomerUrl()
46+
{
47+
return $this->getParameter('customerUrl');
48+
}
49+
50+
public function setCallbackUrl($url)
51+
{
52+
return $this->setParameter('callbackUrl', $url);
53+
}
54+
55+
public function getCallbackUrl()
56+
{
57+
return $this->getParameter('callbackUrl');
58+
}
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Omnipay\EveryPay\Messages;
3+
4+
use Omnipay\Common\Message\AbstractResponse;
5+
use Omnipay\Common\Message\RequestInterface;
6+
use Omnipay\Common\Message\RedirectResponseInterface;
7+
use Omnipay\EveryPay\Concerns\CustomRedirectHtml;
8+
9+
/**
10+
* Response
11+
*/
12+
class BackendPurchaseResponse extends AbstractResponse implements RedirectResponseInterface
13+
{
14+
use CustomRedirectHtml;
15+
16+
protected $message;
17+
18+
public function __construct(RequestInterface $request, $data)
19+
{
20+
$this->request = $request;
21+
$this->data = $data;
22+
}
23+
24+
public function isSuccessful()
25+
{
26+
$response = @json_decode($this->data, true);
27+
28+
if (!is_array($response) || !isset($response['charge'])) {
29+
$this->message = 'Error';
30+
return false;
31+
}
32+
33+
$charge = $response['charge'];
34+
// TODO: implement
35+
36+
return false;
37+
}
38+
39+
public function isRedirect()
40+
{
41+
return false;
42+
}
43+
44+
public function getMessage()
45+
{
46+
return $this->message;
47+
}
48+
}

0 commit comments

Comments
 (0)