Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Paypal未继续接收付款_Php_Api_Paypal_Paypal Rest Sdk - Fatal编程技术网

Php Paypal未继续接收付款

Php Paypal未继续接收付款,php,api,paypal,paypal-rest-sdk,Php,Api,Paypal,Paypal Rest Sdk,我正试图通过paypal使用URL进行捐赠。当我登录PayPal并付款时,我收到以下消息 我有后端代码,下面是它的样子 <?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\Pa

我正试图通过paypal使用URL进行捐赠。当我登录PayPal并付款时,我收到以下消息

我有后端代码,下面是它的样子

<?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);
    }

}

您拥有的
var_dump()
语句可能违反了“标题前无内容输出”规则,因此,用户的支付流在
标题('Location:'.$redirectUrl)之前,使用您的代码停止可以重定向到PayPal以完成作业

尝试删除那些
var\u dump
s,并改为执行
error\u log(printf($val,true))
,以免中断对
header()的调用

编辑-来自:

记住,在发送任何实际输出之前,必须调用header(),可以是通过普通HTML标记、文件中的空行,也可以是通过PHP。使用include或require函数或其他文件访问函数读取代码,并在调用header()之前输出空格或空行,这是非常常见的错误。使用单个PHP/HTML文件时也存在同样的问题



您好@cameron hurd您的意思是我应该将上述代码的var_dump($val)替换为error_log(printf($val,true)),没错-
error_log
不输出任何内容,将
true
作为第二个参数传递给
printf
返回,而不是输出值。如果输出前的内容存在问题,这将允许您在检查变量值时避开该问题。我进行了替换,现在它只打印单词“Array”,而不是前面的消息(如图所示):(我看到你在这个类的某个地方有一个
var_dump_str
函数……它被调用了吗?它能输出
数组
文本吗?游戏的名字是找到输出的来源,以便测试
header()
无法重定向用户。我在前端页面中调用$current.=self::var_dump_str($log)。“\n”;它的定义如下:静态函数var_dump_str(){$argc=func_num_args();$argv=func_get_args();if($argc>0){ob start();调用_user_func_数组('var_dump',$argv)$result=ob_get_contents();ob_end_clean();return$result;}return“”;}请首先验证您的paypal帐户。如果您的代码正常,则此步骤可能会解决您的问题。
<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>