I am running into a snag trying to patch a PayPal order with updated totals. I am using the PayPal Checkout-NET-SDK that they provide out on GitHub, but the sample documentation they have for Patch Order Sample is a bit too simplistic:
https://github.com/paypal/Checkout-NET-SDK/blob/develop/Samples/PatchOrderSample.cs
I am trying to update the following path:
/purchase_units/@reference_id==’default’/amount"
I’ve tried using a combination of setting the value as:
- A JSON string representing the AmountWithBreakdown object
- An AmountWithBreakdown object
When calling the API with an AmountWithBreakdown object assigned as the value, I am met with a .NET exception:
Type ‘PayPalCheckoutSdk.Orders.AmountWithBreakdown’ with data contract name ‘AmountWithBreakdown:http://schemas.datacontract.org/2004/07/PayPalCheckoutSdk.Orders’ is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
Sample Function that builds the PATCH request:
Private Function BuildPatchRequest() As List(Of Patch(Of Object))
Dim patches = New List(Of Patch(Of Object)) From {
New Patch(Of Object) With {
.Op = "replace",
.Path = "/intent",
.Value = "CAPTURE"
},
New Patch(Of Object) With {
.Op = "replace",
.Path = "/purchase_units/@reference_id=='default'/amount",
.Value = New AmountWithBreakdown With {
.CurrencyCode = Me.Order.CurrencyCode,
.Value = Me.Order.Total.ToString("N2"),
.AmountBreakdown = New AmountBreakdown With {
.ItemTotal = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = Me.Order.SubTotal.ToString("N2")},
.TaxTotal = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = Me.Order.TaxTotal.ToString("N2")},
.Shipping = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = Me.Order.ShippingTotal.ToString("N2")},
.Discount = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = Me.Order.DiscountTotal.ToString("N2")},
.Handling = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = Me.Order.HandlingFeeTotal.ToString("N2")},
.Insurance = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = "0.00"},
.ShippingDiscount = New PayPalCheckoutSdk.Orders.Money With {.CurrencyCode = Me.Order.CurrencyCode, .Value = "0.00"}
}
}
}
}
Return patches
End Function
All attempts at constructing the JSON manually as a string and assigning it to the value are met with the generic INVALID_PARAMETER_SYNTAX error response, despite the output passing JSON validation tools.
Has anyone had any success updating this datapoint with PayPal using this SDK? My implementation is in VB but I have gotten the gist of implementing all other functionality with the SDK being sourced in C#.
2
Answers
I’m running into the same issue.
System.Runtime.Serialization.SerializationException: Type ‘PayPalCheckoutSdk.Orders.AmountWithBreakdown’ with data contract name ‘AmountWithBreakdown:http://schemas.datacontract.org/2004/07/PayPalCheckoutSdk.Orders‘ is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
The call never makes it to PayPal for a response because it is failing to serialize the request. The only way I’ve been able to get it to work is by generating my own request without the SDK. This isn’t production ready code. I was just testing to see if it would work if I crafted the request myself, and it does.
I had this problem too and I looked to PayPalHttp.HttpClient source code and found the solution that working for me. Maybe this would be helpful for you.