Created
August 10, 2019 22:53
-
-
Save ccmelas/7f378b78c82f61c7066a727c8e046065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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