贝宝(Paypal)PHP Rest API SDK-现在似乎无法正常工作

时间:2018-07-10 09:29:49

标签: php paypal paypal-sandbox paypal-rest-sdk

我目前正在尝试测试我的paypal rest api sdk实现。但是,当我通过沙盒帐户登录时,它要花很长时间才能加载,然后我收到“事情似乎现在无法正常工作”。

在我将其范围缩小到贝宝方面之前,我只想确保我的代码设置正确。我读到代码中的错误可能会导致这种情况。

<?php 
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;

require 'vendor/autoload.php';

$paypal = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
    '***',
    '***'
    )
);

if (isset($_GET['created'], $_GET['paymentId'], $_GET['PayerID'])){
    $paymentId = $_GET['paymentId'];
    $payerId = $_GET['PayerID'];

    $payment = Payment::get($paymentId, $paypal);

    $execute = new PaymentExecution();
    $execute->setPayerId($payerId);

    try{
        $result = $payment->execute($execute, $paypal);
        //Success
        header("Location: createInstance.php?created");
    }catch(Exception $e){
        $data = json_decode($e-getData());
        header("Location: createInstance.php?error&message=".$data->message);
        die();
    }

}else{
    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $item = new Item();
    $item->setName('Test Item')
        ->setCurrency('AUD')
        ->setQuantity(1)
        ->setPrice('9.99');

    $itemList = new ItemList();
    $itemList->setItems([$item]);

    $amount = new Amount();
    $amount->setCurrency('AUD')
        ->setTotal('9.99');

    $transaction = new Transaction();
    $transaction->setAmount($amount)
        ->setItemList($itemList)
        ->setDescription('Test Description')
        ->setInvoiceNumber(uniqid());

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl('******') <--Full Url
        ->setCancelUrl('******'); <--Full URL

    $payment = new Payment();
    $payment->setIntent('sale')
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions([$transaction]);

    try{
        $payment->create($paypal);
    }catch(Exception $e){
        die($e);
    }

    $approvalUrl = $payment->getApprovalLink();
    header("Location: {$approvalUrl}");
}
?>

0 个答案:

没有答案