I'm trying to follow the example on the API documentation in order to create my first payment, unfortunately what I get no matter what is a MALFORMED REQUEST... I have checked and checked and I can't find where the error is... please take a look at my console code app: using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using static System.Console;
namespace ConsoleApp2
{
class Program
{
private static string clientID = "AeYFKFaosBNqZ_prqYRG821QgpbsQ5yuHRll1rhj7PHySwHy88AY42hYfrY5Fmx3Jrnieh75O3NyWP1O";
private static string Secret = "ECuUltxHhBahx3cPlTBH9CZVkcE56NUZGCagjaa9SHXe3Nt1q8v93fcqNSZMMQk0gQ1CiKUDfnHXlvus";
private static string baseURL = "https://api.sandbox.paypal.com";
private static string Token, TokenType;
static void Main(string[] args)
{
try
{
getToken();
WriteLine($"My Paypal Token is:\n{Token}");
WriteLine($"Payment: {MakePayment()}");
}
catch (Exception myEx)
{
WriteLine($"Error: {myEx.Message}");
}
finally
{
ReadLine();
}
}
private static string MakePayment()
{
string EndPoint = "/v1/payments/payment";
var cliente = new RestClient(baseURL);
var request = new RestRequest(EndPoint, Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", $"{TokenType} {Token}");
string myJson = @"{
""intent"":""sale""
, ""payer"":{
""payment_method"":""paypal""
}
, ""redirect_urls"":{
""return_url"":""https://example.com/return"",
""cancel_url"":""https://example.com/cancel""
}
, ""transactions"": [{
""reference_id"":""REF0001"",
""invoice_number"":""INV89673"",
""description"":""The payment transaction description."",
""amount"":{
""currency"":""USD"",
""total"": ""30.00"",
""details"": {
""subtotal"":""29.00"",
""tax"":""1.00""
}
}
}]
}";
Write(myJson);
request.AddJsonBody(JObject.Parse(myJson));
IRestResponse R = cliente.Execute(request);
return R.Content;
}
private static void getToken()
{
string EndPoint = "/v1/oauth2/token";
var cliente = new RestClient(baseURL);
cliente.Authenticator = new HttpBasicAuthenticator(clientID, Secret);
var request = new RestRequest(EndPoint, Method.POST);
request.AddParameter("grant_type", "client_credentials");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
IRestResponse Respuesta = cliente.Execute(request);
if (!Respuesta.IsSuccessful | Respuesta.StatusCode.ToString() == "Unauthorized")
{
dynamic ObjetoJson = JsonConvert.DeserializeObject<dynamic>(Respuesta.Content);
throw new Exception((string)ObjetoJson.error_description);
}
else {
PayPalToken myToken = JsonConvert.DeserializeObject<PayPalToken>(Respuesta.Content);
Token = myToken.access_token;
TokenType = myToken.token_type;
}
}
}
} at a glance, you can see its about a very simple payment... it just don't work... I'm using core 2.0 console app PLEASE help, what's wrong with my request? By the way, it also worth mention that I did try to use SDK for .net (with Paypal 2.0.0-rc2.), and I was able to do a payment, EXCEPT for an option called ShippingPreferences on ApplicationContext, if you use that option, the same error will appear, except the class show no problems, another user mentioned there was an error on the class definition, it should be ShippingPreference NOT ShippingPreferences...
... View more