Hello,
I need your help to implement paypal payment on my website. I am a beginner and maybe I don't understand something
Environment: ASP.net c#, MVC
As required, I created a PayPalClient class in a folder named "Model" (file PayPalClient.cs)
public class PayPalClient
{
/**
Set up PayPal environment with sandbox credentials.
In production, use LiveEnvironment.
*/
public static PayPalEnvironment environment()
{
return new SandboxEnvironment("ClientId","SecretClientId");
}
/**
Returns PayPalHttpClient instance to invoke PayPal APIs.
*/
public static HttpClient client()
{
return new PayPalHttpClient(environment());
}
public static HttpClient client(string refreshToken)
{
return new PayPalHttpClient(environment(), refreshToken);
}
/**
Use this method to serialize Object to a JSON string.
*/
public static String ObjectToJSONString(Object serializableObject)
{
MemoryStream memoryStream = new MemoryStream();
var writer = JsonReaderWriterFactory.CreateJsonWriter(
memoryStream, Encoding.UTF8, true, true, " ");
DataContractJsonSerializer ser = new DataContractJsonSerializer(serializableObject.GetType(), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
ser.WriteObject(writer, serializableObject);
memoryStream.Position = 0;
StreamReader sr = new StreamReader(memoryStream);
return sr.ReadToEnd();
}
Then I created a new class in the same folder name (file Paypal_CreateOrder.cs)
<removed>
And finally I added this code on my html page
<script
src="https://www.paypal.com/sdk/js?client-id=AZC3rUchW2sQM20buHp10EDFuuGHrezM9mwKWLDMHqps4tXUkKZcyq1UZcJrB7Ca-6UNAb7IGMFb9TlZ¤cy=EUR">
</script>
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
createOrder: function (data, actions) {
return fetch("", {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function (res) {
return res.json();
}).then(function (data) {
return data.id;
});
//return actions.order.create({
// purchase_units: [{
// amount: {
// value: '0.01'
// }
// }]
//});
},
onApprove: function (data, actions) {
return actions.order.capture().then(function (details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('/Payment/ApproveOrder/' + data.orderID, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#paypal-button-container');
</script>
As you can see, I don't know what to put in "fetch" for create order.. I tested to call a simple function in a controller with "/Test/TestFunction" where "Test" is the controller name and "TestFunction" is the function name into the controller. And this mechanic works.
I tried to move the create order function into a controller but it not works, when I click on the button, the function is not called
public class PaypalController : Controller
{
//
// GET: /Paypal/
public ActionResult Index()
{
return View();
}
public async static System.Threading.Tasks.Task<PayPalHttp.HttpResponse> CreateOrder(bool debug = false)
{
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
var response = await PayPalClient.client().Execute(request);
if (debug)
{
var result = response.Result<Order>();
Console.WriteLine("Status: {0}", result.Status);
Console.WriteLine("Order Id: {0}", result.Id);
Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
Console.WriteLine("Links:");
foreach (LinkDescription link in result.Links)
{
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}
AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
}
return response;
}
}
Can you help me and explain me how I can implement this?
Maybe I need to add a MapRoute in RouteConfig.cs file?
Thank you for your attention and your help
... View more