مشكلة عدم التحويل الي صفحة البايبال

ايرور عند التحويل لصفحة البايبال PayPalConnectionException in PayPalHttpConnection.php line 154: Got Http response code 400 when accessing https://api...

ايرور عند التحويل لصفحة البايبال
PayPalConnectionException in PayPalHttpConnection.php line 154:
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.


namespace App\\Http\\Controllers;

use App\\Products;
use Illuminate\\Http\\Request;
use Illuminate\\Support\\Facades\\Redirect;
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\\PaymentExecution;
use PayPal\\Api\\RedirectUrls;
use PayPal\\Api\\Transaction;
use PayPal\\Auth\\OAuthTokenCredential;
use PayPal\\Rest\\ApiContext;

class ProductsController extends Controller
{
    protected $apiContext;

    protected function setContext(){

        $this->apiContext = new ApiContext(
            new OAuthTokenCredential(config(\'payment.accounts.client_id\') , config(\'payment.accounts.secret\'))
        );

        $this->apiContext->setConfig(config(\'payment.setting\'));

    }


    public function index()
    {
        $products = Products::get();
        return view(\'products\' , compact(\'products\'));
    }

    public function pay(Request $request)
    {
        $selectProducts = $request->product;
        $totalPrice = 0;

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

        $items_List = [];

        if(count($selectProducts) > 0)
        {
            $this->setContext();
            foreach ($selectProducts as $select)
            {
                $selectPro = Products::findOrFail($select);

                if ($selectPro)
                {
                    $item = new Item();
                    $item->setName($selectPro->name)
                        ->setCurrency(\'USD\')
                        ->setQuantity(1)
                        ->setSku($selectPro->id) // Similar to `item_number` in Classic API
                        ->setPrice($selectPro->price);

                    $items_List[] = $item; // push new select item in item list
                    $totalPrice = $totalPrice + $selectPro->price;
                }
            }
        }else{
            return Redirect::back();
        }
        $itemList = new ItemList();
        $itemList->setItems($items_List);

        $details = new Details();
        $details->setShipping(0)
            ->setTax(0)
            ->setSubtotal($totalPrice);

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

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

        $baseUrl = url(\'/\');
        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl($baseUrl.\"/success/true\")
            ->setCancelUrl($baseUrl.\"/success/false\");

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

        try
        {
            $payment->create($this->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();
        session([\'totalPrice\' => $totalPrice]);
        return Redirect::away($approvalUrl);

        // dd($itemList);
        //  return view(\'products\' , compact(\'products\'));
    }


    public function makePayment($status , Request $request)
    {
        if ($status == \'true\'){

            if (
                isset($request->paymentId )&& $request->paymentId != \'\' &&
                isset($request->token )&& $request->token != \'\' &&
                isset($request->PayerID )&& $request->PayerID != \'\'
            ){

                $this->setContext();
                $totalPrice = session(\'totalPrice\');
                session()->forget(\'totalPrice\');
                $paymentId = $request->paymentId;
                $payment = Payment::get($paymentId, $this->apiContext);

                $execution = new PaymentExecution();
                $execution->setPayerId($request->PayerID);

                $transaction = new Transaction();
                $amount = new Amount();
                $details = new Details();
                $details->setShipping(0)
                    ->setTax(0)
                    ->setSubtotal($totalPrice);
                $amount->setCurrency(\'USD\');
                $amount->setTotal($totalPrice);
                $amount->setDetails($details);
                $transaction->setAmount($amount);
                $execution->addTransaction($transaction);

                try {
                    $result = $payment->execute($execution, $this->apiContext);
                    try {
                        $payment = Payment::get($paymentId, $this->apiContext);
                    } catch (Exception $ex) {

                        // ResultPrinter::printError(\"Get Payment\", \"Payment\", null, null, $ex);
                        exit(1);
                    }
                } catch (Exception $ex) {
                    //  ResultPrinter::printError(\"Executed Payment\", \"Payment\", null, null, $ex);
                    exit(1);
                }

                // ResultPrinter::printResult(\"Get Payment\", \"Payment\", $payment->getId(), null, $payment);

                dd($payment);
            }
        }

        return Redirect::back();

    }
}

3 التعليقات


  • Mohmd Zamli
    Mohmd Zamli 2017-09-21 15:57:02

    تم التحويل لمرة واحدة وتم خصم المبلغ وبعدها يظهر الايرور بالرغم من عدم تعديل الكود

  • Abdel Aziz Hassan
    Abdel Aziz Hassan 2017-09-21 15:57:02

    المشكلة مش في الكود المشكلة ممكن تكون في البينات الخاصة بباي بال في الاتصال بتاعك او حتي في الابلكش اللي انت عملتى
    علي باي بال راجع كل الخطوات الاول لانه هو بيبقلك في مشكلة في accessing

     Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment. 
  • Mohmd Zamli
    Mohmd Zamli 2017-09-21 15:57:02

    بيانات البايبال صحيحة لانه اشتغل معي مرة وخصم المبلغ وعرض بيانات العملية عن طريق ميثود dd()

اضف تعليق


يجب ان يكون لديك حساب في الموقع تسجيل الدخول او تسجيل عضوية جديدة

خدمات ويب الفريق