Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony上的Paypal Rest API_Php_Rest_Paypal_Payment Gateway_Symfony - Fatal编程技术网

Php Symfony上的Paypal Rest API

Php Symfony上的Paypal Rest API,php,rest,paypal,payment-gateway,symfony,Php,Rest,Paypal,Payment Gateway,Symfony,我正在尝试将paypal Rest API嵌入到我的symfony项目中,但它返回了一个错误,甚至技术服务也无法告诉我问题可能来自何处 返回的错误如下:“试图从命名空间“PayPal\Rest”加载类“ApiContext”。是否忘记了另一个命名空间的“use”语句?” 但是我已经有了正确的use语句,并且api已经加载了composer 这里是完整的代码,如果它可以帮助 <?php namespace PlatformBundle\Controller; use Symfony\Bu

我正在尝试将paypal Rest API嵌入到我的symfony项目中,但它返回了一个错误,甚至技术服务也无法告诉我问题可能来自何处

返回的错误如下:“试图从命名空间“PayPal\Rest”加载类“ApiContext”。是否忘记了另一个命名空间的“use”语句?”

但是我已经有了正确的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();
    }
}
这行吗

private function paypalKeys()
    {
        return new \ApiContext(
            new OAuthTokenCredential(
                'mykey',
                'mykey'
            )
        );
}
试着在调用类之前加一个“\”并告诉它是否有效


您还应该检查ApiContext中的名称空间是否正常。:)

它不会改变任何东西:/@Fabulusco你能给我看一下ApiContext文件的开头吗?名称空间?是的,我刚刚在帖子的附件中添加了它:)谢谢helping@FabulousCo您还可以将composer部分放在添加此api的地方吗?这里是:“paypal/rest api sdk php”:“1.3.*”,
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
     *