简体   繁体   中英

Paypal: API NET: cancel subscription

First, let me complain about Paypal integration. The .NET NuGet packages are partial or obsolete in particular for the subscription/recurring payment. And it's quite the same for the other languages. The documentation is incomplete and mixed between versions.

I integrated Stripe a few months ago and it was a walk in the park compared to this.

So back to the problem. I use the last .net package . And completed it with my custom requests to manage subscriptions .

I manage to create and read plans. Same for subscriptions. But now, I want to cancel the subscription and I've got his error The server does not support the request payload's media type.

here my code:

    public class SubscriptionCancelRequest : HttpRequest
    {
        public SubscriptionCancelRequest(string subscriptionId)
            : base("/v1/billing/subscriptions/{subscriptionId}/cancel", HttpMethod.Post)
        {
            try
            {
                this.Path = this.Path.Replace("{subscriptionId}", Uri.EscapeDataString(Convert.ToString(subscriptionId)));
            }
            catch (IOException) { }

            ContentType = "application/json";
        }
    } 

In my service:

var requestCancel = new SubscriptionCancelRequest(paypalSubId);
var responseCancel = await _paypalClient.Execute(requestCancel); //_paypalClient is PayPalHttpClient for pkg

Can you tell me what I'm doing wrong?

Thx.

EDIT I try:

        public SubscriptionCancelRequest(string subscriptionId)
            : base("/v1/billing/subscriptions/{subscriptionId}/cancel", HttpMethod.Post, typeof(void))

or change the contenttype without better result.

ContentType = "application/x-www-form-urlencoded";;

EDIT 2 I wrote a basic httpclient:

        public async Task TestCancel(string paypalSubId)
        {
            var tokenReponse = await _paypalClient.Execute(new AccessTokenRequest(Environment()));
            var token = tokenReponse.Result<AccessToken>();

            using (var client = new System.Net.Http.HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US"));

                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.Token}");
                var content = new { reason = "Not satisfied with the service" };
                var responseMessage = await client.PostAsJsonAsync($"https://api.sandbox.paypal.com/v1/billing/subscriptions/{paypalSubId}/cancel ", content);
            }
        }

With it, I've got a new error: 422 Unprocessable Entity . I tried to cancel an expired subscription. So I suppose, the current paypalclient don't manage all errors or doesn't support empty body post and I cannot cancel an expired subscription.

Solution

First, the PaypalClient doesn't manage all errors or forward them... or it doesn't support post request without body... Second, you cannot cancel an expired subscription.

So if you wrote your own client like I did in EDIT 2 you will manage to cancel active subscription.

Solution

First, the PaypalClient doesn't manage all errors or forward them... or it doesn't support post request without body...

Second, you cannot cancel an expired subscription.

So if you wrote your own client (sample in the main topic) you will manage to cancel active subscription.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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