forked from laniatech/Checkout-NET-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayPalClient.cs
54 lines (49 loc) · 1.99 KB
/
PayPalClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using PayPalCheckoutSdk.Core;
using PayPalHttp;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace Samples
{
public class PayPalClient
{
/**
Setting up PayPal environment with credentials with sandbox cerdentails.
For Live, this should be LiveEnvironment Instance.
*/
public static PayPalEnvironment environment()
{
return new SandboxEnvironment(
System.Environment.GetEnvironmentVariable("PAYPAL_CLIENT_ID") != null ?
System.Environment.GetEnvironmentVariable("PAYPAL_CLIENT_ID"):"<<PAYPAL-CLIENT-ID>>",
System.Environment.GetEnvironmentVariable("PAYPAL_CLIENT_SECRET") != null ?
System.Environment.GetEnvironmentVariable("PAYPAL_CLIENT_SECRET"):"<<PAYPAL-CLIENT-SECRET>>");
}
/**
Returns PayPalHttpClient instance which can be used to invoke PayPal API's.
*/
public static HttpClient client()
{
return new PayPalHttpClient(environment());
}
public static HttpClient client(string refreshToken)
{
return new PayPalHttpClient(environment(), refreshToken);
}
/**
This method can be used to Serialize Object to 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();
}
}
}