|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using BraintreeHttp; |
| 5 | +using CheckoutNetsdk.Core; |
| 6 | +using CheckoutNetsdk.Orders; |
| 7 | +using Samples.AuthorizeIntentExamples; |
| 8 | + |
| 9 | +namespace Samples |
| 10 | +{ |
| 11 | + public class PatchOrderSample |
| 12 | + { |
| 13 | + |
| 14 | + /** |
| 15 | + This method can be used to build the patch request body. |
| 16 | + */ |
| 17 | + private static List<Patch<Object>> BuildPatchRequest() |
| 18 | + { |
| 19 | + var patches = new List<Patch<Object>> |
| 20 | + { |
| 21 | + new Patch<Object> |
| 22 | + { |
| 23 | + Op= "replace", |
| 24 | + Path= "/intent", |
| 25 | + Value= "CAPTURE" |
| 26 | + |
| 27 | + }, |
| 28 | + new Patch<Object> |
| 29 | + { |
| 30 | + Op= "replace", |
| 31 | + Path= "/purchase_units/@reference_id=='PUHF'/description", |
| 32 | + Value= "Physical Goods" |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + }; |
| 37 | + return patches; |
| 38 | + } |
| 39 | + /* |
| 40 | + This method cn be used to patch an order by passing the order id. |
| 41 | + */ |
| 42 | + public async static Task<HttpResponse> PatchOrder(string orderId, bool debug = false) |
| 43 | + { |
| 44 | + var request = new OrdersPatchRequest<Object>(orderId); |
| 45 | + request.RequestBody(BuildPatchRequest()); |
| 46 | + var response = await PayPalClient.client().Execute(request); |
| 47 | + if(debug) |
| 48 | + { |
| 49 | + var ordersGetRequest= new OrdersGetRequest(orderId); |
| 50 | + response = await PayPalClient.client().Execute(ordersGetRequest); |
| 51 | + var result = response.Result<Order>(); |
| 52 | + Console.WriteLine("Retrieved Order Status After Patch"); |
| 53 | + Console.WriteLine("Status: {0}", result.Status); |
| 54 | + Console.WriteLine("Order Id: {0}", result.Id); |
| 55 | + Console.WriteLine("Intent: {0}", result.Intent); |
| 56 | + Console.WriteLine("Links:"); |
| 57 | + foreach (LinkDescription link in result.Links) |
| 58 | + { |
| 59 | + Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method); |
| 60 | + } |
| 61 | + AmountWithBreakdown amount = result.PurchaseUnits[0].Amount; |
| 62 | + Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value); |
| 63 | + Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result)); |
| 64 | + } |
| 65 | + return response; |
| 66 | + } |
| 67 | + |
| 68 | + /* |
| 69 | + This is the driver method which invokes the patchOrder function with Order Id |
| 70 | + to patch an order details. |
| 71 | + |
| 72 | + To get the new Order id, we are using the createOrder to create new order |
| 73 | + and then we are using the newly created order id. |
| 74 | + */ |
| 75 | + // static void Main(string[] args) |
| 76 | + // { |
| 77 | + // HttpResponse createdResponse = CreateOrderSample.CreateOrder(true).Result; |
| 78 | + // Console.WriteLine("\nAfter PATCH (Changed Intent and Amount):"); |
| 79 | + // PatchOrder(createdResponse.Result<Order>().Id, true).Wait(); |
| 80 | + // } |
| 81 | + } |
| 82 | +} |
0 commit comments