简体   繁体   English

Paypal REST API SDK-在C#中激活计费方案

[英]Paypal REST API SDK - Activating a billing plan in C#

I have the following code to create a billing plan 我有以下代码来创建结算计划

 string iClientID = "xxxxxx";
        string iSecret = "yyyyyy";

        Dictionary<string, string> sdkConfig = new Dictionary<string, string>();
        sdkConfig.Add("mode", "sandbox");
        string accessToken = new OAuthTokenCredential(iClientID, iSecret, sdkConfig).GetAccessToken();
    APIContext apiContext = new APIContext(accessToken);
    apiContext.Config = sdkConfig;

    Plan xPlan = new Plan();
    xPlan.name = "Billing Plan OneA";
    xPlan.description = "Our first billing plan for testing";
    xPlan.type = "INFINITE";

    PaymentDefinition xPDef = new PaymentDefinition();
    xPDef.name = "Payment Def One";
    xPDef.type = "REGULAR";
    xPDef.frequency_interval = "1";
    xPDef.frequency = "MONTH";
    xPDef.cycles = "0";

    MerchantPreferences xPrefs = new MerchantPreferences();
    xPrefs.cancel_url = "http://learnoogle.com";
    xPrefs.return_url = "http://learnoogle.com?success";


    Currency xPCUrr = new Currency();
    xPCUrr.currency = "USD";
    xPCUrr.value = "25.00";

    xPDef.amount = xPCUrr;

    List<PaymentDefinition> xDeffs = new List<PaymentDefinition>();
    xDeffs.Add(xPDef);

    xPlan.payment_definitions = xDeffs;
    xPlan.merchant_preferences = xPrefs;
        Plan cPLan = xPlan.Create(apiContext);

And I attempt to set the plan active with the following code 我尝试使用以下代码将计划设置为活动状态

    Patch xPatch = new Patch();
    xPatch.op = "replace";
    xPatch.path = "state";
    xPatch.value = "ACTIVE";

    PatchRequest yPatch = new PatchRequest();
    yPatch.Add(xPatch);

    cPLan.Update(apiContext, yPatch);

However this gives me a (400) Bad Request. 但是,这给了我一个(400)错误的请求。 {"name":"BUSINESS_VALIDATION_ERROR","details":[{"field":"validation_error","issue":"Invalid Path provided."}],"message":"Validation Error.","information_link":" https://developer.paypal.com/webapps/developer/docs/api/#BUSINESS_VALIDATION_ERROR ","debug_id":"01f0eb9aaaea0"} {“名称”:“ BUSINESS_VALIDATION_ERROR”,“详细信息”:[{“字段”:“ validation_error”,“问题”:“提供的路径无效。”}],“消息”:“验证错误”,“,”信息链接“: “ https://developer.paypal.com/webapps/developer/docs/api/#BUSINESS_VALIDATION_ERROR ”,“ debug_id”:“ 01f0eb9aaaea0”}

Can anyone educate me on what I am doing wrong on this/ 谁能教我这个/我做错了什么/

When updating a plan, you'll need to set the Patch.value property to a new Plan object containing the fields you'd like to replace (setting the state to ACTIVE in this case). 更新计划时,需要将Patch.value属性设置为一个新的Plan对象,其中包含要替换的字段(在这种情况下,将state设置为ACTIVE )。 Also, you'll need to set the Patch.path property to just "/" . 另外,您需要将Patch.path属性设置为"/"

In your code, do the following: 在您的代码中,执行以下操作:

Patch xPatch = new Patch();
xPatch.op = "replace";
xPatch.path = "/";
xPatch.value = new Plan() { state = "ACTIVE" };

PatchRequest yPatch = new PatchRequest();
yPatch.Add(xPatch);

cPLan.Update(apiContext, yPatch);

It appears that the code has changed since the approved answer was submitted. 自从提交批准的答案以来,代码似乎已更改。 It should now be something like this: 现在应该是这样的:

var client = new PayPalHttpClient(environment);
JsonPatch<Plan> xPatch = new JsonPatch<Plan>();
xPatch.op = "replace";
xPatch.path = "/";
xPatch.value = new Plan() { state = "ACTIVE" };

PlanUpdateRequest<Plan> yPatch = new PlanUpdateRequest<Plan>(planId);
yPatch.RequestBody(new List<JsonPatch<Plan>>() { patch });

BraintreeHttp.HttpResponse response = client.Execute(yPatch).Result; // or await this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM