Skip to content

Instantly share code, notes, and snippets.

@ccmelas
Created August 10, 2019 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccmelas/7f378b78c82f61c7066a727c8e046065 to your computer and use it in GitHub Desktop.
Save ccmelas/7f378b78c82f61c7066a727c8e046065 to your computer and use it in GitHub Desktop.
<?php
/**
* Paystack payment service.
*/
namespace App\Payments;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
class Paystack implements PaymentService
{
/**
* @var $client
*/
protected $client;
/**
* @var $secretKey
*/
protected $secretKey;
public function __construct(Client $client)
{
$this->client = $client;
$this->secretKey = config('paystack.secretKey');
}
/**
* Verifies a transaction
* @param array $paymentData
* @return bool|mixed
* @throws \Exception
*/
public function verify(array $paymentData)
{
//The parameter after verify/ is the transaction reference to be verified
$verificationUrl = "https://api.paystack.co/transaction/verify/" . @$paymentData['reference'];
$response = $this->client->get($verificationUrl, [
'headers' => [
'Authorization' => "Bearer $this->secretKey"
]
]);
$decodedResponse = json_decode($response->getBody()->getContents(), true);
Log::channel('payments')->info($decodedResponse);
if (array_key_exists('data', $decodedResponse)
&& array_key_exists('status', $decodedResponse['data']) &&
($decodedResponse['data']['status'] === 'success')) {
return $decodedResponse['data'];
}
throw new \Exception("Payment failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment