I have a locally hosted WCF service that communicates with Paypal’s API to take payments.
This is my service contract;
[DataContract]
public class ProcessPaymentRequest
{
[DataMember]
public string CreditCardNumber { get; set; }
[DataMember]
public string Ccv2Number { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public decimal Amount { get; set; }
[DataMember]
public int ExpiryMonth { get; set; }
[DataMember]
public int ExpiryYear { get; set; }
}
I am calling the service using the following code;
var request = new ProcessPaymentRequest
{
Amount = proforma.Total,
CreditCardNumber = billingDetails.Number,
Ccv2Number = billingDetails.Code,
ExpiryMonth = 3,
ExpiryYear = 2014
};
var response = ServiceInvoker.Invoke<IPayments, ProcessPaymentRequest, ProcessPaymentResponse>(
"Payments",
request,
(proxy, req) => proxy.ProcessPayment(req));
if (response == null)
{
}
proforma.Total is decimal (with a value of 12);
public class Proforma
{
public decimal Total { get; set; }
}
In this scenario, my response is null, which means the payment failed. The underlying error from Paypal is that the OrderTotal is invalid.
This is the code that sets the OrderTotal in the WCF (note the conversion);
return new PaymentDetailsType
{
OrderTotal =
new BasicAmountType
{
currencyID = CurrencyCodeType.GBP,
Value = payment.Amount.ToString()
}
};
Now this is the strange thing. If I change the request to be;
var request = new ProcessPaymentRequest
{
Amount = 12,
CreditCardNumber = billingDetails.Number,
Ccv2Number = billingDetails.Code,
ExpiryMonth = 3,
ExpiryYear = 2014
};
It works!
If I try to set the request.Amount using;
decimal totalAmount = proforma.Total;
It fails.
So why does the call using request.Amount = 12 work and the others don’t when the value (decimal) is the same?
The value was being passed to the service as;
A simple change to the Paypal code fixed that;
Credit to Mike Parkhill for the suggestion of Fiddler which allowed me to find the problem.