当前位置: 首页 > 知识库问答 >
问题:

PayPal REST API-计费协议不返回令牌

南宫保臣
2023-03-14

我正在尝试使用REST API创建一个PayPal的定期订阅(信用卡):

创建计费计划-https://developer . paypal . com/docs/API/quick start/create-Billing-plan/

创建计费协议 - https://developer.paypal.com/docs/api/quickstart/create-billing-agreement/

我正在测试和玩弄API,所以我创建了两个文件计划.php和协议.php。

< b>plan.php是我生成计划ID并在< b>agreement.php中静态传递它的地方。

计划。php

<?php

require 'vendor/autoload.php';

$apiContext = new \PayPal\Rest\ApiContext(
  new \PayPal\Auth\OAuthTokenCredential(
    'Application-ID',    
    'Secret-ID'  
  )
);

use PayPal\Api\ChargeModel;
use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;
use PayPal\Api\Patch;
use PayPal\Api\PatchRequest;
use PayPal\Common\PayPalModel;

use PayPal\Api\Agreement;
use PayPal\Api\Payer;
use PayPal\Api\ShippingAddress;
use PayPal\Api\CreditCard;
use PayPal\Api\FundingInstrument;

    $plan = new Plan();
    $plan->setName('T-Shirt of the Month Club Plan')
      ->setDescription('Template creation.')
      ->setType('fixed');

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

// Set charge models
$chargeModel = new ChargeModel();
$chargeModel->setType('SHIPPING')
  ->setAmount(new Currency(array('value' => 10, 'currency' => 'USD')));
$paymentDefinition->setChargeModels(array($chargeModel));

// Set merchant preferences
$merchantPreferences = new MerchantPreferences();
$merchantPreferences->setReturnUrl('http://localhost:3000/processagreement')
  ->setCancelUrl('http://localhost:3000/cancel')
  ->setAutoBillAmount('yes')
  ->setInitialFailAmountAction('CONTINUE')
  ->setMaxFailAttempts('0')
  ->setSetupFee(new Currency(array('value' => 1, 'currency' => 'USD')));

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




//create plan
try {
  $createdPlan = $plan->create($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, $apiContext);
    $plan = Plan::get($createdPlan->getId(), $apiContext);

    // Output plan id
    echo $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);
}

agreement.php

<代码>

// Autoload SDK package for composer based installations require 'vendor/autoload.php'; $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( 'Application-ID', 'Secret-ID' ) ); use PayPal\Api\ChargeModel; use PayPal\Api\Currency; use PayPal\Api\MerchantPreferences; use PayPal\Api\PaymentDefinition; use PayPal\Api\Plan; use PayPal\Api\Patch; use PayPal\Api\PatchRequest; use PayPal\Common\PayPalModel; use PayPal\Api\Agreement; use PayPal\Api\Payer; use PayPal\Api\ShippingAddress; use PayPal\Api\CreditCard; use PayPal\Api\FundingInstrument; // Create new agreement // $agreement = new Agreement(); // $agreement->setName('Base Agreement') // ->setDescription('Basic Agreement') // ->setStartDate('2017-02-17T9:45:04Z'); // // Set plan id // $plan = new Plan(); // $plan->setId('P-1CD306827C2019339JKC6JDY'); // $agreement->setPlan($plan); // // Add payer type // $payer = new Payer(); // $payer->setPaymentMethod('paypal'); // $agreement->setPayer($payer); // // Adding shipping details // $shippingAddress = new ShippingAddress(); // $shippingAddress->setLine1('111 First Street') // ->setCity('Saratoga') // ->setState('CA') // ->setPostalCode('95070') // ->setCountryCode('US'); // $agreement->setShippingAddress($shippingAddress); //create new agreement $agreement = new Agreement(); $agreement->setName('Base Agreement') ->setDescription('Basic Agreement') ->setStartDate('2017-02-17T9:45:04Z'); // Set plan id $plan = new Plan(); $plan->setId('P-1CD306827C2019339JKC6JDY'); $agreement->setPlan($plan); // Create credit card object and set funding instrument $card = new CreditCard(); $card->setType("visa") ->setNumber("4250448816997456") ->setExpireMonth("06") ->setExpireYear("2018") ->setCvv2("012") ->setFirstName("Joe") ->setLastName("Shopper"); $fi = new FundingInstrument(); $fi->setCreditCard($card); // Set payer to process credit card $payer = new Payer(); $payer->setPaymentMethod("credit_card") ->setFundingInstruments(array($fi)); $agreement->setPayer($payer); // Adding shipping details $shippingAddress = new ShippingAddress(); $shippingAddress->setLine1('111 First Street') ->setCity('Saratoga') ->setState('CA') ->setPostalCode('95070') ->setCountryCode('US'); $agreement->setShippingAddress($shippingAddress); $agreement = $agreement->create($apiContext); // print_r($agreement); exit(); if (isset($_GET['success']) && $_GET['success'] == 'true') { $token = $_GET['token']; $agreement = new \PayPal\Api\Agreement(); try { // Execute agreement $agreement->execute($token, $apiContext); } catch (PayPal\Exception\PayPalConnectionException $ex) { echo $ex->getCode(); echo $ex->getData(); die($ex); } catch (Exception $ex) { die($ex); } } else { echo "user canceled agreement"; } 

但是,它没有返回令牌,并且未设置成功变量。我已经打印了$agreement变量,下面是响应:

PayPal\Api\Agreement Object
(
    [_propMap:PayPal\Common\PayPalModel:private] => Array
        (
            [name] => Base Agreement
            [description] => Basic Agreement
            [start_date] => 2017-02-17T17:45:04Z
            [plan] => PayPal\Api\Plan Object
                (
                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                        (
                            [payment_definitions] => Array
                                (
                                    [0] => PayPal\Api\PaymentDefinition Object
                                        (
                                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                (
                                                    [type] => REGULAR
                                                    [frequency] => Month
                                                    [amount] => PayPal\Api\Currency Object
                                                        (
                                                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                (
                                                                    [value] => 100.00
                                                                )

                                                        )

                                                    [cycles] => 12
                                                    [charge_models] => Array
                                                        (
                                                            [0] => PayPal\Api\ChargeModel Object
                                                                (
                                                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                        (
                                                                            [type] => TAX
                                                                            [amount] => PayPal\Api\Currency Object
                                                                                (
                                                                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                                        (
                                                                                            [value] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                            [1] => PayPal\Api\ChargeModel Object
                                                                (
                                                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                        (
                                                                            [type] => SHIPPING
                                                                            [amount] => PayPal\Api\Currency Object
                                                                                (
                                                                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                                        (
                                                                                            [value] => 10.00
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                    [frequency_interval] => 2
                                                )

                                        )

                                )

                            [merchant_preferences] => PayPal\Api\MerchantPreferences Object
                                (
                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                        (
                                            [setup_fee] => PayPal\Api\Currency Object
                                                (
                                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                        (
                                                            [value] => 1.00
                                                        )

                                                )

                                            [max_fail_attempts] => 0
                                            [auto_bill_amount] => YES
                                        )

                                )

                            [links] => Array
                                (
                                )

                            [currency_code] => USD
                        )

                )

            [payer] => PayPal\Api\Payer Object
                (
                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                        (
                            [payment_method] => credit_card
                            [payer_info] => PayPal\Api\PayerInfo Object
                                (
                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                        (
                                        )

                                )

                            [funding_instruments] => Array
                                (
                                    [0] => PayPal\Api\FundingInstrument Object
                                        (
                                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                (
                                                    [credit_card] => PayPal\Api\CreditCard Object
                                                        (
                                                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                                (
                                                                    [type] => visa
                                                                    [number] => 7456
                                                                    [expire_month] => 06
                                                                    [expire_year] => 2018
                                                                    [first_name] => Edford Patrick
                                                                    [last_name] => Bedia
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [shipping_address] => PayPal\Api\ShippingAddress Object
                (
                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                        (
                            [line1] => 111 First Street
                            [city] => Saratoga
                            [state] => CA
                            [postal_code] => 95070
                            [country_code] => US
                        )

                )

            [id] => I-9107LMJ0351R
            [state] => Active
            [links] => Array
                (
                    [0] => PayPal\Api\Links Object
                        (
                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-9107LMJ0351R
                                    [rel] => self
                                    [method] => GET
                                )

                        )

                )

            [agreement_details] => PayPal\Api\AgreementDetails Object
                (
                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                        (
                            [outstanding_balance] => PayPal\Api\Currency Object
                                (
                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                        (
                                            [value] => 0.00
                                        )

                                )

                            [cycles_remaining] => 12
                            [cycles_completed] => 0
                            [next_billing_date] => 2017-02-17T10:00:00Z
                            [last_payment_date] => 2017-02-17T05:46:56Z
                            [last_payment_amount] => PayPal\Api\Currency Object
                                (
                                    [_propMap:PayPal\Common\PayPalModel:private] => Array
                                        (
                                            [value] => 1.00
                                        )

                                )

                            [final_payment_date] => 2018-12-17T10:00:00Z
                            [failed_payment_count] => 0
                        )

                )

        )

)

任何想法为什么我无法获得令牌?

提前感谢。

共有1个答案

武睿
2023-03-14

这是您的解决方案

1)您需要在创建计划时Execute.php退货URL。

2)您将该计划ID传递给计费协议

3)你的agreement.phpprint_r($协议-

4)当买方批准协议时,它将重定向到Execute.php,这是创建计划时设置的返回Url。

在execute.php,你将得到象征性的东西和真正的成功。

您在协议创建后添加了执行协议代码。这就是为什么设置不成功,您可以在买方批准后通过ID执行计费协议。查看下面的链接。

https://developer.paypal.com/docs/api/payments.billing-agreements

所以现在你应该从agreement.php删除执行代码并添加execute.php

<?php

// #Execute Agreement
// This is the second part of CreateAgreement Sample.
// Use this call to execute an agreement after the buyer approves it
require __DIR__ . '/../bootstrap.php';

// ## Approval Status
// Determine if the user accepted or denied the request
if (isset($_GET['success']) && $_GET['success'] == 'true') {
    $token = $_GET['token'];
    $agreement = new \PayPal\Api\Agreement();
    try {
        // ## Execute Agreement
        // Execute the agreement by passing in the token
        $agreement->execute($token, $apiContext);
    } catch (Exception $ex) {
        // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
        ResultPrinter::printError("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $ex);
        exit(1);
    }

    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    ResultPrinter::printResult("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $agreement);

    // ## Get Agreement
    // Make a get call to retrieve the executed agreement details
    try {
        $agreement = \PayPal\Api\Agreement::get($agreement->getId(), $apiContext);
    } catch (Exception $ex) {
        // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
        ResultPrinter::printError("Get Agreement", "Agreement", null, null, $ex);
        exit(1);
    }

    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    ResultPrinter::printResult("Get Agreement", "Agreement", $agreement->getId(), null, $agreement);
} else {
    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    ResultPrinter::printResult("User Cancelled the Approval", null);
}
 类似资料:
  • 因此,我正在将Paypal Rest API与我的django站点集成,以便引入基于订阅的计费,到目前为止,一切都很顺利。 我有一个用于计费计划,计费协议,交易历史记录以及创建和激活计费协议等的所有内容的界面。 但是,如果计费协议被取消或为计费协议付款,我需要一种方式来通知我,所以我认为webhooks是解决这个问题的方法,但是我不能100%确定webhooks适用于计费协议? 任何人都可以建议一

  • 我已经从这里下载了Paypal的SDK:https://github.com/paypal/rest-api-sdk-php 我正在使用Paypal Payments Pro,并希望添加计费计划和计费协议。 浏览 SDK,尽管我没有找到允许我创建计费计划或计费协议的示例或对象。当我寻找如何创建计费计划或协议的示例时,他们似乎解释了如何在没有SDK的情况下从头开始执行此操作。如果计费计划/协议尚不存

  • 问题内容: 我有一个协议P,它返回对象的副本: 和一个实现P的类C: 但是,是否在出现以下错误时放入返回值: 无法将类型“ C”的返回表达式转换为类型“ Self” 我也试着回来。 这导致以下错误: 非最终类“ C”中的方法“ copy()”必须返回以符合协议“ P” 除了我以ie做前缀的情况外,没有任何作用: 但是,如果我想对C进行子类化,那么将无济于事。有没有办法解决? 问题答案: 问题是您要

  • 我正在使用PayPal-PHP-SDK创建计费协议 使用PayPal帐户付款是可行的,但我希望买家在没有帐户的情况下付款(只是一张卡) 有3个订阅计划: 如果我选择订阅并单击我的PayPal按钮,这将调用计费协议创建以生成批准链接,并重定向页面 但是我没有选择用卡支付。 我的问题是: 我如何生成一个按钮,直接用卡支付这个账单协议,而不使用PayPal账户?

  • 问题内容: 当我尝试在http://golang.org/pkg/net/http/#example_Get上运行此示例时,它返回以下错误 2009/11/10 23:00:00获取http://www.google.com/robots.txt:拨打tcp:协议不可用 知道为什么吗? 问题答案: Go Playground不允许HTTP请求。这与代码无关。这是操场强制执行的安全预防措施。

  • cmf_get_domain() 功能 返回带协议的域名 参数 无 返回 string