Код: Выделить всё
Payment failed: Authentication failed due to invalid authentication credentials or a missing Authorization header.
Код: Выделить всё
public function generatePaypalPaymentByTransaction(Transaction $transaction) {
try {
$gateway = Omnipay::create('PayPal_Rest');
$gateway->setClientId(config('paypal.client_id'));
$gateway->setSecret(config('paypal.secret'));
$gateway->setTestMode(config('paypal.test_mode'));
$urlHost = rtrim(config('paypal.url_host'), '/') . '/';
if($transaction->type != Transaction::DEPOSIT_TYPE) {
$username = User::find($transaction->recipient_user_id)->username;
$username = rawurlencode($username); // Codificar el username para la URL
}
$returnUrl = $urlHost . 'pay/execute-payment' . '?transaction_id=' . $transaction->id;
if($transaction->type == Transaction::ROULETTE) {
$cancel_url = $urlHost . 'roulette/' . $username . '/' . $transaction->roulette_id;
} else {
$cancel_url = $urlHost . 'feed';
}
if($transaction->type == 'deposit') {
// Incrementa el amount en un 10%
$transaction->amount = $transaction->amount * 1.10;
}
$response = $gateway->purchase([
'amount' => $transaction->amount,
'currency' => config('paypal.currency'),
'returnUrl' => $returnUrl,
'cancelUrl' => $cancel_url,
])->send();
if($response->isRedirect()) {
$response->redirect();
} else {
return $response->getMessage();
}
} catch (Throwable $e){
return $e->getMessage();
}
}
Код: Выделить всё
public function completePurchase($PayerID, $paymentId, $amount)
{
try {
Log::info("Pre Complte Purchase");
$response = $this->gateway->completePurchase([
'payer_id' => $PayerID,
'transactionReference' => $paymentId,
'amount' => $amount,
'currency' => env('PAYPAL_CURRENCY'),
])->send();
Log::info("Post Complte Purchase");
if ($response->isSuccessful()) {
Log::info('Payment completed successfully: ' . json_encode($response->getData()));
} else {
Log::error('Payment failed: ' . $response->getMessage());
}
return $response;
} catch (Throwable $e){
return $e->getMessage();
}
}
Код: Выделить всё
public function success (Request $request)
{
$transaction = Transaction::find($request->transaction_id);
Log::info("Request All");
Log::info($request->all());
$response = $this->completePurchase($request->PayerID, $request->paymentId, $request->amount);
if ($response instanceof \Omnipay\Common\Message\ResponseInterface && $response->isSuccessful()) {
// El pago se completó con éxito
Log::info('Payment completed successfully.');
} else {
// El pago falló
Log::error('Payment failed or an error occurred.');
// Actualizar el estado de la transacción a 'declined'
$transaction->status = Transaction::DECLINED_STATUS;
$transaction->save();
if($transaction->type === Transaction::ROULETTE) {
// Redirigir al usuario a la página de error
return redirect()->route('roulette.show', ['username' => rawurlencode(User::find($transaction->recipient_user_id)->username), 'roulette_id' => $transaction->roulette_id])
->with('error', __('Payment failed, please try again.'));
} else if ($transaction->type === Transaction::TIP_TYPE) {
// Redirigir al usuario a la página de error
return redirect()->route('profile', ['username' => rawurlencode(User::find($transaction->recipient_user_id)->username)])
->with('error', __('Payment failed, please try again.'));
} else {
// Redirigir al usuario a la página de error
return redirect()->route('feed')->with('error', __('Payment failed, please try again.'));
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... cation-cre