В настоящее время я внедряю стороннюю платежную систему через PayPal в своем приложении MVC. Я создал платежные реквизиты, которые проходят через, но в начале метода при проверке, вошел ли пользователь в систему или нет (в настоящее время пользователь не вошел в систему), ссылка перенаправления rel не является appruval_url, а дает себя. Ниже приведен код, который у меня есть на данный момент.
Этот код устанавливается в контроллере, вызываемом кнопкой.
//Controller code
// Paypal Payment
private Payment payment;
// Create a payment using APIContext
private Payment CreatePayment(APIContext apiContext, string redirectUrl)
{
var listItems = new ItemList() { items = new List<Item>() };
listItems.items.Add(new Item()
{
name = "M&Ms",
currency = "EUR",
price = "0.7",
quantity = "5",
sku = "sku"
});
var payer = new Payer() { payment_method = "paypal" };
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl,
return_url = redirectUrl
};
// Create detials object
var details = new Details()
{
tax = "0",
shipping = "0",
subtotal = "3.5"
};
// Create amount object
var amount = new Amount()
{
currency = "EUR",
total = "3.5",
details = details
};
var transectionList = new List<Transaction>();
transectionList.Add(new Transaction()
{
description = "Testing transection",
invoice_number = Convert.ToString((new Random().Next(100000))),
amount = amount,
item_list = listItems
});
payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transectionList,
redirect_urls = redirUrls
};
return payment.Create(apiContext);
}
// Create Execute Payment method
private Payment ExecutePayment(APIContext apiContext, string payerid, string paymentId)
{
var paymentExecution = new PaymentExecution()
{
payer_id = payerid
};
payment = new Payment() { id = paymentId };
return payment.Execute(apiContext, paymentExecution);
}
// Create Payment with Paypal
public ActionResult PaymentWithPaypal()
{
APIContext apiContext = PaypalConfiguration.GetAPIContext();
try
{
string payerId = Request.Params["PayerID"];
if (string.IsNullOrEmpty(payerId))
{
// Creating payment
string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Orders/Index.aspx?";
var guid = Convert.ToString((new Random()).Next(100000));
var createdPayment = CreatePayment(apiContext, baseURI + "guid=" + guid);
// Gets links returned from Paypal response to create call function
var links = createdPayment.links.GetEnumerator();
string paypalRedirectUrl = string.Empty;
while (links.MoveNext())
{
Links link = links.Current;
var temp = link.rel.ToLower().Trim();
if (link.rel.ToLower().Trim().Equals("appruval_url"))
{
paypalRedirectUrl = link.href;
}
Session.Add(guid, createdPayment.id);
return Redirect(paypalRedirectUrl);
}
}
else
{
var guid = Request.Params["guid"];
var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
if (executedPayment.state.ToLower() != "approved")
{
return View("Failure");
}
}
}
catch (Exception e)
{
PaypalLogger.Log("Error: " + e.Message);
return View("Failure");
}
return View("Success");
}
// webconfig code
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit
http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- Paypal & Log4net -->
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<!-- PayPal SDK settings -->
<paypal>
<settings>
<add name="mode" value="sandbox" />
<add name="connectionTimeout" value="360000" />
<add name="requestRetries" value="1" />
<add name="clientId" value="AdzOKk5M7yVA_dWjifjTs6rSAZX9pCRJ7xJ5wX4e_TikEPieiGBJwuMz8h4Su0UVpPVIB5IMgghc7XvZ" />
<add name="clientSecret" value="EEmh_b6MxGLxNNjbI4E-eW96MluwGd0TxU_Ex8Rehyk4yoeHWfiRaS4ERlAVfFrq_izauhIWZukhJ8At" />
</settings>
</paypal>