Skip to content

Commit

Permalink
Use System.Text.Json (#87)
Browse files Browse the repository at this point in the history
* Add System.Text.Json
* Drop Newtonsoft.Json
* Add JsonStringEnumConverterFactory
* Replace DataMember with JsonPropertyName
* Use System.Test in ObjectExtensions.AsJson
* Utilize JsonSerializerContext
* Add PayPal*JsonSerializerContext.CustomConverters
  • Loading branch information
trejjam authored Jun 3, 2023
1 parent 1cd7a98 commit c7d9309
Show file tree
Hide file tree
Showing 134 changed files with 1,613 additions and 1,664 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static class AuthorizeOrderSample
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

// ReSharper disable once UnusedVariable
var amount = response.PurchaseUnits.Single().AmountWithBreakdown;
Console.WriteLine("Buyer:");
Console.WriteLine("\tEmail Address: {0}", response.Payer.Email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,60 @@
using System.Linq;
using System.Threading.Tasks;

namespace PayPal.Sdk.Checkout.Samples.CaptureIntentExamples
namespace PayPal.Sdk.Checkout.Samples.CaptureIntentExamples;

public static class CaptureOrderSample
{
public static class CaptureOrderSample
/*
Method to capture order after creation. Valid approved order Id should be
passed an argument to this method.
*/
public static async Task<Order?> CaptureOrder(this IPayPalHttpClient payPalHttpClient, AccessToken accessToken,
string orderId, bool debug = false)
{
/*
Method to capture order after creation. Valid approved order Id should be
passed an argument to this method.
*/
public static async Task<Order?> CaptureOrder(this IPayPalHttpClient payPalHttpClient, AccessToken accessToken, string orderId, bool debug = false)
{
var response = await payPalHttpClient.CaptureOrderAsync(accessToken, orderId);
var response = await payPalHttpClient.CaptureOrderAsync(accessToken, orderId);

if (debug && response != null)
if (debug && response != null)
{
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Order Id: {0}", response.Id);
Console.WriteLine("Intent: {0}", response.CheckoutPaymentIntent);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Order Id: {0}", response.Id);
Console.WriteLine("Intent: {0}", response.CheckoutPaymentIntent);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

Console.WriteLine("Capture Ids: ");
foreach (var purchaseUnit in response.PurchaseUnits)
Console.WriteLine("Capture Ids: ");
foreach (var purchaseUnit in response.PurchaseUnits)
{
foreach (var capture in purchaseUnit.Payments.Captures)
{
foreach (var capture in purchaseUnit.Payments.Captures)
{
Console.WriteLine("\t {0}", capture.Id);
}
Console.WriteLine("\t {0}", capture.Id);
}

var amount = response.PurchaseUnits.Single().AmountWithBreakdown;
Console.WriteLine("Buyer:");
Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}\n",
response.Payer.Email,
response.Payer.Name.GivenName,
response.Payer.Name.Surname
);
Console.WriteLine("Amount: {0}", amount);
Console.WriteLine("Response JSON:\n{0}", response.AsJson());
}

return response;
var amount = response.PurchaseUnits.Single().AmountWithBreakdown;
Console.WriteLine("Buyer:");
Console.WriteLine("\tEmail Address: {0}\n\tName: {1} {2}\n",
response.Payer.Email,
response.Payer.Name.GivenName,
response.Payer.Name.Surname
);
Console.WriteLine("Amount: {0}", amount);
Console.WriteLine("Response JSON:\n{0}", response.AsJson());
}

/*
Driver Function to invoke capture payment on order.
Order Id should be replaced with the valid approved order id.
*/
//static void Main(string[] args)
//{
// string OrderId = "<<REPLACE-WITH-APPROVED-ORDER-ID>>";
// CaptureOrder(OrderId, true).Wait();
//}
return response;
}

/*
Driver Function to invoke capture payment on order.
Order Id should be replaced with the valid approved order id.
*/
//static void Main(string[] args)
//{
// string OrderId = "<<REPLACE-WITH-APPROVED-ORDER-ID>>";
// CaptureOrder(OrderId, true).Wait();
//}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PayPal.Sdk.Checkout.Extensions;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -8,6 +9,7 @@ namespace PayPal.Sdk.Checkout.Samples.CaptureIntentExamples;
public static class RunAll
{
//Rename to Main1 => Main
[SuppressMessage("ReSharper", "UnusedMember.Local")]
private static async Task Main1()
{
var payPalHttpClient = SampleHttpClientFactory.CreateHttpClient();
Expand Down
82 changes: 41 additions & 41 deletions samples/PayPal.Sdk.Checkout.Samples/CapturesRefundSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,56 @@
using System;
using System.Threading.Tasks;

namespace PayPal.Sdk.Checkout.Samples
namespace PayPal.Sdk.Checkout.Samples;

public static class CapturesRefundSample
{
public static class CapturesRefundSample
/// <summary>
/// Method for refund the capture. Valid capture Id should be passed an argument to this method.
/// </summary>
public static async Task<Refund?> CapturesRefund(this IPayPalHttpClient httpClient, AccessToken accessToken,
string captureId, bool debug = false)
{
/// <summary>
/// Method for refund the capture. Valid capture Id should be passed an argument to this method.
/// </summary>
public static async Task<Refund?> CapturesRefund(this IPayPalHttpClient httpClient, AccessToken accessToken, string captureId, bool debug = false)
{
var response = await httpClient.CapturesRefundAsync(
accessToken,
captureId,
request =>
var response = await httpClient.CapturesRefundAsync(
accessToken,
captureId,
request =>
{
request.SetPreferReturn(EPreferReturn.Representation);
request.SetRequestBody(new RefundRequest
{
request.SetPreferReturn(EPreferReturn.Representation);
request.SetRequestBody(new RefundRequest
Amount = new Money
{
Amount = new Money
{
Value = "20.00",
CurrencyCode = "USD"
}
});
}
);
Value = "20.00",
CurrencyCode = "USD"
}
});
}
);

if (debug && response != null)
if (debug && response != null)
{
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Refund Id: {0}", response.Id);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Refund Id: {0}", response.Id);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

Console.WriteLine("Response JSON: \n {0}", response.AsJson());
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

return response;
Console.WriteLine("Response JSON: \n {0}", response.AsJson());
}

/*
Driver Function to perform refund on capture.
Capture Id should be replaced with the valid capture id.
*/
// static void Main(string[] args)
// {
// string CaptureId = "<<REPLACE-WITH-VALID-CAPTURE-ID>>";
// CapturesRefund(CaptureId, true).Wait();
// }
return response;
}

/*
Driver Function to perform refund on capture.
Capture Id should be replaced with the valid capture id.
*/
// static void Main(string[] args)
// {
// string CaptureId = "<<REPLACE-WITH-VALID-CAPTURE-ID>>";
// CapturesRefund(CaptureId, true).Wait();
// }
}
76 changes: 38 additions & 38 deletions samples/PayPal.Sdk.Checkout.Samples/GetOrderSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@
using System.Linq;
using System.Threading.Tasks;

namespace PayPal.Sdk.Checkout.Samples
namespace PayPal.Sdk.Checkout.Samples;

public static class GetOrderSample
{
public static class GetOrderSample
/// <summary>
/// This method cn be used to retrieve an order by passing the order id.
/// </summary>
public static async Task<Order?> GetOrder(this IPayPalHttpClient httpClient, AccessToken accessToken,
string orderId, bool debug = false)
{
/// <summary>
/// This method cn be used to retrieve an order by passing the order id.
/// </summary>
public static async Task<Order?> GetOrder(this IPayPalHttpClient httpClient, AccessToken accessToken, string orderId, bool debug = false)
{
var response = await httpClient.GetOrderAsync(
accessToken,
orderId
);
var response = await httpClient.GetOrderAsync(
accessToken,
orderId
);

if (debug && response != null)
if (debug && response != null)
{
Console.WriteLine("Retrieved Order Status");
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Order Id: {0}", response.Id);
Console.WriteLine("Intent: {0}", response.CheckoutPaymentIntent);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("Retrieved Order Status");
Console.WriteLine("Status: {0}", response.Status);
Console.WriteLine("Order Id: {0}", response.Id);
Console.WriteLine("Intent: {0}", response.CheckoutPaymentIntent);
Console.WriteLine("Links:");
foreach (var link in response.Links)
{
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

var amount = response.PurchaseUnits.Single().AmountWithBreakdown;
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
Console.WriteLine("Response JSON: \n {0}", response.AsJson());
Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
}

return response;
var amount = response.PurchaseUnits.Single().AmountWithBreakdown;
Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
Console.WriteLine("Response JSON: \n {0}", response.AsJson());
}

/*
This is the driver method which invokes the getOrder function with Order Id
to retrieve an order details.
To get the correct Order id, we are using the createOrder to create new order
and then we are using the newly created order id.
*/
// static void Main(string[] args)
// {
// HttpResponse createdResponse = CreateOrderSample.CreateOrder().Result;
// GetOrder(createdResponse.Result<Order>().Id).Wait();
// }
return response;
}

/*
This is the driver method which invokes the getOrder function with Order Id
to retrieve an order details.
To get the correct Order id, we are using the createOrder to create new order
and then we are using the newly created order id.
*/
// static void Main(string[] args)
// {
// HttpResponse createdResponse = CreateOrderSample.CreateOrder().Result;
// GetOrder(createdResponse.Result<Order>().Id).Wait();
// }
}
11 changes: 6 additions & 5 deletions samples/PayPal.Sdk.Checkout.Samples/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Newtonsoft.Json;
using System.Text.Json;

namespace PayPal.Sdk.Checkout.Samples;

public static class ObjectExtensions
{
public static string AsJson<TObject>(this TObject sourceObject)
{
return JsonConvert.SerializeObject(sourceObject, typeof(TObject), new JsonSerializerSettings());
}
public static string AsJson<TObject>(
this TObject sourceObject
) => JsonSerializer.Serialize(
sourceObject
);
}
Loading

0 comments on commit c7d9309

Please sign in to comment.