C# PayPal PaymentsException:远程服务器返回错误:(400)请求错误
我打算向我的服务用户出售一个订阅,我试图用c#在我的asp.net网络表单网站中设置paypal,但我遇到了一个未知的错误。我想知道我写的代码是否正确 代码:C# PayPal PaymentsException:远程服务器返回错误:(400)请求错误,c#,asp.net,paypal,C#,Asp.net,Paypal,我打算向我的服务用户出售一个订阅,我试图用c#在我的asp.net网络表单网站中设置paypal,但我遇到了一个未知的错误。我想知道我写的代码是否正确 代码: var apiContext=Configuration.GetAPIContext(); 字符串payerId=userId.ToString(); 如果(!string.IsNullOrEmpty(payerId)) { var itemList=new itemList() { items=新列表() { 新项目() { name=
var apiContext=Configuration.GetAPIContext();
字符串payerId=userId.ToString();
如果(!string.IsNullOrEmpty(payerId))
{
var itemList=new itemList()
{
items=新列表()
{
新项目()
{
name=“每周订阅”,
货币=“美元”,
price=“15”,
quantity=“1”,
sku=“sku”
}
}
};
var payer=new payer(){payment_method=“paypal”};
var baseURI=”http://localhost:1067/Pages/General/BackFromPayPal.aspx?";
var guid=Convert.ToString((new Random()).Next(100000));
var redirectUrl=baseURI+“id=“+item.PaidHistoryID;
var redirls=新的重定向URL()
{
取消\u url=redirectUrl+“&isPaid=false”,
return\u url=redirectUrl+“&isPaid=true”
};
var details=new details()
{
tax=“0”,
shipping=“0”,
小计=“15”
};
var金额=新金额()
{
货币=“美元”,
total=“15.00”,//总计必须等于运费、税金和小计之和。
细节=细节
};
var transactionList=新列表();
transactionList.Add(新事务()
{
description=“description”,
发票编号=通用。GetRandomInvoiceNumber(),
金额=金额,
项目列表=项目列表
});
var付款=新付款()
{
intent=“销售”,
付款人=付款人,
事务=事务列表,
重定向\u URL=redirls
};
尝试
{
var createdPayment=payment.Create(apiContext);
var links=createdPayment.links.GetEnumerator();
while(links.MoveNext())
{
var link=links.Current;
如果(link.rel.ToLower().Trim().Equals(“批准url”))
{
//this.flow.RecordRedirectUrl(“重定向到PayPal以批准付款…”,link.href);
}
}
添加(guid,createdPayment.id);
var paymentId=createdPayment.id;
var paymentExecution=new paymentExecution(){payer_id=payerId,transactions=createdPayment.transactions};
var executedPayment=createdPayment.Execute(apiContext,paymentExecution);
}
捕获(PayPalException例外)
{
如果(例如是ConnectionException)
{
Label11.Text=例如消息;
}
其他的
{
Label11.Text=ex.ToString();
}
}
您在web.config文件中设置了clientId和clientSecret吗?@ValNolav是的,我已经设置了PayPal SDK设置。我正在live中工作mode@Morteza你有什么解决办法吗?我在var createdPayment=payment.Create(apiContext)上也面临同样的问题;
您在web.config文件中设置了clientId和clientSecret吗?@ValNolav是的,我已经设置了PayPal SDK设置。我正在live中工作mode@Morteza你有什么解决办法吗?我在var createdPayment=payment.Create(apiContext);
var apiContext = Configuration.GetAPIContext();
string payerId = userId.ToString();
if (!string.IsNullOrEmpty(payerId))
{
var itemList = new ItemList()
{
items = new List<Item>()
{
new Item()
{
name = "Weekly Subscribe",
currency = "USD",
price = "15",
quantity = "1",
sku = "sku"
}
}
};
var payer = new Payer() { payment_method = "paypal" };
var baseURI = "http://localhost:1067/Pages/General/BackFromPayPal.aspx?";
var guid = Convert.ToString((new Random()).Next(100000));
var redirectUrl = baseURI + "id=" + item.PaidHistoryID;
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl + "&isPaid=false",
return_url = redirectUrl + "&isPaid=true"
};
var details = new Details()
{
tax = "0",
shipping = "0",
subtotal = "15"
};
var amount = new Amount()
{
currency = "USD",
total = "15.00", // Total must be equal to sum of shipping, tax and subtotal.
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "description",
invoice_number = Common.GetRandomInvoiceNumber(),
amount = amount,
item_list = itemList
});
var payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
try
{
var createdPayment = payment.Create(apiContext);
var links = createdPayment.links.GetEnumerator();
while (links.MoveNext())
{
var link = links.Current;
if (link.rel.ToLower().Trim().Equals("approval_url"))
{
//this.flow.RecordRedirectUrl("Redirect to PayPal to approve the payment...", link.href);
}
}
Session.Add(guid, createdPayment.id);
var paymentId = createdPayment.id;
var paymentExecution = new PaymentExecution() { payer_id = payerId, transactions = createdPayment.transactions };
var executedPayment = createdPayment.Execute(apiContext, paymentExecution);
}
catch (PayPalException ex)
{
if (ex is ConnectionException)
{
Label11.Text = ex.Message;
}
else
{
Label11.Text = ex.ToString();
}
}