带有PHP和JS SDK的PayPal订阅API-RESOURCE_NOT_FOUND错误

时间:2020-01-22 14:20:55

标签: php paypal

我正在使用JS SDK进行PayPal订阅过程。首先,我已经通过PHP SDK成功创建了计划并激活了它。然后,我试图通过该计划ID来实现JS SDK的订阅过程。但是有些却不起作用。如果有人可以帮助我,那就太好了。

这是我的代码:

<div id="paypal-button-container"></div>

<script>
    paypal.Buttons({
        env: 'sandbox',
        createSubscription: function (data, actions) {

            return actions.subscription.create({

                'plan_id': 'P-15T61220BN038602UZQLZ2UI',

            });
        }
        ,
        onApprove: function (data, actions) {

            alert('You have successfully created subscription ' + data.subscriptionID);
        }
    }).render('#paypal-button-container');
</script>

这是错误:

未捕获的错误:创建订阅Api响应错误:

{
    "name": "RESOURCE_NOT_FOUND",
    "message": "The specified resource does not exist.",
    "debug_id": "f2628efbed424",
    "details": [
        {
            "issue": "INVALID_RESOURCE_ID",
            "description": "Requested resource ID was not found."
        }
    ],
    "links": [
        {
            "href": "https://developer.paypal.com/docs/api/v1/billing/subscriptions#RESOURCE_NOT_FOUND",
            "rel": "information_link",
            "method": "GET"
        }
    ]
}

enter image description here

我的计划API代码:

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;

require __DIR__ . '/vendor/autoload.php';
require "app/config.php";

$apiContext = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential(
        'AbMJNJRtMOefeVPE9UM50...........', // ClientID
        'EN8kxO-TjQb_Lr..........'     // ClientSecret
        )
);


//$apiContext = new \PayPal\Rest\ApiContext(new \PayPal\Auth\OAuthTokenCredential('AbMJNJRtMOefeV........', 'EN8kxO-TjQb_LrcpSdYGRoXz5LX6HmJyQ4lC---------'));
//if (! empty($_POST["subscribe"])) {
//    require_once "./Service/createPHPTutorialSubscriptionPlan.php";
//}
// Create a new billing plan
$plan = new Plan();
$plan->setName('Family plan monthly fathomreads')
        ->setDescription('Famaliy plan monthly')
        ->setType('INFINITE');

// Set billing plan definitions
$paymentDefinition = new PaymentDefinition();
$paymentDefinition->setName('Regular Payments')
        ->setType('REGULAR')
        ->setFrequency('Month')
        ->setFrequencyInterval('2')
        ->setCycles('0')
        ->setAmount(new Currency(array('value' => 6.60, '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/fathomreads/paypalsuccess.php')
        ->setCancelUrl('http://localhost/fathomreads/paypalcancel.php')
        ->setAutoBillAmount('yes')
        ->setInitialFailAmountAction('CONTINUE')
        ->setMaxFailAttempts('0')
        ->setSetupFee(new Currency(array('value' => 0, '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

        dbQuery("UPDATE general_settings SET paypal_subscription_planid = '" . $plan->getId() . "' WHERE id = 1 ");



        echo "Plan created Successfuly. Plan Id : " . $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);
}

响应:

计划创建成功。计划编号:P-15T61220BN038602UZQLZ2UI

PayPal\Api\Plan Object
(
    [_propMap:PayPal\Common\PayPalModel:private] => Array
        (
            [id] => P-15T61220BN038602UZQLZ2UI
            [state] => ACTIVE
            [name] => Family plan monthly fathomreads
            [description] => Famaliy plan monthly
            [type] => INFINITE
            [payment_definitions] => Array
                (
                    [0] => PayPal\Api\PaymentDefinition Object
                        (
                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                (
                                    [id] => PD-6S8510199C217192C2GF6Z7A
                                    [name] => Regular Payments
                                    [type] => REGULAR
                                    [frequency] => Month
                                    [amount] => PayPal\Api\Currency Object
                                        (
                                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                                (
                                                    [currency] => USD
                                                    [value] => 6.60
                                                )

                                        )

                                    [cycles] => 0
                                    [frequency_interval] => 1
                                )

                        )

                )

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

                                )

                            [max_fail_attempts] => 0
                            [return_url] => http://localhost/fathomreads/paypalsuccess.php
                            [cancel_url] => http://localhost/fathomreads/paypalcancel.php
                            [auto_bill_amount] => YES
                            [initial_fail_amount_action] => CONTINUE
                        )

                )

            [create_time] => 2020-01-23T08:36:45.948Z
            [update_time] => 2020-01-23T08:36:47.640Z
            [links] => Array
                (
                    [0] => PayPal\Api\Links Object
                        (
                            [_propMap:PayPal\Common\PayPalModel:private] => Array
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/payments/billing-plans/P-15T61220BN038602UZQLZ2UI
                                    [rel] => self
                                    [method] => GET
                                )

                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

旧的paypal / PayPal-PHP-SDK不支持新的Subscriptions API。它为较旧的内容使用了计费方案,这与您尝试执行的操作不兼容。

新的paypal / Checkout-PHP-SDK还不支持新的Subscriptions API。

因此,基本上,要使用PHP的Subscriptions API,当前需要实现直接HTTPS REST API调用(无SDK)以创建计划。

您可以使用命令行curl进行测试,或者先进行邮递员调用以确认其正常工作,然后编写必要的PHP函数以使用oauth令牌对PayPal进行自己的HTTPS调用。