paypal / PayPal-PHP-SDK

PHP SDK for PayPal RESTful APIs
https://developer.paypal.com/docs/api/
Other
27 stars 100 forks source link

PayPal response code 400 #947

Closed nkuldip closed 7 years ago

nkuldip commented 7 years ago

I'm workin on PayPal Payments in PHP. Now i am using paypal recurring payment option all proccess don but last operation not done propery and payment not received.

Everything was fine until i got this error: Got Http response code 400 when accessing api.sandbox.paypal.com/v1/payments/paymen Here is code '<?php

namespace App\Http\Controllers;

use Auth; use App\User; use App\PaymentTransaction;

use App\Http\Requests; use Illuminate\Http\Request; use Validator; use URL; use Session; use Redirect; use Input; / All Paypal Details class / use PayPal\Rest\ApiContext; use PayPal\Auth\OAuthTokenCredential;

// use to process billing agreements use PayPal\Api\Agreement; use PayPal\Api\Payer; use PayPal\Api\Plan; use PayPal\Api\ShippingAddress;

// Used to process plans use PayPal\Api\ChargeModel; use PayPal\Api\Currency; use PayPal\Api\MerchantPreferences; use PayPal\Api\PaymentDefinition; use PayPal\Api\Patch; use PayPal\Api\PatchRequest; use PayPal\Common\PayPalModel; use PayPal\Exception\PayPalConnectionException;

class PaypalBillingController extends Controller { private $apiContext; private $client_id; private $secret; private $plan_id;

// Create a new instance with our paypal credentials
public function __construct()
{
    // Detect if we are running in live mode or sandbox
    //$this->plan_id = env('PAYPAL_SANDBOX_PLAN_ID', '');

    // Set the Paypal API Context/Credentials       

    $paypal_conf = \Config::get('paypal');
    $this->apiContext = new ApiContext(new OAuthTokenCredential($paypal_conf['client_id'], $paypal_conf['secret']));
    $this->apiContext->setConfig($paypal_conf['settings']);
}

public function create_plan(){

    // Create a new billing plan
    $plan = new Plan();
    $plan->setName('Premium membership')
      ->setDescription('Monthly Subscription to the App Name')
      ->setType('FIXED');

    // Set billing plan definitions
    $paymentDefinition = new PaymentDefinition();
    $paymentDefinition->setName('Regular Payments')
      ->setType('REGULAR')
      ->setFrequency('Month')
      ->setFrequencyInterval('1')
      ->setCycles('12')
      ->setAmount(new Currency(array('value' => 100, 'currency' => 'USD')));

    // Set merchant preferences
    $merchantPreferences = new MerchantPreferences();
    $merchantPreferences->setReturnUrl(URL::route('paypal.return'))
      ->setCancelUrl(URL::route('paypal.return'))
      ->setAutoBillAmount('yes')
      ->setInitialFailAmountAction('CONTINUE')
      ->setMaxFailAttempts('0')
      ->setSetupFee(new Currency(array('value' => 1, 'currency' => 'USD')));

    $plan->setPaymentDefinitions(array($paymentDefinition));
    $plan->setMerchantPreferences($merchantPreferences);

    //create the plan
    try {
        $createdPlan = $plan->create($this->apiContext);

        try {
            $patch = new Patch();
            $value = new PayPalModel('{"state":"ACTIVE"}');
            $patch->setOp('replace')
              ->setPath('/')
              ->setValue($value);
            $patchRequest = new PatchRequest();
            $patchRequest->addPatch($patch);
            $createdPlan->update($patchRequest, $this->apiContext);
            $plan = Plan::get($createdPlan->getId(), $this->apiContext);

            // Output plan id
            //echo 'Plan ID:' . $plan->getId();

            return redirect('subscribe/paypal/'.$plan->getId());

        } catch (\PayPal\Exception\PayPalConnectionException $ex) {
            echo $ex->getCode();
            echo $ex->getData();
            die($ex);
        } catch (Exception $ex) {
            die($ex);
        }
    } catch (\PayPal\Exception\PayPalConnectionException $ex) {
        echo $ex->getCode();
        echo $ex->getData();
        die($ex);
    } catch (Exception $ex) {
        die($ex);
    }

}

public function paypalRedirect($plan_id){
    // Create new agreement
    $agreement = new Agreement();
    $agreement->setName('App Name Monthly Subscription Agreement')
      ->setDescription('Basic Subscription')
      ->setStartDate(\Carbon\Carbon::now()->addMinutes(5)->toIso8601String());

    // Set plan id
    $plan = new Plan();
    $plan->setId($plan_id);
    $agreement->setPlan($plan);

    // Add payer type
    $payer = new Payer();
    $payer->setPaymentMethod('paypal');
    $agreement->setPayer($payer);

    try {
      // Create agreement
      $agreement = $agreement->create($this->apiContext);

      // Extract approval URL to redirect user
      $approvalUrl = $agreement->getApprovalLink();

      return redirect($approvalUrl);
    } catch (\PayPal\Exception\PayPalConnectionException $ex) {
      echo $ex->getCode();
      echo $ex->getData();
      die($ex);
    } catch (Exception $ex) {
      die($ex);
    }

}

public function paypalReturn(){

    echo "string";
    exit;
    //$token = $request->token;
    $agreement = new \PayPal\Api\Agreement();

    try {
        // Execute agreement
        $result = $agreement->execute($token, $this->apiContext);
       /* $user = Auth::user();
        $user->role = 'subscriber';
        $user->paypal = 1;
        if(isset($result->id)){
            $user->paypal_agreement_id = $result->id;
        }
        $user->save();*/

        echo 'New Subscriber Created and Billed';

    } catch (\PayPal\Exception\PayPalConnectionException $ex) {
        echo 'You have either cancelled the request or your session has expired';
    }
}

}'

Why this is happening? How can I fix that? Please help me. Thanks

xiaoleih41 commented 7 years ago

Did the buyer approve the the payment before you execute the payment?

nkuldip commented 7 years ago

test facilitator's Test Store suspended your automatic payments This message received in sandbox notification.

xiaoleih41 commented 7 years ago

Please provide your 'debugId" for us to do troubleshooting. Please help answer my previous question as well.