Skip to content

Commit

Permalink
Merge pull request #2 from pixelandtonic/master
Browse files Browse the repository at this point in the history
General fixes and improvements
  • Loading branch information
tolu-paystack authored May 21, 2020
2 parents 211f1b5 + d238643 commit 9079b9d
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
composer.lock
vendor
build
.idea
.DS_Store
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Omnipay: Paystack
# Paystack support for Omnipay

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

*TODO* Add description

## Install

Instal the gateway using require. Require the `league/omnipay` base package and this gateway.
Install the gateway using require. Require the `league/omnipay` base package and this gateway.

```bash
$ composer require league/omnipay paystackhq/omnipay-paystack
Expand All @@ -17,7 +15,7 @@ $ composer require league/omnipay paystackhq/omnipay-paystack

The following gateways are provided by this package:

- Paystack
- Paystack (Offsite Redirect)

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

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"omnipay/common": "v3.0"
"omnipay/common": "^3.0"
},
"require-dev": {
"omnipay/tests": "~3.0",
Expand All @@ -29,7 +29,7 @@
},
"autoload-dev": {
"psr-4": {
"League\\Paystack\\Test\\": "tests"
"Omnipay\\Paystack\\Test\\": "tests"
}
},
"scripts": {
Expand Down
44 changes: 38 additions & 6 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,82 @@
namespace Omnipay\Paystack;

use Omnipay\Common\AbstractGateway;
use Omnipay\Paystack\Message\CompletePurchaseRequest;
use Omnipay\Paystack\Message\PurchaseRequest;
use Omnipay\Paystack\Message\PurchaseResponse;

/**
* Skeleton Gateway
* PayStack Gateway
*
*/
class Gateway extends AbstractGateway
{
/**
* @return string
*/
public function getName()
{
return 'Paystack';
}

/**
* @return array
*/
public function getDefaultParameters()
{
return array(
return [
'secret_key' => '',
'public_key' => '',
);
];
}

/**
* @return mixed
*/
public function getSecretKey()
{
return $this->getParameter('secret_key');
}

/**
* @param $value
* @return Gateway
*/
public function setSecretKey($value)
{
return $this->setParameter('secret_key', $value);
}

/**
* @return mixed
*/
public function getPublicKey()
{
return $this->getParameter('public_key');
}

/**
* @param $value
* @return Gateway
*/
public function setPublicKey($value)
{
return $this->setParameter('public_key', $value);
}

/**
* @return Message\AuthorizeRequest
* @inheritDoc
*/
public function purchase(array $parameters = [])
{
return $this->createRequest(PurchaseRequest::class, $parameters);
}

/**
* @inheritDoc
*/
public function purchase(array $parameters = array())
public function completePurchase(array $parameters = [])
{
return $this->createRequest('\Omnipay\Skeleton\Message\AuthorizeRequest', $parameters);
return $this->createRequest(CompletePurchaseRequest::class, $parameters);
}
}
40 changes: 40 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Exception\BadMethodCallException;
use Omnipay\Common\Exception\InvalidRequestException;

abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
protected $baseApiEndpoint = 'https://api.paystack.co/';

/**
* @return string The URL endpoint for the request
* @throws InvalidRequestException
*/
public function getApiEndpoint()
{
throw new InvalidRequestException('Api endpoint not implemented');
}

public function getPublicKey()
{
return $this->getParameter('public_key');
}

public function setPublicKey($value)
{
$this->setParameter('public_key', $value);
}

public function getSecretKey()
{
return $this->getParameter('secret_key');
}

public function setSecretKey($value)
{
$this->setParameter('secret_key', $value);
}
}
42 changes: 27 additions & 15 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,40 @@

class CompletePurchaseRequest extends AbstractRequest
{
/**
* @inheritDoc
*/
public function getApiEndpoint()
{
return $this->baseApiEndpoint . 'transaction/verify/' . rawurlencode($this->getTransactionReference());
}

/**
* @inheritDoc
*/
public function getData()
{
return [];
}

/**
* @inheritDoc
*/
public function sendData($data)
{
try {
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paystack.co/transaction/verify" . rawurlencode($reference),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"authorization: Bearer " . $this->getSecretKey(),
"content-type: application/json",
"cache-control: no-cache"
],
));

$response = curl_exec($curl);
$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$tranx = json_decode($response, true);
$response = $this->httpClient->request('GET', $this->getApiEndpoint(), $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);
} catch (\Exception $e) {
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

return $this->response = new CompletePurchaseResponse($this, $tranx['data']);
return $this->response = new CompletePurchaseResponse($this, $responseData);
}
}
45 changes: 38 additions & 7 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,58 @@

class CompletePurchaseResponse extends \Omnipay\Common\Message\AbstractResponse
{

public function isSuccessful()
{
return 'success' === $this->getCode();
}


public function getTransactionId()
{
return $this->data['id'];
if (isset($this->data['data']) && $data = $this->data['data']) {
if ($data['reference']) {
return $data['reference'];
}
}
}

public function getMessage()
{
if (isset($this->data['data']) && $data = $this->data['data']) {
if ($data['message']) {
return $data['message'];
}
}

if (isset($this->data['data']) && $data = $this->data['data']) {
if ($data['gateway_response']) {
return $data['gateway_response'];
}
}

if (isset($this->data['message']) && $message = $this->data['message']) {
return $message;
}

return '';
}


public function getTransactionReference()
{
return $this->data['reference'];
if (isset($this->data['data']) && $data = $this->data['data']) {
return $data['reference'];
}

return '';
}


public function getCode()
{
return $this->data['status'];
if (isset($this->data['data']) && $data = $this->data['data']) {
return $data['status'];
}

return '';
}
}
58 changes: 30 additions & 28 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
<?php
namespace Omnipay\Paystack\Messages;

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Exception\InvalidRequestException;

class PurchaseRequest extends AbstractRequest
{
/**
* {@inheritdoc}
* @inheritDoc
*/
public function getApiEndpoint()
{
return $this->baseApiEndpoint . '/transaction/initialize/';
}

/**
* @inheritdoc
*/
public function getData()
{
//$this->validate('amount', 'email');
$amount = $this->getAmount();
$this->validate('amount', 'email');

$amount = $this->getAmountInteger();
$email = $this->getParameter('email');
$reference = $this->getTransactionReference();

return [
'amount' => $amount*100,
'email' => $this->getParameter('email'),
'callback_url' => $this->getReturnUrl()
'amount' => $amount,
'email' => $email,
'callback_url' => $this->getReturnUrl(),
'reference' => $reference
];
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function sendData($data)
{
try {
$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$secret_key = $this->getParameter('secret_key');
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paystack.co/transaction/initialize",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => [
"authorization: Bearer $secret_key",
"content-type: application/json",
"cache-control: no-cache"
],
));

$response = curl_exec($curl);

$tranx = json_decode($response, true);
$response = $this->httpClient->request('POST', $this->getApiEndpoint(), $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);
} catch (\Exception $e) {
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

return $this->response = new PurchaseResponse($this, $tranx['data']);
return $this->response = new PurchaseResponse($this, $responseData);
}

/**
* Sets the email parameter.
*
* @param string $email
* @param string $email
* @return $this
*/
public function setEmail($email)
Expand Down
Loading

0 comments on commit 9079b9d

Please sign in to comment.