C# ASP.NET MVC C中带有计费计划的PayPal Rest API发布问题
2014年7月,PayPal将计费计划添加到Rest API中,并通过使用cURL(仅截至2014年8月5日)获得了成功。他们也提到了这一点。问题是,我目前正在使用PayPal SDK for.NET,而计费计划此时不属于此SDK的一部分,因此我必须手动创建这些发布连接 更新:该问题源于贝宝关于账单计划主题的博客文章中的错误示例。他们的API示例显示了金额对象中包含的值和货币等内容,而博客文章中没有,不幸的是,这正是我开始讨论的内容。我已经为其他需要在C#中根据PayPal REST API创建计费计划的人更新了下面的JSON示例。 我收到的错误是一个“远程服务器返回了一个错误:(400)错误请求。”在使用以下代码(如下)发布时。我正在使用他们的数据发布,并在飞行中提取我的客户Id和机密。我获得了ApiContext和我的访问令牌,因此在推送数据之前我已经过身份验证 Fiddler2返回以下(原始数据):C# ASP.NET MVC C中带有计费计划的PayPal Rest API发布问题,c#,asp.net-mvc,rest,paypal,C#,Asp.net Mvc,Rest,Paypal,2014年7月,PayPal将计费计划添加到Rest API中,并通过使用cURL(仅截至2014年8月5日)获得了成功。他们也提到了这一点。问题是,我目前正在使用PayPal SDK for.NET,而计费计划此时不属于此SDK的一部分,因此我必须手动创建这些发布连接 更新:该问题源于贝宝关于账单计划主题的博客文章中的错误示例。他们的API示例显示了金额对象中包含的值和货币等内容,而博客文章中没有,不幸的是,这正是我开始讨论的内容。我已经为其他需要在C#中根据PayPal REST API创建
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
PROXY_SERVER_INFO: host=slcsbjava4.slc.paypal.com;threadId=21857
Paypal-Debug-Id: 9435d30090e68
SERVER_INFO: paymentsplatformserv:v1.payments.billing-plans&CalThreadId=26685&TopLevelTxnStartTime=147a6db17e2&Host=slcsbjm2.slc.paypal.com&pid=14241
Content-Language: *
Date: Tue, 05 Aug 2014 15:48:02 GMT
Connection: close
Content-Type: application/json
Content-Length: 213
Connection: close
{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"9435d30090e68"}
Json(由于博客帖子值不正确而更新)
--这将通过Fiddler2 JSON解析
{
"description" : "Template Creation",
"merchant_preferences" : {
"auto_bill_amount" : "yes",
"cancel_url" : "http://www.fruitofthemonth.com/cancel",
"return_url" : "http://www.fruitofthemonth.com/complete",
"setup_fee" : {
"currency" : "USD",
"value" : "0"
}
},
"name" : "Fast Speed Plan",
"payment_definitions" : [ {
"amount" : {
"currency" : "USD",
"value" : "100"
},
"charge_models" : [ {
"amount" : {
"currency" : "USD",
"value" : "8"
},
"type" : "SHIPPING"
},
{
"amount" : {
"currency" : "USD",
"value" : "4"
},
"type" : "TAX"
}
],
"cycles" : "12",
"frequency" : "MONTH",
"frequency_interval" : "2",
"name" : "Standard Package",
"type" : "REGULAR"
} ],
"type" : "fixed"
}
C#(代码片段-下面是公共类中的代码)
publicstaticdictionary GetConfiguration()
{
Dictionary configurationMap=新字典();
添加(“模式”、“沙盒”);
返回配置图;
}
私有字符串访问令牌
{
得到
{
字符串令牌=新的OAuthTokenCredential
(
ConfigurationManager.AppSettings[“PayPalClientId”],
ConfigurationManager.AppSettings[“PayPalClientSecret”],
GetConfiguration()
).GetAccessToken();
返回令牌;
}
}
专用apicontextapi
{
得到
{
APIContext上下文=新的APIContext(AccessToken);
context.Config=GetConfiguration();
返回上下文;
}
}
受保护的无效姿势()
{
var-context=Api;
尝试
{
var httpWebRequest=(httpWebRequest)WebRequest.Create(“https://api.sandbox.paypal.com/v1/payments/billing-plans");
httpWebRequest.ContentType=“应用程序/json”;
httpWebRequest.Method=“POST”;
httpWebRequest.Headers.Add(“授权”,context.AccessToken);
数据=“{名称:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\“}}]},名称:\“第一个月免费试用\”,类型:\“试用\”,频率:\“月\”,频率间隔:\“1\”,值:\“50\”,货币:\“USD\”},周期:\“1\”,收费模式:【类型:\“发货\”,值:\“8\”,货币:\“USD\”},类型:\“税\”,值:\“4\”,货币:\“USD\”}],值:\,货币:\“USD\”,返回url:”http://www.fruitofthemonth.com/complete\",取消\u url:\”http://www.fruitofthemonth.com/cancel\“,自动票据金额:\“是”,初始金额\失败\操作:\“继续”,最大失败\尝试次数:\“0\”}”;
使用(var writer=newstreamwriter(httpWebRequest.GetRequestStream()))
{
var serializer=新的JavaScriptSerializer();
string jsonText=serializer.Serialize(数据);
writer.Write(jsonText);
}
//在此处创建响应时,代码失败
var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
}
捕获(例外情况除外)
{
异常(this.GetType().Name,String.Format(“错误:{0}”,“无法发布url”)),例如;
}
上面的代码经过简化,只是为了测试在C#中发布/创建账单计划。如果任何人有一个可以分享的工作示例,请分享
我现在被卡住了,因为我不知道根据他们的API发布PayPal账单计划的神奇过程是什么。谢谢你的指导。看看你的代码,你的
数据值中存储的JSON似乎格式不正确。例如,值和货币需要在金额下payment\u definitions
中的容器,但它似乎丢失了。正如您所评论的,我正在逐行检查。搞砸的是paypal开发人员的博客文章(见图)。我已经更正了JSON并修改了请求,因为没有更多的发布错误。感谢您的双重检查!
public static Dictionary<string, string> GetConfiguration()
{
Dictionary<string, string> configurationMap = new Dictionary<string, string>();
configurationMap.Add("mode", "sandbox");
return configurationMap;
}
private string AccessToken
{
get
{
string token = new OAuthTokenCredential
(
ConfigurationManager.AppSettings["PayPalClientId"],
ConfigurationManager.AppSettings["PayPalClientSecret"],
GetConfiguration()
).GetAccessToken();
return token;
}
}
private APIContext Api
{
get
{
APIContext context = new APIContext(AccessToken);
context.Config = GetConfiguration();
return context;
}
}
protected void PostUrl()
{
var context = Api;
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/payments/billing-plans");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", context.AccessToken);
data = "{name:\"Fruit of the Month\",description:\"10 Pound Box of Fruit\",type:\"fixed\",payment_definitions:[name:\"Standard Package\",type:\"REGULAR\",frequency:\"MONTH\",frequency_interval:\"2\",value:\"50\",currency:\"USD\"},cycles:\"12\",charge_models:[type:\"SHIPPING\",value:\"8\",currency:\"USD\"}},type:\"TAX\",value:\"4\",currency:\"USD\"}}]},name:\"First Month Free Trial\",type:\"TRIAL\",frequency:\"MONTH\",frequency_interval:\"1\",value:\"50\",currency:\"USD\"},cycles:\"1\",charge_models:[type:\"SHIPPING\",value:\"8\",currency:\"USD\"}},type:\"TAX\",value:\"4\",currency:\"USD\"}}]}],value:\"1\",currency:\"USD\"},return_url:\"http://www.fruitofthemonth.com/complete\",cancel_url:\"http://www.fruitofthemonth.com/cancel\",autobill_amount:\"YES\",initial_amount_fail_action:\"CONTINUE\",max_fail_attempts:\"0\"}}";
using (var writer = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var serializer = new JavaScriptSerializer();
string jsonText = serializer.Serialize(data);
writer.Write(jsonText);
}
// The code fails when creating the Response here
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception ex)
{
Logger.Exception(this.GetType().Name, String.Format("Error: {0}", "Unable to post url."), ex);
}