简体   繁体   English

Paypal:API NET:取消订阅

[英]Paypal: API NET: cancel subscription

First, let me complain about Paypal integration.首先,让我抱怨一下 Paypal 集成。 The .NET NuGet packages are partial or obsolete in particular for the subscription/recurring payment. .NET NuGet 包是部分或过时的,特别是对于订阅/定期付款。 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.几个月前我集成了 Stripe,与此相比,这是在公园里散步。

So back to the problem.所以回到问题上来。 I use the last .net package .我使用最后一个 .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:编辑 2我写了一个基本的 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 .有了它,我得到了一个新错误: 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.所以我想,当前的 paypalclient 不管理所有错误或不支持空正文帖子,我无法取消过期订阅。

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.首先,PaypalClient 不管理所有错误或转发它们......或者它不支持没有正文的发布请求......其次,您不能取消过期的订阅。

So if you wrote your own client like I did in EDIT 2 you will manage to cancel active subscription.因此,如果您像我在EDIT 2中那样编写自己的客户端,您将设法取消有效订阅。

Solution解决方案

First, the PaypalClient doesn't manage all errors or forward them... or it doesn't support post request without body...首先,PaypalClient 不管理所有错误或转发它们......或者它不支持没有正文的发布请求......

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.因此,如果您编写了自己的客户端(主题中的示例),您将设法取消活动订阅。

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

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