我正在尝试通过此URL使用Paypal进行捐赠。当我登录PayPal并进行付款时,我会收到以下消息:
我有后端代码,它看起来像这样。
我查看了日志,没有报告任何问题。API调用似乎也正常。
有什么建议可以让它正常工作会很有帮助。
![enter image description here](https://istack.dev59.com/g2Zva.webp)
<?php
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
require __DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php';
/**
* Edri PayPal Pyment
*/
class Edri_PayPal_Payment
{
private $api;
private $payer;
private $details;
private $amount;
private $transaction;
private $payment;
private $redirectUrls;
function __construct()
{
$this->api = $this->setup_PayPal_Api();
}
private function debug($val)
{
echo '<pre>';
var_dump($val);
echo '</pre>';
}
private function setup_PayPal_Api()
{
$api = new ApiContext(
new OAuthTokenCredential(
'MyPayPalClientID',
'MyClientSecret'
)
);
$api->setConfig(array(
'mode' => 'live',
'http.ConnectionTimeOut' => 30,
'Log.LogEnabled' => false,
'Log.FileName' => '',
'Log.LogLevel' => 'FINE',
'validation.level' => 'log'
));
return $api;
}
private function setupPayer()
{
$this->payer = new Payer();
$this->payer->setPayment_method('paypal');
}
private function setupDetails($amount)
{
$this->details = new Details();
$this->details->setShipping('0.00')
->setTax('0.00')
->setsubTotal($amount);
}
private function setupAmount($amount)
{
$this->amount = new Amount();
$this->amount->setCurrency('EUR')
->setTotal($amount)
->setDetails($this->details);
}
private function setupTransaction($amount)
{
$this->transaction = new Transaction();
$this->transaction->setAmount($this->amount)
->setDescription('Make a donation of €' . $amount . ' to EDRi');
}
private function setupPayment()
{
$this->payment = new Payment();
$this->payment->setIntent('sale')
->setPayer($this->payer)
->setTransactions(array($this->transaction))
->setRedirectUrls($this->redirectUrls);
}
private function setupRedirectUrls()
{
$this->redirectUrls = new RedirectUrls();
$this->redirectUrls->setReturnUrl('https://edri.org/payment?pppa=true')
->setCancelUrl('https://edri.org/payment?pppa=false');
}
public function prepare_payment ($paymentCredtials) {
$amount = str_replace(',', '', number_format($paymentCredtials['edriPayment_amount'], 2));
$this->setupPayer();
$this->setupDetails($amount);
$this->setupAmount($amount);
$this->setupTransaction($amount);
$this->setupRedirectUrls();
$this->setupPayment();
try {
$this->payment->create($this->api);
$paymentID = $this->payment->getId();
} catch (Exception $e) {
$this->log($e);
header('Location: https://edri.org/donation-oops');
return false;
}
return $paymentID;
}
private function log($log){
$file = __DIR__ . DIRECTORY_SEPARATOR . '../logs/paypal_log.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $prefix . ' ' . date('m/d/Y h:i:s a', time()) . ' //// ' . "\n";
$current .= self::var_dump_str($log) . "\n";
// Write the contents back to the file
file_put_contents($file, $current);
}
public function execute_payment($paymentCredentials)
{
$this->debug($paymentCredentials);
$payment = Payment::get($paymentCredentials['paymentId'], $this->api);
$execution = new PaymentExecution();
$execution->setPayerId($paymentCredentials['PayerID']);
try {
echo $payment->execute($execution, $this->api);
} catch (Exception $e) {
$this->log($e);
header('Location: https://edri.org/donation-oops');
return false;
}
return $payment->state = 'approved' ? true : false;
}
public function kickoff_payment()
{
foreach ($this->payment->getLinks() as $link) {
if ($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
}
}
header('Location: ' . $redirectUrl);
}
}
我查看了日志,没有报告任何问题。API调用似乎也正常。
有什么建议可以让它正常工作会很有帮助。