简体   繁体   English

在Symfony上使用Paypal Rest API

[英]Paypal Rest API on Symfony

I'm trying to embed the paypal Rest API on my symfony project, but it returns me an error and even the technical service couldn't tell me where the problem could come from... 我正在尝试将paypal Rest API嵌入我的symfony项目中,但是它返回一个错误,甚至技术服务人员也无法告诉我问题可能出在哪里……

The error returned is the following : "Attempted to load class "ApiContext" from namespace "PayPal\\Rest". Did you forget a "use" statement for another namespace?" 返回的错误如下:“试图从命名空间“ PayPal \\ Rest”加载类“ ApiContext”。您是否忘记了另一个命名空间的“ use”语句?

But I already have the right use statement and the api has already been loaded with composer. 但是我已经有了正确的use语句,并且api已经加载了composer。

Here is the whole code, if it can help... 如果可以帮助的话,这是完整的代码...

<?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();
    }
}

thank you... 谢谢...

Annex : 附件:

Beginning of my ApiContext file 我的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
     *

Could this work? 这行得通吗?

private function paypalKeys()
    {
        return new \ApiContext(
            new OAuthTokenCredential(
                'mykey',
                'mykey'
            )
        );
}

Try putting a "\\" before calling the class and tell if it works. 在调用该类之前,尝试放置一个“ \\”,并告诉它是否有效。

You should also check if the namespace inside you ApiContext is ok. 您还应该检查ApiContext内部的命名空间是否正常。 :) :)

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

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