我试图在我的 symfony 项目中嵌入 paypal Rest API,但它返回一个错误,甚至技术服务也无法告诉我问题出在哪里...
返回的错误如下:“试图从 namespace “PayPal\Rest”加载类“ApiContext”。您是否忘记了另一个 namespace 的“使用”语句?”
但是我已经有了正确的 use 语句并且 api 已经用 composer 加载了。
这是完整的代码,如果它能帮助...
<?php
namespace PlatformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
class PaypalController extends Controller
{
private function paypalKeys()
{
return new ApiContext(
new OAuthTokenCredential(
'mykey',
'mykey'
)
);
}
public function chargeAction()
{
$this->paypalKeys();
$price = 10;
$total = $price;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName('transfer')
->setCurrency('eur')
->setQuantity(1)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping(0)
->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency('eur')
->setTotal($total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('Description')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://localhost/dublin/web/app_dev.php/admin')
->setCancelUrl('http://localhost/dublin/web/app_dev.php/cancel');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
// try {
$payment->create($this->paypalKeys());
// } catch (Exception $e) {
// die($e);
// }
//
// return new Response('true');
// echo $approvalUrl = $payment->getApprovalLink();
}
}
谢谢你...
附件:
我的 ApiContext 文件的开头
namespace PayPal\Rest;
use PayPal\Core\PayPalConfigManager;
use PayPal\Core\PayPalCredentialManager;
/**
* Class ApiContext
*
* Call level parameters such as request id, credentials etc
*
* @package PayPal\Rest
*/
class ApiContext
{
/**
* Unique request id to be used for this call
* The user can either generate one as per application
* needs or let the SDK generate one
*
最佳答案
这行得通吗?
private function paypalKeys()
{
return new \ApiContext(
new OAuthTokenCredential(
'mykey',
'mykey'
)
);
}
尝试在调用类之前放置一个“\”并判断它是否有效。
您还应该检查 ApiContext 中的命名空间是否正常。 :)
关于php - Symfony 上的 Paypal Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37418343/