Skip to content

Commit d070afb

Browse files
author
Rajaram Padmanathan
committed
Updated Samples to print JSON serialized response
1 parent e63fcb1 commit d070afb

File tree

9 files changed

+55
-15
lines changed

9 files changed

+55
-15
lines changed

Samples/AuthorizeIntentExamples/AuthorizeOrderSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public async static Task<HttpResponse> AuthorizeOrder(string OrderId, bool debug
3434
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
3535
Console.WriteLine("Buyer:");
3636
Console.WriteLine("\tEmail Address: {0}", result.Payer.EmailAddress);
37+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
3738
}
3839

3940
return response;

Samples/AuthorizeIntentExamples/CaptureOrderSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async static Task<HttpResponse> CaptureOrder(string AuthorizationId, bool
2828
{
2929
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
3030
}
31+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
3132
}
3233

3334
return response;

Samples/AuthorizeIntentExamples/CreateOrderSample.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using CheckoutNetsdk.Orders;
77
using BraintreeHttp;
88

9+
using System.IO;
10+
using System.Text;
11+
using System.Runtime.Serialization.Json;
12+
913
namespace Samples.AuthorizeIntentExamples
1014
{
1115
public class CreateOrderSample
@@ -21,8 +25,8 @@ private static OrderRequest BuildRequestBody()
2125
{
2226
BrandName = "EXAMPLE INC",
2327
LandingPage = "BILLING",
24-
CancelUrl = "https://www.google.com",
25-
ReturnUrl = "https://www.google.com",
28+
CancelUrl = "https://www.example.com",
29+
ReturnUrl = "https://www.example.com",
2630
UserAction = "CONTINUE",
2731
ShippingPreference = "SET_PROVIDED_ADDRESS"
2832
},
@@ -36,7 +40,7 @@ private static OrderRequest BuildRequestBody()
3640
Amount = new AmountWithBreakdown
3741
{
3842
CurrencyCode = "USD",
39-
Value = "230.00",
43+
Value = "220.00",
4044
Breakdown = new AmountBreakdown
4145
{
4246
ItemTotal = new Money
@@ -47,7 +51,7 @@ private static OrderRequest BuildRequestBody()
4751
Shipping = new Money
4852
{
4953
CurrencyCode = "USD",
50-
Value = "30.00"
54+
Value = "20.00"
5155
},
5256
Handling = new Money
5357
{
@@ -136,10 +140,9 @@ public async static Task<HttpResponse> CreateOrder(bool debug=false)
136140
request.Prefer("return=representation");
137141
request.RequestBody(BuildRequestBody());
138142
var response = await PayPalClient.client().Execute(request);
139-
143+
var result = response.Result<Order>();
140144
if (debug)
141145
{
142-
var result = response.Result<Order>();
143146
Console.WriteLine("Status: {0}", result.Status);
144147
Console.WriteLine("Order Id: {0}", result.Id);
145148
Console.WriteLine("Intent: {0}", result.Intent);
@@ -150,8 +153,8 @@ public async static Task<HttpResponse> CreateOrder(bool debug=false)
150153
}
151154
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
152155
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
156+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
153157
}
154-
155158
return response;
156159
}
157160

@@ -161,13 +164,18 @@ private static OrderRequest BuildRequestBodyWithMinimumFields()
161164
OrderRequest orderRequest = new OrderRequest()
162165
{
163166
Intent = "AUTHORIZE",
167+
ApplicationContext = new ApplicationContext
168+
{
169+
CancelUrl = "https://www.example.com",
170+
ReturnUrl = "https://www.example.com"
171+
},
164172
PurchaseUnits = new List<PurchaseUnitRequest>
165173
{
166174
new PurchaseUnitRequest{
167175
Amount = new AmountWithBreakdown
168176
{
169177
CurrencyCode = "USD",
170-
Value = "230.00"
178+
Value = "220.00"
171179
}
172180

173181
}

Samples/CaptureIntentExamples/CaptureOrderSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public async static Task<HttpResponse> CaptureOrder(string OrderId, bool debug =
4444
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
4545
Console.WriteLine("Buyer:");
4646
Console.WriteLine("\tEmail Address: {0}\n\tName: {1}\n\tPhone Number: {2}{3}", result.Payer.EmailAddress, result.Payer.Name.FullName, result.Payer.Phone.CountryCode, result.Payer.Phone.NationalNumber);
47+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
4748
}
4849

4950
return response;

Samples/CaptureIntentExamples/CreateOrderSample.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ private static OrderRequest BuildRequestBody()
2626
{
2727
BrandName = "EXAMPLE INC",
2828
LandingPage = "BILLING",
29-
CancelUrl = "https://www.google.com",
30-
ReturnUrl = "https://www.google.com",
29+
CancelUrl = "https://www.example.com",
30+
ReturnUrl = "https://www.example.com",
3131
UserAction = "CONTINUE",
3232
ShippingPreference = "SET_PROVIDED_ADDRESS"
3333
},
@@ -41,7 +41,7 @@ private static OrderRequest BuildRequestBody()
4141
Amount = new AmountWithBreakdown
4242
{
4343
CurrencyCode = "USD",
44-
Value = "230.00",
44+
Value = "220.00",
4545
Breakdown = new AmountBreakdown
4646
{
4747
ItemTotal = new Money
@@ -52,7 +52,7 @@ private static OrderRequest BuildRequestBody()
5252
Shipping = new Money
5353
{
5454
CurrencyCode = "USD",
55-
Value = "30.00"
55+
Value = "20.00"
5656
},
5757
Handling = new Money
5858
{
@@ -160,6 +160,7 @@ public async static Task<HttpResponse> CreateOrder(bool debug = false)
160160
}
161161
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
162162
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
163+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
163164
}
164165

165166
return response;

Samples/CaptureIntentExamples/RunAllCaptureIntentFlow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class RunAll
1111
//Rename to Main1 => Main
1212
static void Main1(string []args){
1313
Console.WriteLine("Running Capture Intent Flow..");
14-
var createOrderResponse = Samples.CaptureIntentExamples.CreateOrderSample.CreateOrder().Result;
14+
var createOrderResponse = Samples.CaptureIntentExamples.CreateOrderSample.CreateOrder(true).Result;
1515
var createOrderResult = createOrderResponse.Result<Order>();
1616
Console.WriteLine("Status: {0}", createOrderResult.Status);
1717
Console.WriteLine("Order Id: {0}", createOrderResult.Id);
@@ -28,7 +28,7 @@ static void Main1(string []args){
2828
Console.Read();
2929

3030
Console.WriteLine("Capturing the payment...");
31-
var captureOrderResponse = CaptureOrderSample.CaptureOrder(createOrderResult.Id).Result;
31+
var captureOrderResponse = CaptureOrderSample.CaptureOrder(createOrderResult.Id, true).Result;
3232
var captureOrderResult = captureOrderResponse.Result<Order>();
3333
var captureId= "";
3434
Console.WriteLine("Status: {0}", captureOrderResult.Status);
@@ -53,7 +53,7 @@ static void Main1(string []args){
5353
captureOrderResult.Payer.Phone.CountryCode, captureOrderResult.Payer.Phone.NationalNumber);
5454

5555
Console.WriteLine("Refunding the Order....");
56-
var refundOrderResponse = CapturesRefundSample.CapturesRefund(captureId).Result;
56+
var refundOrderResponse = CapturesRefundSample.CapturesRefund(captureId, true).Result;
5757
var refundOrderResult = refundOrderResponse.Result<CheckoutNetsdk.Payments.Refund>();
5858
Console.WriteLine("Status: {0}", refundOrderResult.Status);
5959
Console.WriteLine("Refund Id: {0}", refundOrderResult.Id);

Samples/CapturesRefundSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public async static Task<HttpResponse> CapturesRefund(string CaptureId, bool deb
3737
{
3838
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
3939
}
40+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
4041
}
4142
return response;
4243
}

Samples/GetOrderSample.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public async static Task<HttpResponse> GetOrder(string orderId, bool debug = fal
3030
}
3131
AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
3232
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
33+
Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
3334

3435
return response;
3536
}

Samples/PayPalClient.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22
using CheckoutNetsdk.Core;
33
using BraintreeHttp;
44

5+
using System.IO;
6+
using System.Text;
7+
using System.Runtime.Serialization.Json;
8+
59
namespace Samples
610
{
711
public class PayPalClient
812
{
13+
/**
14+
Setting up PayPal environment with credentials with sandbox cerdentails.
15+
For Live, this should be LiveEnvironment Instance.
16+
*/
917
public static PayPalEnvironment environment()
1018
{
1119
return new SandboxEnvironment("AVNCVvV9oQ7qee5O8OW4LSngEeU1dI7lJAGCk91E_bjrXF2LXB2TK2ICXQuGtpcYSqs4mz1BMNQWuso1", "EDQzd81k-1z2thZw6typSPOTEjxC_QbJh6IithFQuXdRFc7BjVht5rQapPiTaFt5RC-HCa1ir6mi-H5l");
1220
}
1321

22+
/**
23+
Returns PayPalHttpClient instance which can be used to invoke PayPal API's.
24+
*/
1425
public static HttpClient client()
1526
{
1627
return new PayPalHttpClient(environment());
@@ -20,5 +31,20 @@ public static HttpClient client(string refreshToken)
2031
{
2132
return new PayPalHttpClient(environment(), refreshToken);
2233
}
34+
35+
/**
36+
This method can be used to Serialize Object to JSON string.
37+
*/
38+
public static String ObjectToJSONString(Object serializableObject)
39+
{
40+
MemoryStream memoryStream = new MemoryStream();
41+
var writer = JsonReaderWriterFactory.CreateJsonWriter(
42+
memoryStream, Encoding.UTF8, true, true, " ");
43+
DataContractJsonSerializer ser = new DataContractJsonSerializer(serializableObject.GetType(), new DataContractJsonSerializerSettings{UseSimpleDictionaryFormat = true});
44+
ser.WriteObject(writer, serializableObject);
45+
memoryStream.Position = 0;
46+
StreamReader sr = new StreamReader(memoryStream);
47+
return sr.ReadToEnd();
48+
}
2349
}
2450
}

0 commit comments

Comments
 (0)