I'm hacking hard at Battle Hack London and I've stumbled in an annoying problem. The PayPal SDK for c# doesn't seem to work quite right.
I'm trying to do my first transaction and here's my code (which I put together fixing the broken online docs:
var tokenCredential = new OAuthTokenCredential(something, someother);
var accessToken = tokenCredential.GetAccessToken();
Payment createdPayment = new Payment
{
intent = "sale",
transactions = new List<Transaction>
{
new Transaction
{
amount = new Amount
{
total = value.ToString("R"),
currency = "GBP"
},
description = forWhat
}
}
}.Create(accessToken);
This results in
Cannot parse *.Config file. Ensure you have configured the 'paypal' section correctly.
which I've traced down to this line of code but I don't know how to configure that section correctly and I can't find the correct documentation.
How is tthe csharp REST SDK supposed to be configured?
I was running into this same error. I tried Skliwz's solution but it did not work for me.
Instead I was able to get a result by passing a dictionary object with the call.
Dictionary<string, string> payPalConfig = new Dictionary<string, string>();
payPalConfig.Add("mode", "sandbox");
OAuthTokenCredential tokenCredential = new AuthTokenCredential("myCliedId", "myClientSecret", payPalConfig);
string accessToken = tokenCredential.GetAccessToken();
Still working on get my Log In to work...
I've worked this out with the support of a PayPal dev. One needs to add:
<configSections>
<section name="paypal" type="PayPal.Manager.SDKConfigHandler, PayPalCoreSDK"/>
</configSections>
<paypal>
<accounts>
<account apiUsername="xxx"
apiPassword="yyy"
applicationId="APP-80W284485P519543T"
apiSignature="zzz"
/>
</accounts>
<settings>
<add name="mode" value="sandbox"/>
</settings>
</paypal>
where xxx
, yyy
, zzz
you are values that you get from the "Account details" of your main sandbox test account.
If you're using PayPal .Net SDK (mine is version 1.3.0) you just need the following:
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<paypal>
<settings>
<add name="mode" value="sandbox" />
</settings>
</paypal>
If you're like me, and don't want to store the client information in the *.config file (web.config, app.config) I've found you can specify it in a Dictionary which you have to pass-in to OAuthTokenCredential AND assigned to the APIContext.Config (key to working):
var clientId = "___REPLACE_WITH_CLIENTID___";
var clientSecret = "___REPLACE_WITH_CLIENTSECRET___";
var sdkConfig = new Dictionary<string, string> {
{ "mode", "sandbox" },
{ "clientId", clientId },
{ "clientSecret", clientSecret }
};
var accessToken = new OAuthTokenCredential(clientId, clientSecret, sdkConfig).GetAccessToken();
var apiContext = new APIContext(accessToken);
apiContext.Config = sdkConfig;
Seems a bit redundant to have to pass it into OAuthTokenCredential and set it to apiContext.Config, but that's what works for me.
Just for future reference, the available config settings for the PayPal .NET SDK are now provided on the SDK's GitHub wiki. This includes information on what all the supported PayPal config settings are and their default values.
The wiki also includes information on how to (optionally) setup log4net in the config if you'd like to enable logging with your application.
If any information is missing or needs clarification, or if you'd like to request support for more config settings, please don't hesitate to let me know here or on GitHub.
var config = ConfigManager.Instance.GetProperties();
// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
Web config:
<configuration>
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<!-- PayPal SDK settings -->
<paypal>
<settings>
<add name="mode" value="sandbox"/>
<add name="clientId" value="_client_Id_"/>
<add name="clientSecret" value="_client_secret_"/>
</settings>
</paypal>
</configuration>
来源:https://stackoverflow.com/questions/19338986/paypal-c-sharp-rest-api-asks-for-an-undocumented-configuration-section