简体   繁体   English

贝宝PHP SDK错误

[英]Paypal PHP SDK Error

so I want to integrate paypal payments to my site. 所以我想将贝宝付款集成到我的网站。

I tried integrating the paypal button form straight from paypal but there is a problem with it. 我尝试直接从贝宝集成贝宝按钮表单,但存在问题。 The price of the product can be changed by easily inspecting the element and changing the price input. 可以通过轻松检查元素并更改price输入来更改产品的price

I found out about the Paypal PHP SDK so I installed it via composer. 我找到了有关Paypal PHP SDK的信息,因此我通过composer安装了它。 The problem I'm having is when I want the user to pay for the product. 我遇到的问题是当我希望用户为产品付款时。

I copied and pasted the Create Payment method found here . 我复制并粘贴了在此处找到的“创建付款”方法。 But I'm getting and error: 但是我得到和错误:

Notice: Undefined variable: apiContext in C:\xampp\htdocs\paypal\index.php on line 62

Fatal error: Class 'ResultPrinter' not found in C:\xampp\htdocs\paypal\index.php on line 64

I know I'm getting this error because I haven't initialized this variable. 我知道我收到此错误,因为我尚未初始化此变量。 I don't know where to create it and what this variable it must contain. 我不知道在哪里创建它以及它必须包含什么变量。 Does anyone know how to fully integrate this? 有谁知道如何完全整合吗?

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks in advanced! 提前致谢!

This is my full PHP code: 这是我完整的PHP代码:

<?php

require 'vendor/autoload.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;

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

$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
    ->setCurrency('USD')
    ->setQuantity(1)
    ->setSku("123123") // Similar to `item_number` in Classic API
    ->setPrice(7.5);
$item2 = new Item();
$item2->setName('Granola bars')
    ->setCurrency('USD')
    ->setQuantity(5)
    ->setSku("321321") // Similar to `item_number` in Classic API
    ->setPrice(2);

$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));

$details = new Details();
$details->setShipping(1.2)
    ->setTax(1.3)
    ->setSubtotal(17.50);

$amount = new Amount();
$amount->setCurrency("USD")
    ->setTotal(20)
    ->setDetails($details);

$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription("Payment description")
    ->setInvoiceNumber(uniqid());

//I changed the base url and the return url's to mine.
$baseUrl = "http://www.localhost/paypal";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
    ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");

$payment = new Payment();
$payment->setIntent("sale")
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions(array($transaction));

$request = clone $payment;

try {
    $payment->create($apiContext);
} catch (Exception $ex) {
    ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
    exit(1);
}

$approvalUrl = $payment->getApprovalLink();

ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $payment);

return $payment;

It looks like you've not defined $apiContext . 看来您尚未定义$apiContext I'm not totally familiar with this SDK but it looks like you can create one like so: 我对这个SDK并不完全熟悉,但是看起来您可以像这样创建一个:

$apiContext = new \Paypal\Rest\ApiContext(
    new \PayPal\Auth\AuthTokenCredential(
        $clientId,
        $clientSecret
    )
);

reference 参考

You will need to pass in your $clientId and $clientSecret that you have obtained from PayPal. 您将需要传入从贝宝(PayPal)获得的$clientId$clientSecret

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM