Paypal PHP SDK实现

时间:2018-01-02 10:01:23

标签: php paypal

我的paypal sdk版本是^ 1.13

我的paypal实现有错误。我按照本教程 https://www.youtube.com/watch?v=BD1dOWIABe0

<?php
require 'vendor/autoload.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\Details;
use PayPal\Rest\ApiContext;
Use PayPal\Auth\OAuthTokenCredential;



if(!isset($_POST['product'], $_POST['price'])){
die();
}

$clientId = 'client-id';
$clientSecret = 'client-secret';

$apiContext = new ApiContext(
new OAuthTokenCredential(
$clientId,
$clientSecret
)
);

$product = $_POST['product'];
$price = $_POST['price'];
$tax = 2.00;

$total = $price + $tax;

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

$item = new Item();
$item->setName($product)
->setCurrency('USD')
->setQuantity(1)
->setPrice($price);

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

$details = new Details();
$details->setShipping($tax)
->setSubtotal($price)
->setTax(2.00);

$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($product);

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

$baseUrl = "http://www.localhost/xc";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/pay.php?success=true")
->setCancelUrl("$baseUrl/pay.php?success=false");

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


try{

$payment->create($apiContext);

}
catch(PayPal\Exception\PayPalConnectionException $ex){
echo $ex;
die($ex);
}

echo $approvalUral = $payment->getApprovalLink(); 

我试图搜索错误,但我似乎无法找到答案,所以我需要一些帮助。我做错了什么?我需要降级吗?因为2年前教程中的视频所以我认为我需要降级但是有可能吗?

  

异常'PayPal \ Exception \ PayPalConnectionException',并在访问https://api.sandbox.paypal.com/v1/payments/payment时收到消息'获取Http响应代码400'。在C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Core \ PayPalHttpConnection.php:202
  堆栈跟踪:
  #0 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Transport \ PayPalRestCall.php(78):PayPal \ Core \ PayPalHttpConnection-&gt;执行('{“意图“:” 销售......')
  #1 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Common \ PayPalResourceModel.php(104):PayPal \ Transport \ PayPalRestCall-&gt; execute(Array,'/ v1 / payments / pa ...','POST','{“intent”:“sale ...',NULL)
  #2 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Api \ Payment.php(577):PayPal \ Common \ PayPalResourceModel :: executeCall('/ v1 / payments / pa ...','POST','{“intent”:“sale ...”,NULL,Object(PayPal \ Rest \ ApiContext),NULL)
  #3 C:\ xampp \ htdocs \ ladea \ checkout.php(78):PayPal \ Api \ Payment-&gt;创建(对象(PayPal \ Rest \ ApiContext))
  #4 {main} exception'PayPal \ Exception \ PayPalConnectionException',并在访问https://api.sandbox.paypal.com/v1/payments/payment时收到消息'获取Http响应代码400'。在C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Core \ PayPalHttpConnection.php:202堆栈跟踪:#0 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Transport \ PayPalRestCall.php(78):PayPal \ Core \ PayPalHttpConnection-&gt;执行('{“意图”:“销售......”)#1 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Common \ PayPalResourceModel.php(104):PayPal \ Transport \ PayPalRestCall-&gt; execute(Array,'/ v1 / payments / pa ...','POST','{“intent”:“sale ...',NULL)#2 C:\ xampp \ htdocs \ ladea \ vendor \ paypal \ rest-api-sdk-php \ lib \ PayPal \ Api \ Payment.php(577):PayPal \ Common \ PayPalResourceModel :: executeCall('/ v1 / payments / pa ...','POST','{“intent”:“sale ...',NULL,对象(PayPal \ Rest \ ApiContext),NULL)#3 C:\ xampp \ htdocs \ ladea \ checkout.php(78):PayPal \ Api \ Payment-&gt; create(Object(PayPal \ Rest \ ApiContext))#4 {主要}

谢谢你和问候。

2 个答案:

答案 0 :(得分:0)

删除您的* .paypal.com Cookie。 这是PayPal系统中的一个不幸的错误,其中cookie太大而服务器错误。 您可以使用两种不同的浏览器来避免此问题,例如用于PayPal Live的IE和用于PayPal Sandbox的FF。 也可以使用隐身模式进行检查。

答案 1 :(得分:0)

我已在您的GitHub问题中提供此信息,但会在此提供帮助。

我使用wiki中的信息更新了代码示例的异常输出,其中提供了一些其他信息,400是格式错误的请求https://github.com/paypal/PayPal-PHP-SDK/wiki/exception-%27PayPal%5CException%5CPayPalConnectionException%27-with-message-%27Got-Http-response-code-400-when-accessing

event.data.delta

如果您的总额是商品的税和价格,则不要设置运费。此外,amount-&gt; setDetails应该是详细信息。

$details = new Details();
$details->setShipping($tax)
->setSubtotal($price)
->setTax(2.00);

$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($product);