2015-04-06 91 views
0

这是我用信用卡结账的代码。使用PayPal Trans可以正常工作,但我缺少信用卡转账的东西。C#PayPal REST API使用信用卡结账

HttpContext CurrContext = HttpContext.Current;  
APIContext apiContext = Configuration.GetAPIContext();  
Item item = new Item(); 
      item.name = _ItemDescription; 
      item.currency = "USD"; 
      item.price = _Amount; 
      item.quantity = "1"; 
      item.sku = _UPC; 

      List<Item> itms = new List<Item>(); 
      itms.Add(item); 
      ItemList itemList = new ItemList(); 
      itemList.items = itms; 

      Address billingAddress = new Address(); 
      billingAddress.city = ciTxBx.Text; 
      billingAddress.country_code = "US"; 
      billingAddress.line1 = ad1TxBx.Text; 
      billingAddress.line2 = ad2TxBx.Text; 
      billingAddress.postal_code = pcTxBx.Text; 
      billingAddress.state = stRcb.SelectedValue.ToString(); 

      CreditCard crdtCard = new CreditCard(); 
      crdtCard.billing_address = billingAddress; 
      crdtCard.cvv2 = scTxBx.Text; 
      crdtCard.expire_month = Convert.ToInt32(emonthTxBx.Text); 
      crdtCard.expire_year = Convert.ToInt32(eyearTxBx.Text); 
      crdtCard.first_name = ccfnTxBx.Text; 
      crdtCard.last_name = cclnTxBx.Text; 
      crdtCard.number = ccnTxBx.Text; 
      crdtCard.type = ConvertLower(cctRcb.SelectedValue.ToString()); 

      Details details = new Details(); 
      details.tax = "0"; 
      details.shipping = "0"; 
      details.subtotal = _Amount; 

      Amount amont = new Amount(); 
      amont.currency = "USD"; 
      amont.total = _Amount; 
      amont.details = details; 

      Transaction tran = new Transaction(); 
      tran.amount = amont; 
      tran.description = _ItemDescription; 
      tran.item_list = itemList; 

      List<Transaction> transactions = new List<Transaction>(); 
      transactions.Add(tran); 

      FundingInstrument fundInstrument = new FundingInstrument(); 
      fundInstrument.credit_card = crdtCard; 

      List<FundingInstrument> fundingInstrumentList = new List<FundingInstrument>(); 
      fundingInstrumentList.Add(fundInstrument); 

      PayerInfo pi = new PayerInfo(); 
      pi.email = emTxBx.Text; 
      pi.first_name = fnTxBx.Text; 
      pi.last_name = lnTxBx.Text; 
      pi.shipping_address = billingAddress; 

      Payer payr = new Payer(); 
      payr.funding_instruments = fundingInstrumentList; 
      payr.payment_method = "credit_card"; 
      payr.payer_info = pi; 

      Payment paymnt = new Payment(); 
      paymnt.intent = "sale"; 
      paymnt.payer = payr; 
      paymnt.transactions = transactions; 
      try 
      { 
       Payment createdPayment = paymnt.Create(apiContext); 
       CurrContext.Items.Add("ResponseJson", JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented)); 
      } 
      catch (PayPal.Exception.PayPalException ex) 
      { 
       if (ex.InnerException is PayPal.Exception.ConnectionException) 
       { 
        CurrContext.Response.Write(((PayPal.Exception.ConnectionException)ex.InnerException).Response); 
       } 
       else 
       { 
        CurrContext.Response.Write(ex.Message); 
       } 
      } 
      CurrContext.Items.Add("RequestJson", JObject.Parse(paymnt.ConvertToJson()).ToString(Formatting.Indented)); 

任何人都知道为什么我收到“没有这个请求的有效载荷”消息?

+0

看来你有没有['PayerInfo'你'Payer'(https://developer.paypal.com/webapps/developer/docs/api/#payer-object) 。 Hth ... – EdSF

+0

我编辑了我原来的帖子。感谢发布。尽管如此,仍然没有欢乐 – RooGuru

+0

因此,在检查沙箱后,我看到列出的信用卡交易,但网站仍显示错误“此请求无有效负载”。任何其他想法? – RooGuru

回答

1

嘲笑你的代码(和硬编码值)后,我评论的这一行是我纠正使它工作的唯一的东西(我不知道你的代码是如何编译的):

pi.shipping_address = new ShippingAddress 
{ 
    city = "San Mateo", 
    country_code = "US", 
    line1 = "SO TEST", 
    line2 = "", 
    postal_code = "94002", 
    state = "CA", 
}; 

最后,来自贝宝(成功的)响应(ResponseJson):

ResponseJson: { 
    "id": "PAY-8EU11336Y2032051SKUYZZOQ", 
    "intent": "sale", 
    "payer": { 
     "payment_method": "credit_card", 
     "funding_instruments": [{ 
      "credit_card": { 
       "number": "xxxxxxxxxxxx2274", 
       "type": "visa", 
       "expire_month": 1, 
       "expire_year": 2018, 
       "first_name": "Stack", 
       "last_name": "Overflow", 
       "billing_address": { 
        "line1": "SO TEST", 
        "line2": "", 
        "city": "San Mateo", 
        "country_code": "US", 
        "postal_code": "94002", 
        "state": "CA" 
       } 
      } 
     }] 
    }, 
    "transactions": [{ 
     "related_resources": [{ 
      "sale": { 
       "id": "1Y28050325502832C", 
       "amount": { 
        "currency": "USD", 
        "total": "99.00" 
       }, 
       "state": "completed", 
       "parent_payment": "PAY-8EU11336Y2032051SKUYZZOQ", 
       "create_time": "2015-04-17T23:52:26Z", 
       "update_time": "2015-04-17T23:52:28Z", 
       "links": [{ 
        "href": "https://api.sandbox.paypal.com/v1/payments/sale/1Y28050325502832C", 
        "rel": "self", 
        "method": "GET" 
       }, 
       { 
        "href": "https://api.sandbox.paypal.com/v1/payments/sale/1Y28050325502832C/refund", 
        "rel": "refund", 
        "method": "POST" 
       }, 
       { 
        "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8EU11336Y2032051SKUYZZOQ", 
        "rel": "parent_payment", 
        "method": "GET" 
       }] 
      } 
     }], 
     "amount": { 
      "currency": "USD", 
      "total": "99.00", 
      "details": { 
       "subtotal": "99.00" 
      } 
     }, 
     "description": "trnx desc", 
     "item_list": { 
      "items": [{ 
       "quantity": "1", 
       "name": "foo", 
       "price": "99.00", 
       "currency": "USD", 
       "sku": "fooSku" 
      }] 
     } 
    }], 
    "state": "approved", 
    "create_time": "2015-04-17T23:52:26Z", 
    "update_time": "2015-04-17T23:52:28Z", 
    "links": [{ 
     "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8EU11336Y2032051SKUYZZOQ", 
     "rel": "self", 
     "method": "GET" 
    }] 
} 

而在贝宝显影剂沙盒交易所示:

screen shot paypal developer

H个...

+0

你介意发布你的代码,以便我可以做一个比较我的。谢谢。 – RooGuru

+0

这太令人沮丧了!我仍然收到没有有效载荷的错误。有没有人有使用信用卡的贝宝的C#代码?我不想回到经典的贝宝。 – RooGuru

+0

@RooGuru我用你的代码,除了来自控件的变量(我刚刚提供硬编码值)。我改变的另一件事是'ShippingAddress'的行。你是否在asp.net应用上下文中做了上面的事情?我是(也许我们有什么不同?)。另一个假设是你正在使用.Net SDK(基于你使用的方法和对象名称,仍然只是我的假设)。 – EdSF