【发布时间】:2020-11-09 19:01:31
【问题描述】:
首先,让我抱怨一下 Paypal 集成。 .NET NuGet 包是部分或过时的,特别是对于订阅/定期付款。其他语言也是如此。 文档不完整,版本之间混杂。
几个月前我集成了 Stripe,与此相比,这是在公园里散步。
回到问题上来。我使用the last .net package。 并使用我的自定义请求来完成它以管理 subscriptions。
我设法制定和阅读计划。订阅也一样。但是现在,我想取消订阅,我得到了他的错误服务器不支持请求有效负载的媒体类型。
这是我的代码:
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";
}
}
为我服务:
var requestCancel = new SubscriptionCancelRequest(paypalSubId);
var responseCancel = await _paypalClient.Execute(requestCancel); //_paypalClient is PayPalHttpClient for pkg
你能告诉我我做错了什么吗?
谢谢。
编辑 我试试:
public SubscriptionCancelRequest(string subscriptionId)
: base("/v1/billing/subscriptions/{subscriptionId}/cancel", HttpMethod.Post, typeof(void))
或者在没有更好结果的情况下更改内容类型。
ContentType = "application/x-www-form-urlencoded";;
编辑 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);
}
}
有了它,我得到了一个新错误:422 Unprocessable Entity。我试图取消过期的订阅。 所以我想,当前的 paypalclient 不管理所有错误或不支持空正文帖子,我无法取消过期订阅。
解决方案
首先,PaypalClient 不会管理所有错误或转发它们... 或者它不支持没有正文的发布请求... 其次,您无法取消已过期的订阅。
因此,如果您像我在 EDIT 2 中那样编写自己的客户端,您将设法取消有效订阅。
【问题讨论】:
标签: .net paypal paypal-subscriptions