我参考此tutorial来为我的网站设置基本的PayPal付款系统。点击“立即购买”按钮后,我将重定向到PayPal付款页面进行付款。
付款后,我被重定向回“ CompletePayment.aspx”页面以确认我的付款。但是,当我单击“确认购买”按钮时,它给我一个错误400错误的请求(如下所示)。
我在我的web.config中包含了客户端ID和密码。
这是我的代码:
(ConfirmOrder.aspx)
List<UserShoppingCartDAO> scList = UserShoppingCartDAO.GetCartOfUser(uID);
Product p = Product.getProduct(scList[0].ProductID.ToString());
decimal deliveryCharges = p.price * 0.05m;
string deliveryChargesStr = deliveryCharges.ToString();
decimal postagePackingCost = 10m;
decimal examPaperPrice = p.price;
int quantityOfExamPapers = scList[0].ProductQuantity;
decimal subtotal = (quantityOfExamPapers * examPaperPrice);
decimal total = subtotal + postagePackingCost;
//Authenticate with paypal
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
//Get APIContent Object
var apiContext = new APIContext(accessToken);
var examPaperItem = new Item();
examPaperItem.name = "Past Exam Paper";
examPaperItem.currency = "SGD";
examPaperItem.price = examPaperPrice.ToString();
examPaperItem.sku = "PEPCO5027m15";
examPaperItem.quantity = quantityOfExamPapers.ToString();
var transactionDetails = new Details();
transactionDetails.tax = "0";
transactionDetails.shipping = postagePackingCost.ToString();
transactionDetails.subtotal = subtotal.ToString("0.00");
var transactionAmount = new Amount();
transactionAmount.currency = "SGD";
transactionAmount.total = total.ToString("0.00");
transactionAmount.details = transactionDetails;
var transaction = new Transaction();
transaction.description = "Your order of Past Exam Papers";
transaction.invoice_number = Guid.NewGuid().ToString();
// ^ ID of record storing the order
transaction.amount = transactionAmount;
transaction.item_list = new ItemList
{
items = new List<Item> { examPaperItem }
};
var payer = new Payer();
payer.payment_method = "paypal";
var redirectUrls = new RedirectUrls();
redirectUrls.cancel_url = "http://localhost:53238/Cancel.aspx";
redirectUrls.return_url = "http://localhost:53238/CompletePurchase.aspx";
var payment = Payment.Create(apiContext, new Payment
{
intent = "sale",
payer = payer,
transactions = new List<Transaction> { transaction },
redirect_urls = redirectUrls
});
Session["paymentId"] = payment.id;
foreach (var link in payment.links)
{
if (link.rel.ToLower().Trim().Equals("approval_url"))
{
Response.Redirect(link.href);
}
}
这是CompletePurchase(包含问题代码的文件)的网络表单
//Authenticate with paypal
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
//Get APIContent Object
var apiContext = new APIContext(accessToken);
var paymentId = Session["paymentId"].ToString();
if (!String.IsNullOrEmpty(paymentId))
{
var payment = new Payment() { id = paymentId };
//retrieve the payerId from the querystring and use it to create a new payment execution object
var payerId = Request.QueryString["PayerID"].ToString();
var paymentExecution = new PaymentExecution() { payer_id = payerId };
//execute the payment
var executedPayment = payment.Execute(apiContext, paymentExecution);
litInformation.Text = "<p>Your order has been completed</p>";
btnConfirmPurchase.Visible = false;
}
第二个代码块中的var executePayment行是这里的问题。
请帮助!