我正在尝试将PayPal应用到我的网络应用中。
PayPal付款第一次正常运作!
但如果我第二次尝试,那就失败了。我可以只测试一次???
当PayPal成功时,我会看到以下链接:
https://localhost:44333/Paypal/PaymentWithPayPal?guid=94987&paymentId=PAY-1DM32358RW0519317LDPKCYY&token=EC-1G993722W11620224&PayerID=8QXDDJEKRZKZW
当PayPal失败时,我会看到以下链接:
https://localhost:44333/Paypal/PaymentWithPayPal?guid=52246&paymentId=PAY-9V465873R0219235GLDPKDNA&token=EC-1F105391KF572764B&PayerID=8QXDDJEKRZKZW
这是我的 PayPalController :
public class PayPalController : Controller
{
[Authorize]
// GET: PayPal
public ActionResult Index()
{
return View();
}
public ActionResult PaymentWithPaypal()
{
//getting the apiContext as earlier
APIContext apiContext = Configuration.GetAPIContext();
try
{
string payerId = Request.Params["PayerID"];
if (string.IsNullOrEmpty(payerId))
{
//this section will be executed first because PayerID doesn't exist
//it is returned by the create function call of the payment class
// Creating a payment
// baseURL is the url on which paypal sendsback the data.
// So we have provided URL of this controller only
string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority +
"/Paypal/PaymentWithPayPal?";
//guid we are generating for storing the paymentID received in session
//after calling the create function and it is used in the payment execution
var guid = Convert.ToString((new Random()).Next(100000));
//CreatePayment function gives us the payment approval url
//on which payer is redirected for paypal account payment
var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);
//get links returned from paypal in response to Create function call
var links = createdPayment.links.GetEnumerator();
string paypalRedirectUrl = null;
while (links.MoveNext())
{
Links lnk = links.Current;
if (lnk.rel.ToLower().Trim().Equals("approval_url"))
{
//saving the payapalredirect URL to which user will be redirected for payment
paypalRedirectUrl = lnk.href;
}
}
// saving the paymentID in the key guid
Session.Add(guid, createdPayment.id);
return Redirect(paypalRedirectUrl);
}
else
{
// This section is executed when we have received all the payments parameters
// from the previous call to the function Create
// Executing a payment
var guid = Request.Params["guid"];
var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
if (executedPayment.state.ToLower() != "approved")
{
return View("FailureView");
}
}
}
catch (Exception ex)
{
Logger.Log("Error" + ex.Message);
return View("FailureView");
}
return View("SuccessView");
}
private PayPal.Api.Payment payment;
private Payment ExecutePayment(APIContext apiContext, string payerId, string paymentId)
{
var paymentExecution = new PaymentExecution() { payer_id = payerId };
this.payment = new Payment() { id = paymentId };
return this.payment.Execute(apiContext, paymentExecution);
}
private Payment CreatePayment(APIContext apiContext, string redirectUrl)
{
var userId = User.Identity.GetUserId();
var userEmail = User.Identity.GetUserName();
//similar to credit card create itemlist and add item objects to it
var itemList = new ItemList() { items = new List<Item>() };
itemList.items.Add(new Item()
{
name = "Premium Monthly $99.00",
currency = "USD",
price = "99",
quantity = "1",
description = userEmail + " Pays Premium Monthly $99.00 on " + DateTime.Now.ToString("F")
});
var payer = new Payer() { payment_method = "paypal" };
// Configure Redirect Urls here with RedirectUrls object
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl,
return_url = redirectUrl
};
// similar as we did for credit card, do here and create details object
var details = new Details()
{
subtotal = "99.00"
};
// similar as we did for credit card, do here and create amount object
var amount = new Amount()
{
currency = "USD",
total = "99.00", // Total must be equal to sum of shipping, tax and subtotal.
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = userEmail + "Paid Premium Monthly $99.00 on " + DateTime.Now.ToString("F"),
invoice_number = "890918",
amount = amount,
item_list = itemList
});
this.payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
// Create a payment using a APIContext
return this.payment.Create(apiContext);
}
}
请帮忙!