Paypal不会继续收到付款

时间:2016-09-26 13:15:43

标签: php api paypal paypal-rest-sdk

我正在尝试使用this网址通过PayPal进行捐赠。当我登录PayPal并付款时,我收到以下消息 enter image description here

我确实有后端代码,这就是它的样子。

<?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调用似乎也很好。

任何有关这项工作的建议都会有所帮助。

1 个答案:

答案 0 :(得分:3)

您的var_dump()语句可能违反&#34;标题之前没有内容输出&#34;规则因此,在header('Location: ' . $redirectUrl);可以重定向到PayPal完成工作之前,用户的付款流程会停止使用您的代码。

请尝试删除这些var_dump,然后执行error_log(printf($val, true)),以免中断对header()的调用。

修改 - 来自http://php.net/manual/en/function.header.php

  

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

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>