PayPal响应代码400

时间:2017-10-06 11:10:29

标签: php laravel paypal

我正在使用PHP进行PayPal付款。现在我使用paypal定期付款选项所有进程不但最后一次操作没有完成propery和付款没有收到。

一切都很好,直到我收到此错误:http://prntscr.com/gtz4xx

以下是代码

public function create_plan(){

    // Create a new billing plan
    $plan = new Plan();
    $plan->setName('App Name Monthly Billing')
      ->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(Request $request){

    $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';
    }
}

为什么会这样?我该如何解决这个问题?

请帮帮我。 谢谢

0 个答案:

没有答案