Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
composer.json fix

readme

composer.json

be crazy, skip the min stability

be crazy, skip the min stability

be crazy, skip the min stability

signature fix

concat string

missing exception class
  • Loading branch information
cave committed Sep 27, 2021
0 parents commit 24428a5
Show file tree
Hide file tree
Showing 23 changed files with 1,126 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/vendor
composer.lock
composer.phar
phpunit.xml
.directory
/dirlist.*
/documents/
/.idea/
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Contributing Guidelines

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit just the modifications, do not mess with the composer.json or CHANGELOG.md files.
* Ensure your code is nicely formatted in the [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
style and that all tests pass.
* Send the pull request.
* Check that the Travis CI build passed. If not, rinse and repeat.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Marco Beinbrech

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Omnipay: APS (Amazon Payment Service)

**APS driver for the Omnipay PHP payment processing library**

[Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment
processing library for PHP. This package implements Amazon Payment Services (APS) support for Omnipay.

## Installation

Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply require
`league/omnipay` and `cave/omnipay-aps` with Composer:

```
composer require league/omnipay cave/omnipay-aps
```

## Basic Usage

The following gateways are provided by this package:

* APS

For general usage instructions, please see the main [Omnipay](https://github.com/omnipay/omnipay)
repository.

## Example

### Purchase

The result will be a redirect to the gateway or bank.

```php
use Omnipay\Omnipay;

$gateway = Omnipay::create('Amazon Payment Service');

// Send purchase request (don't get so excited... params below are just fake :))
$response = $gateway->purchase([
'access_code' => 'zx0IPmPy5jp1vAz8Kpg7',
'merchant_identifier' => 'CycHZxVj',
'merchant_reference' => 'XYZ9239-yu898',
'amount' => '10000',
'currency' => 'AED',
'language' => 'en',
'customer_email' => '[email protected]',
'order_description' => 'iPhone 6-S',
])->send();

// Process response
if ($response->isSuccessful()) {
// Let's party!!!
} else {
// Payment failed: display message to customer
echo $response->getMessage();
}
```

The Purchase request combines Authorization and Capture in the same request
(per APS' documentation). If you need to first authorize the amount you can call the Authorization
request and then the Capture request separately.

### Testing

```sh
composer test
```

## Support

If you are having general issues with Omnipay, we suggest posting on
[Stack Overflow](http://stackoverflow.com/). Be sure to add the
[omnipay tag](http://stackoverflow.com/questions/tagged/omnipay) so it can be easily found.

If you want to keep up to date with release anouncements, discuss ideas for the project,
or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which
you can subscribe to.

If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/cave/omnipay-aps/issues),
or better yet, fork the library and submit a pull request.
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "cave/omnipay-aps",
"type": "library",
"description": "Omnipay driver for Amazon Payment Services",
"require": {
"php": "^7.3 || ^8.0",
"ext-json": "*",
"omnipay/common": "^3.0"
},
"require-dev": {
"omnipay/tests": "^3",
"squizlabs/php_codesniffer": "^3.4",
"phpunit/phpunit": "^6.0"
},
"keywords": [
"omnipay",
"amazon",
"payment",
"service",
"payment",
"gateway",
"laravel",
"purchase"
],
"homepage": "https://github.com/cave/omnipay-aps",
"license": "MIT",
"autoload": {
"psr-4": {
"Omnipay\\APS\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Omnipay\\APS\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "cave",
"email": "[email protected]"
}
],
"scripts": {
"test": "phpunit",
"check-style": "phpcs -p --standard=PSR2 src/",
"fix-style": "phpcbf -p --standard=PSR2 src/"
},
"minimum-stability": "dev",
"prefer-stable": true
}
112 changes: 112 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php namespace Omnipay\APS;

/**
* Amazon Payment Service
*/
use Omnipay\APS\Message\Request\AuthorizationRequest;
use Omnipay\APS\Message\Request\CaptureRequest;
use Omnipay\APS\Message\Request\PurchaseRequest;
use Omnipay\APS\Message\Request\RefundRequest;
use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\RequestInterface;

/**
* @method \Omnipay\Common\Message\NotificationInterface acceptNotification(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface completeAuthorize(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface completePurchase(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface fetchTransaction(array $options = [])
* @method \Omnipay\Common\Message\RequestInterface void(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface createCard(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface updateCard(array $options = array())
* @method \Omnipay\Common\Message\RequestInterface deleteCard(array $options = array())
*/
class Gateway extends AbstractGateway
{
/**
* @return string
*/
public function getName(): string
{
return 'Amazon Payment Service';
}

/**
* @return string
*/
public function getShortName(): string
{
return 'APS';
}

/**
* @return array
*/
public function getDefaultParameters(): array
{
return [
'testMode' => FALSE,
];
}

/**
* @param array $options
*
* @return AbstractRequest|RequestInterface
*/
public function authorize(array $options = [])
{
return $this->createRequest(AuthorizationRequest::class, $options);
}

/**
* @param array $options
*
* @return AbstractRequest|RequestInterface
*/
public function purchase(array $options = [])
{
return $this->createRequest(PurchaseRequest::class, $options);
}

/**
* @param array $options
*
* @return AbstractRequest|RequestInterface
*/
public function capture(array $options = [])
{
return $this->createRequest(CaptureRequest::class, $options);
}

/**
* @param array $options
*
* @return AbstractRequest|RequestInterface
*/
public function refund(array $options = [])
{
return $this->createRequest(RefundRequest::class, $options);
}

/**
* @param string $requestPhrase
*
* @return mixed
*/
public function setRequestPhrase(string $requestPhrase)
{
return $this->setParameter('request_phrase', $requestPhrase);
}

/**
* @param string $shaType
*
* @return mixed
*/
public function setShaType(string $shaType)
{
return $this->setParameter('sha_type', $shaType);
}
}

84 changes: 84 additions & 0 deletions src/Message/Request/APSAbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php namespace Omnipay\APS\Message\Request;

use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\ResponseInterface;

abstract class APSAbstractRequest extends AbstractRequest
{
const DEFAULT_SHA_TYPE = 'sha256';

/**
* @param mixed $data
*
* @return ResponseInterface
* @throws InvalidResponseException
*/
public function sendData($data): ResponseInterface
{
try
{
// Sort array by key ascending
ksort($data);

$data['signature'] = $this->_createSignature($data);

$httpResponse = $this->httpClient->request(
'POST',
$this->getEndpoint(),
[
'Accept' => 'application/json',
'Content-type' => 'application/json',
],
json_encode($data)
);

$json = $httpResponse->getBody()->getContents();

$data = !empty($json) ? json_decode($json, true) : [];

return $this->response = $this->createResponse($data);

}
catch (\Exception $e)
{
throw new InvalidResponseException(
"Communication failed with message: " . $e->getMessage(),
$e->getCode()
);
}
}

/**
* @return string
*/
protected function getEndpoint(): string
{
return $this->getTestMode() ? $this->test_endpoint : $this->live_endpoint;
}

/**
* Creates signature used by Amazon for authorisation
* @param array $data
*
* @return string
* @throws RequestPhraseException
*/
private function _createSignature(array $data): string
{
$shaType = $this->hasParameter('sha_type') ? $this->hasParameter('sha_type') : self::DEFAULT_SHA_TYPE;

if ( ! $this->hasParameter('request_phrase'))
throw new RequestPhraseException('Request phrase is missing.');

foreach ($data as $key => $value)
{
$shaString .= "$key=$value";
}

// "Glue" phrase to the both sides of the payload
$shaString = $this->getParameter('request_phrase') . $shaString . $this->getParameter('request_phrase');

return hash($shaType, $shaString);
}
}
13 changes: 13 additions & 0 deletions src/Message/Request/AbstractCheckoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Omnipay\APS\Message\Request;

abstract class AbstractCheckoutRequest extends APSAbstractRequest
{
private $test_endpoint = 'https://sbcheckout.payfort.com/FortAPI/paymentPage';

private $live_endpoint = 'https://checkout.payfort.com/FortAPI/paymentPage';

public function validateData()
{
$this->validate('access_code', 'merchant_identifier', 'merchant_reference', 'amount', 'currency', 'language', 'customer_email', 'signature');
}
}
Loading

0 comments on commit 24428a5

Please sign in to comment.