3

So I've been using the Pay Simple SDK. I'm getting this error. Does anyone know how to solve this?

var customerPayment = new NewCustomerPayment<CreditCard>

So my model is in the Pay Simple SDK. This is for Payment Transaction.

Controller code:

[HttpPost]
[ValidateAntiForgeryToken]
public async System.Threading.Tasks.Task<ActionResult> Create(FormCollection formCollection) {
    string username = "jnjk";
    string apiKey = "khkh";
    string baseUrl = "https://sandbox-api.paysimple.com";
    var settings = new PaySimpleSdk.Models.PaySimpleSettings(apiKey, username, baseUrl);

    var paymentService = new PaymentService(settings);

    var customerPayment = new NewCustomerPayment<CreditCard>()
    {

        Customer = new Customer
        {
            FirstName = formCollection["FirstName"],
            LastName = formCollection["LastName"],
            BillingAddress = (Address)Enum.Parse(typeof(Address), formCollection["BillingAddress"])
        },

        Account = new CreditCard
        {
            CreditCardNumber = formCollection["CreditCardNumber"],
            ExpirationDate = formCollection["ExpirationDate"],
            Issuer = (Issuer)Enum.Parse(typeof(Issuer), formCollection["Issuer"])
        },

        Payment = new Payment
        {  
            Amount = int.Parse(formCollection["Amount"]),
            Cvv = formCollection["Ccv"]
        }

    };

    var newCustomerPayment = await paymentService.CreateNewCustomerPaymentAsync(customerPayment);

    return View();
}

Model from sdk

This is The Billing Address in the Customer Model..

  public Address BillingAddress { get; set; }

This is for Address

  public class Address : IValidatable
  {
    public Address();
    public string City { get; set; }
    public CountryCode? Country { get; set; }
    public StateCode? StateCode { get; set; }
    public string StreetAddress1 { get; set; }
    public string StreetAddress2 { get; set; }
    public string ZipCode { get; set; }
  }
Woshooo
  • 250
  • 1
  • 2
  • 18

3 Answers3

3

Are you sure Address and Issuer are enums? They sound like classes to me. Enum.Parse(Type enumType, string value) throws an exception if enumType is not an enum-type.

Gooz
  • 1,106
  • 2
  • 9
  • 20
  • public enum Issuer { Visa = 12, Master = 13, Amex = 14, Discover = 15 } – Woshooo Mar 11 '17 at 15:17
  • 1
    So I think the address is not enum, – Woshooo Mar 11 '17 at 15:19
  • @SylzzHeartnet, that's probably the case then. – Gooz Mar 11 '17 at 15:19
  • what would be the proper way of calling it?? – Woshooo Mar 11 '17 at 15:21
  • That indeed doesn't look like an enum. You could try `BillingAddress = formCollection["BillingAddress"]` and making `BillingAddress` of type `Address`. – Gooz Mar 11 '17 at 15:22
  • I already tried that but having an error in this because it's not a string, because address have 6 other classes(street1,street2,country,state,city and zip) – Woshooo Mar 11 '17 at 15:25
  • @SylzzHeartnet, that's why you should change the type of `BillingAddress` to `Address`. – Gooz Mar 11 '17 at 15:26
  • I can't change it, because the model is from PaySimple SDK, another way to solve this ?? – Woshooo Mar 11 '17 at 15:28
  • @SylzzHeartnet, instead of asking it here, you should open this as a new question (if you're really stuck). Also don't forget to mark this as the correct answer if it helped you :) – Gooz Mar 11 '17 at 15:30
  • What do you think is the possible question for this?? I don't know what type is that address? string,int,enum or?? I can't produce a question :D – Woshooo Mar 11 '17 at 15:35
  • @SylzzHeartnet I don't know what the field represents so I can't help you. Maybe it requires all fields of `Address` concatenated in a `String`. You should read the official documentation. – Gooz Mar 11 '17 at 15:39
0

According to your code your raw address data lives in a FormCollection, which is some form of a NameValueCollection. So you would need to know what the contents of formCollection["BillingAddress"] looks like.

If this is some form of json or map, then you can parse it with a Json parser or via reflection and fill your Address class.

Note: it would be helpful, if you showed us, what the value of this key looks like, in case this does not answer your question.

Ronald Rink 'd-fens'
  • 1,289
  • 1
  • 10
  • 27
0

I already solved my problem by doing this...

 BillingAddress =
    {
     StreetAddress1 = formCollection["StreetAddress1"],
     StreetAddress2 = formCollection["StreetAddress2"],
     City = formCollection["City"],
     StateCode = (StateCode)Enum.Parse(typeof(StateCode), formCollection["StateCode"]),
     Country = (CountryCode)Enum.Parse(typeof(CountryCode), formCollection["Country"]),
     ZipCode = formCollection["ZipCode"]
    }
Woshooo
  • 250
  • 1
  • 2
  • 18