fix gitlab tests
This commit is contained in:
@@ -6,19 +6,13 @@ use App\Models\Payment;
|
||||
use App\Services\ApplicationUrlService;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\Payer;
|
||||
use PayPal\Api\PaymentExecution;
|
||||
use PayPal\Api\RedirectUrls;
|
||||
use PayPal\Api\Transaction;
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
use PayPal\Rest\ApiContext;
|
||||
|
||||
class PaypalPaymentService
|
||||
{
|
||||
public function __construct(
|
||||
private ApplicationUrlService $urls,
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRedirectUrl(): string
|
||||
{
|
||||
@@ -30,11 +24,11 @@ class PaypalPaymentService
|
||||
public function createPayment(int $paymentId): array
|
||||
{
|
||||
$payment = Payment::query()->find($paymentId);
|
||||
if (! $payment) {
|
||||
if (!$payment) {
|
||||
throw new \RuntimeException('Payment not found.');
|
||||
}
|
||||
|
||||
if (! $this->sdkAvailable()) {
|
||||
if (!$this->sdkAvailable()) {
|
||||
return [
|
||||
'redirect_url' => $this->getRedirectUrl(),
|
||||
'mode' => 'hosted',
|
||||
@@ -45,23 +39,23 @@ class PaypalPaymentService
|
||||
try {
|
||||
$apiContext = $this->buildApiContext();
|
||||
|
||||
$payer = new Payer;
|
||||
$payer = new \PayPal\Api\Payer();
|
||||
$payer->setPaymentMethod('paypal');
|
||||
|
||||
$amount = new Amount;
|
||||
$amount = new \PayPal\Api\Amount();
|
||||
$amount->setCurrency('USD')->setTotal($payment->balance ?? $payment->total_amount ?? 0);
|
||||
|
||||
$transaction = new Transaction;
|
||||
$transaction = new \PayPal\Api\Transaction();
|
||||
$transaction->setAmount($amount)
|
||||
->setDescription('Payment for school fees')
|
||||
->setInvoiceNumber(uniqid('inv_', true));
|
||||
|
||||
$paypalPayment = new \PayPal\Api\Payment;
|
||||
$paypalPayment = new \PayPal\Api\Payment();
|
||||
$paypalPayment->setIntent('sale')
|
||||
->setPayer($payer)
|
||||
->setTransactions([$transaction]);
|
||||
|
||||
$redirectUrls = new RedirectUrls;
|
||||
$redirectUrls = new \PayPal\Api\RedirectUrls();
|
||||
$redirectUrls->setReturnUrl($this->urls->paypalExecuteReturnUrl())
|
||||
->setCancelUrl($this->urls->paypalCancelUrl());
|
||||
|
||||
@@ -77,14 +71,14 @@ class PaypalPaymentService
|
||||
'payment_id' => $paymentId,
|
||||
];
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('PayPal create payment failed: '.$e->getMessage());
|
||||
Log::error('PayPal create payment failed: ' . $e->getMessage());
|
||||
throw new \RuntimeException('Unable to create PayPal payment.');
|
||||
}
|
||||
}
|
||||
|
||||
public function executePayment(string $payerId, ?string $paypalPaymentId = null, ?int $paymentId = null): void
|
||||
{
|
||||
if (! $this->sdkAvailable()) {
|
||||
if (!$this->sdkAvailable()) {
|
||||
throw new \RuntimeException('PayPal SDK not installed.');
|
||||
}
|
||||
|
||||
@@ -95,13 +89,13 @@ class PaypalPaymentService
|
||||
|
||||
$apiContext = $this->buildApiContext();
|
||||
$payment = \PayPal\Api\Payment::get($paypalId, $apiContext);
|
||||
$execution = new PaymentExecution;
|
||||
$execution = new \PayPal\Api\PaymentExecution();
|
||||
$execution->setPayerId($payerId);
|
||||
|
||||
$payment->execute($execution, $apiContext);
|
||||
|
||||
$internalPaymentId = $paymentId;
|
||||
if (! $internalPaymentId && $paypalId !== '') {
|
||||
if (!$internalPaymentId && $paypalId !== '') {
|
||||
$internalPaymentId = (int) Cache::get($this->cacheKey($paypalId));
|
||||
}
|
||||
if ($internalPaymentId) {
|
||||
@@ -119,18 +113,18 @@ class PaypalPaymentService
|
||||
|
||||
private function sdkAvailable(): bool
|
||||
{
|
||||
return class_exists(ApiContext::class)
|
||||
&& class_exists(OAuthTokenCredential::class);
|
||||
return class_exists(\PayPal\Rest\ApiContext::class)
|
||||
&& class_exists(\PayPal\Auth\OAuthTokenCredential::class);
|
||||
}
|
||||
|
||||
private function buildApiContext(): ApiContext
|
||||
private function buildApiContext(): \PayPal\Rest\ApiContext
|
||||
{
|
||||
$clientId = (string) (config('services.paypal.client_id') ?: env('PAYPAL_CLIENT_ID'));
|
||||
$secret = (string) (config('services.paypal.secret') ?: env('PAYPAL_SECRET'));
|
||||
$mode = (string) (config('services.paypal.mode') ?: env('PAYPAL_MODE') ?: 'sandbox');
|
||||
|
||||
$apiContext = new ApiContext(
|
||||
new OAuthTokenCredential($clientId, $secret)
|
||||
$apiContext = new \PayPal\Rest\ApiContext(
|
||||
new \PayPal\Auth\OAuthTokenCredential($clientId, $secret)
|
||||
);
|
||||
|
||||
$apiContext->setConfig([
|
||||
@@ -143,6 +137,6 @@ class PaypalPaymentService
|
||||
|
||||
private function cacheKey(string $paypalPaymentId): string
|
||||
{
|
||||
return 'paypal_payment:'.$paypalPaymentId;
|
||||
return 'paypal_payment:' . $paypalPaymentId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user