CADES signature with Json data in C# - c#

I have below json data I need to apply CAdES-BES Signature with Automatic JSON Canonicalization. Please find my json data below. Helpful link from https://www.example-code.com/Csharp/itida_egypt_cades_bes_json_canonicalization.asp. I follow the steps but still digital signature is not applying. Its returns normal json data.
[HttpGet]
[Route("api/invoiceLines/")]
public IHttpActionResult getEInvoiceLines()
{
Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
crypt.VerboseLogging = true;
Chilkat.Cert cert = new Chilkat.Cert();
cert.VerboseLogging = true;
// Set the smart card PIN, which will be needed for signing.
cert.SmartCardPin = "1245345";
// There are many ways to load the certificate.
// This example was created for a customer using an ePass2003 USB token.
// Assuming the USB token is the only source of a hardware-based private key..
bool success = cert.LoadFromSmartcard(#"E"); //Is this Right way To load certificate ?
Chilkat.JsonObject cmsOptions = new Chilkat.JsonObject();
// Setting "DigestData" causes OID 1.2.840.113549.1.7.5 (digestData) to be used.
cmsOptions.UpdateBool("DigestData", true);
cmsOptions.UpdateBool("OmitAlgorithmIdNull", true);
// Indicate that we are passing normal JSON and we want Chilkat do automatically
// do the ITIDA JSON canonicalization:
cmsOptions.UpdateBool("CanonicalizeITIDA", true);
crypt.CmsOptions = cmsOptions.Emit();
// The CadesEnabled property applies to all methods that create CMS/PKCS7 signatures.
// To create a CAdES-BES signature, set this property equal to true.
crypt.CadesEnabled = true;
crypt.HashAlgorithm = "sha256";
Chilkat.JsonObject jsonSigningAttrs = new Chilkat.JsonObject();
jsonSigningAttrs.UpdateInt("contentType", 1);
jsonSigningAttrs.UpdateInt("signingTime", 1);
jsonSigningAttrs.UpdateInt("messageDigest", 1);
jsonSigningAttrs.UpdateInt("signingCertificateV2", 1);
crypt.SigningAttributes = jsonSigningAttrs.Emit();
// By default, all the certs in the chain of authentication are included in the signature.
// If desired, we can choose to only include the signing certificate:
crypt.IncludeCertChain = false;
EInvoiceModel.Example ds = new EInvoiceModel.Example();
//Start issuer details
ds.issuer = new EInvoiceModel.Issuer();
ds.issuer.type = "B";
ds.issuer.id = "113317713";
ds.issuer.name = "Issuer Company";
//Start issuer address details
ds.issuer.address = new EInvoiceModel.Address();
ds.issuer.address.branchID = "1";
ds.issuer.address.country = "EG";
ds.issuer.address.governate = "Cairo";
ds.issuer.address.regionCity = "Nasr City";
ds.issuer.address.street = "stree1";
ds.issuer.address.buildingNumber = "Bldg. 0";
ds.issuer.address.postalCode = "68030";
ds.issuer.address.floor = "1";
ds.issuer.address.room = "123";
ds.issuer.address.landmark = "7660 Melody Trail";
ds.issuer.address.additionalInformation = "beside Town Hall";
//Start Receiver details
ds.receiver = new EInvoiceModel.Receiver();
ds.receiver.type = "B";
ds.receiver.id = "3125617";
ds.receiver.name = "Receiver company";
//Start Receiver address datails
ds.receiver.address = new EInvoiceModel.AddressReceiver();
ds.receiver.address.country = "EG";
ds.receiver.address.governate = "Cairo";
ds.receiver.address.regionCity = "Nasr City";
ds.receiver.address.street = "stree1";
ds.receiver.address.buildingNumber = "Bldg. 0";
ds.receiver.address.postalCode = "68030";
ds.receiver.address.floor = "1";
ds.receiver.address.room = "123";
ds.receiver.address.landmark = "7660 Melody Trail";
ds.receiver.address.additionalInformation = "beside Town Hall";
//Document type & version
ds.documentType = "i";
ds.documentTypeVersion = "1.0";
DateTime d = new DateTime();
ds.dateTimeIssued = d; //Invoice date
ds.taxpayerActivityCode = "9478"; //needed info
ds.internalID = "WADIn1234"; //Internal Invoice number
ds.salesOrderReference = "So1234"; //So number //optional
ds.salesOrderDescription = "SO1234"; //So additional Info //optional
ds.proformaInvoiceNumber = "SoPro123"; //optional
//Invoiceline Start
ds.invoiceLines = new List<EInvoiceModel.InvoiceLine>
{
new EInvoiceModel.InvoiceLine
{
description = "Computer1",
itemType = "GPC",
itemCode = "10001774",
unitType = "EA",
quantity = 2,
internalCode = "IC0",
salesTotal = 23.99,
total = 2969.89,
valueDifference = 7.00,
totalTaxableFees = 817.42,
netTotal = 880.71,
itemsDiscount = 5.00,
unitValue = new EInvoiceModel.UnitValue
{
currencySold = "EUR",
amountEGP = 189.40,
amountSold = 10.00,
currencyExchangeRate = 18.94,
},
discount = new EInvoiceModel.Discount
{
rate = 7,
amount = 66.29
},
taxableItems = new List<EInvoiceModel.TaxableItem>
{
new EInvoiceModel.TaxableItem
{
taxType = "T1",
amount = 272.07,
subType = "T1",
rate = 12
}
}
}
}; //Invoice Lines End
//Items total Discount and Sales/NetAmount
ds.totalDiscountAmount = 76.29;
ds.totalSalesAmount = 1609.90;
ds.netAmount = 1533.61;
//Tax Total Start
ds.taxTotals = new List<EInvoiceModel.TaxTotal>
{
new EInvoiceModel.TaxTotal
{
taxType = "T1",
amount = 477.54,
}
};//Tax Total End
//Total Sales Amount & discounts
ds.totalAmount = 5191.50;
ds.extraDiscountAmount = 5.00;
ds.totalItemsDiscountAmount = 14.00;
string strResultJson = JsonConvert.SerializeObject(ds);
//System.IO.File.WriteAllText(#"C:\inetpub\wwwroot\path.json", strResultJson);
// File.WriteAllText(#"ds.json", strResultJson);
// string jsonToSign = "{ ... }";
string jsonToSign = strResultJson;
// Create the CAdES-BES signature.
crypt.EncodingMode = "base64";
// Make sure we sign the utf-8 byte representation of the JSON string
crypt.Charset = "utf-8";
string sigBase64 = crypt.SignStringENC(jsonToSign);
// return Ok(ds);
return Ok(sigBase64);
}

public static string SerializeObject2(object obj, int indent = 0)
{
var sb = new StringBuilder();
if (obj != null)
{
string indentString = new string(' ', indent);
if (obj is string || obj.IsNumber())
{
sb.Append($"\"{obj}\"");
}
else if (obj.GetType().BaseType == typeof(Enum))
{
sb.Append($"\"{obj.ToString()}\"");
}
else if (obj is Array)
{
var elems = obj as IList;
sb.Append($"{indentString}- [{elems.Count}] :\n");
for (int i = 0; i < elems.Count; i++)
{
sb.Append(SerializeObject2(elems[i], indent + 4));
}
}
else
{
Type objType = (obj).GetType();
PropertyInfo[] props = objType.GetProperties();
foreach (PropertyInfo prop in props)
{
if (prop.GetIndexParameters().Length == 0)
{
object propValue = prop.GetValue(obj);
var elems = propValue as IList;
if (elems != null)
{
sb.Append($"\"{prop.Name.ToUpper()}\"");
foreach (var item in elems)
{
sb.Append($"\"{prop.Name.ToUpper()}\"");
sb.Append(SerializeObject2(item, indent + 4));
}
}
else
{
if (Assembly.GetExecutingAssembly().DefinedTypes.Select(p => p.Assembly).ToList().Contains(prop.PropertyType.Assembly))
{
sb.Append($"\"{prop.Name.ToUpper()}\"");
sb.Append(SerializeObject2(propValue, indent + 4));
}
else
{
sb.Append($"\"{prop.Name.ToUpper()}\"\"{propValue}\"");
}
}
}
else if (objType.GetProperty("Item") != null)
{
int count = -1;
if (objType.GetProperty("Count") != null &&
objType.GetProperty("Count").PropertyType == typeof(int))
{
count = (int)objType.GetProperty("Count").GetValue(obj, null);
}
for (int i = 0; i < count; i++)
{
object val = prop.GetValue(obj, new object[] { i });
sb.Append(SerializeObject2(val, indent + 4));
}
}
}
}
}
return sb.ToString();
}

Related

Change for loop to forEach

I am working on a inventory updating program in vs2022 which I am using Gembox Spreadsheet for, It is working right now, but the boss man would like me to try to convert my for loop to a for each loop. The loop is what I am using to update my file.
Current For Loop
public void updateFile(string filename, int range, ItemLine selectedItem, decimal outValue)
{
var fullPath = $".\\Backend Files\\{filename}";
SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY");
var workbook = ExcelFile.Load(fullPath, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
//foreach (var row in worksheet.Rows.Skip(1))
//{
// foreach (var cell in row.AllocatedCells)
// {
// string updateValue =
// if(cell.Value == int.TryParse((int)outValue, out int updateValue))
// {
// }
// }
//}
for (int i = 1; i <= range; i++)
{
var plan = worksheet.Rows[i].Cells[0].Value;
var desc = worksheet.Rows[i].Cells[1].Value;
var csvDescription = plan + " - " + desc;
var platedescription = plan + " - ";
if (selectedItem.ComboDescription == csvDescription)
{
worksheet.Rows[i].Cells[2].Value = selectedItem.Quantity;
}
if (selectedItem.plateDescription == platedescription)
{
worksheet.Rows[i].Cells[1].Value = selectedItem.Quantity;
}
}
workbook.Save(fullPath, new CsvSaveOptions(CsvType.CommaDelimited));
}
I beleive the forEach would look similar to this
private static List<ItemLine> ReadFile(string fileName, string defaultValueDescription)
{
string path = $".\\Backend Files\\{fileName}";
SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY");
var workbook = ExcelFile.Load(path, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
var items = new List<ItemLine>();
items.Add(new ItemLine { Description = defaultValueDescription, Quantity = 0 });
foreach (var row in worksheet.Rows.Skip(1))
{
var cells = row.AllocatedCells;
var il = new ItemLine();
if (cells.Count == 2)
{
il.Description = cells[0].Value?.ToString() ?? "Description Not Found";
il.Quantity = cells[1].IntValue;
}
else if (cells.Count >= 3)
{
il.Plan = cells[0].Value?.ToString() ?? "Plan Not Found";
il.Description = cells[1].Value?.ToString() ?? "Description Not Found";
il.Quantity = cells[2].IntValue;
}
items.Add(il);
}
return items;
}

Problem with stock items, always returns 0 wsdl NetSuite c#

I try to return the stock of the items and I can not get it. Could someone guide me and tell me what I'm doing wrong?
This is my code, quantityAvailable is always 0 and location is null. This is an excerpt from my source code:
var recordRefs = new List<RecordRef>();
var ExternalIds = new List<string>();
ExternalIds.Add("REV0001");
ExternalIds.Add("REV0002");
foreach (string externalId in ExternalIds)
{
recordRefs.Add(new RecordRef
{
externalId = externalId,
type = RecordType.InventoryItem,
typeSpecified = true
});
}
var request1 = new ItemSearchBasic
{
externalId = new SearchMultiSelectField
{
#operator = SearchMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = recordRefs.ToArray()
}
};
SearchResult response = Client.Service.search(request1);
if (response.status.isSuccess)
{
if (response.totalRecords > 1)
{
Record[] records;
records = response.recordList;
for (int lcv = 0; lcv <= records.Length - 1; lcv++)
{
if (records[lcv].GetType().Name == "InventoryItem")
{
Console.WriteLine(((InventoryItem)records[lcv]).itemId);
Console.WriteLine(((InventoryItem)records[lcv]).internalId);
Console.WriteLine(((InventoryItem)records[lcv]).quantityAvailable);
Console.WriteLine(((InventoryItem)records[lcv]).averageCost);
}
}
}
}

SAP.Middleware.Connector.RfcInvalidParameterException: Table line 0 out of range: table is empty

I have a C# console aplication that connects to a SAP server to get data from some of its tables.
Sometimes this error pops up when i try to copy a value from a field in a SAP table called "SOHEADER" to a string variable.
Here's the stack trace:
SAP.Middleware.Connector.RfcInvalidParameterException: Table line 0
out of range: table is empty en
SAP.Middleware.Connector.RfcTable.get_Item(Int32 lineIndex) en
ExportarPedidosSAP.Program.ControlFacturasRemitosNotasCreditos(RfcDestination
rfcDest, String& nroerror) en
C:\Users\USER\Source\repos\BSCPY\BSCPY\ExportarPedidosSAP\Program.cs:línea
1220 en ExportarPedidosSAP.Program.ConnSAPNotasCreditos() en
C:\Users\USER\Source\repos\BSCPY\BSCPY\ExportarPedidosSAP\Program.cs:línea
936
Here is the ControlFacturasRemitosNotasCreditos function:
static void ControlFacturasRemitosNotasCreditos(RfcDestination rfcDest, out string nroerror)
{
Console.WriteLine("** Controlando remitos... ");
nroerror = "";
using (SqlBriefcaseCropScience db = new SqlBriefcaseCropScience())
{
using (var transaction = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.Snapshot }))
{
List<NotaCreditoCabecera> lstpedidos = new List<NotaCreditoCabecera>();
List<NotaCreditoItem> lstItemProducto = new List<NotaCreditoItem>();
IQueryable<NotaCreditoCabecera> ilstpedidos = null;
IQueryable<Compania> ilstcompanias = null;
List<NotaCreditoItem> lstprod = null;
NotaCreditoItem itProd = null;
int statuscode = 0;
decimal FKIMG = 0;
string ProdCode = "";
string statusdesc = "";
string status_doc = "";
string LEGALNUMBER = "";
string LEGALNUMBER_DEL = "";
bool savecabecera = false;
bool nolegalnumber = false;
bool itemfacturado = false;
bool pedidofacturado = false;
List<string> List_LEGALNUMBER_DEL = new List<string>();
List<string> List_LEGALNUMBER = new List<string>();
ilstcompanias = db.Compania;
DateTime unmes = Helpers.GetCurrentDateTime().AddMonths(1);
if (ilstcompanias != null)
{
foreach (Compania comp in ilstcompanias.ToList())
{
ilstpedidos = db.NotaCreditoCabecera.Where(c => c.CodigoCompania == comp.Codigo &&
(c.CodigoEstado == 5 || c.CodigoEstado == 11 && (DbFunctions.TruncateTime(c.FechaEstado) <= DbFunctions.TruncateTime(unmes))));
if (ilstpedidos != null)
{
lstpedidos = ilstpedidos.ToList<NotaCreditoCabecera>();
if (lstpedidos.Count() > 0)
{
RfcRepository rfcRep = rfcDest.Repository;
var funcionStatus = ConfigurationManager.AppSettings["SAP-RFC-Status"];
foreach (NotaCreditoCabecera cabecera in lstpedidos.ToList())
{
lstprod = db.NotaCreditoItem.Where(c => c.CodigoCompania == comp.Codigo && c.NroPedido == cabecera.NroPedido).ToList();
IRfcFunction functionStatus = rfcRep.CreateFunction(funcionStatus);
functionStatus.SetValue("SEL_ORDERSTATUS", "X");
functionStatus.SetValue("P_VKORG", cabecera.SalesOrg);
functionStatus.SetValue("P_BRIEFDOC", cabecera.NroPedido);
IRfcTable tableHstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaH-Status"]);
IRfcTable tableIstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaI-Status"]);
IRfcTable tablaEstatus = functionStatus.GetTable(ConfigurationManager.AppSettings["SAP-TablaE"]);
functionStatus.Invoke(rfcDest);
status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
itemfacturado = false;
pedidofacturado = false;
nolegalnumber = false;
if (tableIstatus.RowCount > 0)
{
for (int e = 0; e < tableIstatus.RowCount; e++)
{
if (Convert.ToString(tableIstatus[e].GetValue("NETWR")) != "")
{
ProdCode = Convert.ToString(tableIstatus[e].GetValue("MATERIAL"));
itProd = lstprod.FirstOrDefault(i => i.CodigoProducto == ProdCode);
if (itProd != null)
{
//Num Factura
LEGALNUMBER = Convert.ToString(tableIstatus[e].GetValue("LEGALNUMBER")).Trim();
if (!String.IsNullOrEmpty(LEGALNUMBER))
{
itemfacturado = true;
//Num Remito
LEGALNUMBER_DEL = Convert.ToString(tableIstatus[e].GetValue("LEGALNUMBER_DEL")).Trim();
//Cantidad
FKIMG = (decimal.TryParse(Convert.ToString(tableIstatus[e].GetValue("FKIMG")), out FKIMG) == true) ?
Convert.ToDecimal(Convert.ToString(tableIstatus[e].GetValue("FKIMG"))) : 0;
//Si algun item no esta completamente facturado se considera el pedido no facturado
if ((LEGALNUMBER != "" && LEGALNUMBER_DEL != "") && (itProd.CantidadDevolucion == Convert.ToDecimal(FKIMG)) && (nolegalnumber == false))
{
pedidofacturado = true;
}
else
{
pedidofacturado = false;
break;
}
}
else
{
nolegalnumber = true;
}
}
}
}
//statuscode = 0;
//if (itemfacturado == true)
//{
// if (pedidofacturado == true)
// {
// statuscode = 12;
// }
//}
//if (statuscode > 0)
//{
// statusdesc = repositorioAdm.CEstado(cabecera.IdTipoOrden, statuscode);
// int reslog = ALogAutorizacion(db, transaction, cabecera.CodigoCompania,
// cabecera.SalesOrg, cabecera.CodigoDivision,
// cabecera.IdTipoOrden, cabecera.TipoOrden,
// cabecera.NroPedido.ToString(),
// cabecera.CodigoEstado.ToString(), cabecera.Estado,
// statuscode.ToString(), statusdesc, "inter", "");
// cabecera.CodigoEstado = statuscode;
// cabecera.Estado = statusdesc;
// cabecera.FechaEstado = Helpers.GetCurrentDateTime();
// savecabecera = true;
//}
}
//Tabla de Error
if (tablaEstatus.RowCount > 0)
{
//Obtengo el Estado
statuscode = 9;
cabecera.CodigoEstado = statuscode;
statusdesc = repositorioAdm.CEstado(cabecera.IdTipoOrden, statuscode);
cabecera.Estado = statusdesc;
cabecera.FechaEstado = Helpers.GetCurrentDateTime();
savecabecera = true;
int reslog = ALogAutorizacion(db, transaction, cabecera.CodigoCompania,
cabecera.SalesOrg, cabecera.CodigoDivision,
cabecera.IdTipoOrden, cabecera.TipoOrden,
cabecera.NroPedido.ToString(),
cabecera.CodigoEstado.ToString(),
cabecera.Estado,
statuscode.ToString(),
statusdesc, "inter", "");
}
}
}
}
if (savecabecera)
{
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
}
transaction.Complete();
}
}
}
}
}
Line 1220 inside ControlFacturasRemitosNotasCreditos:
status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
Line 936 inside ConnSAPNotasCreditos that calls the ControlFacturasRemitosNotasCreditos function:
ControlFacturasRemitosNotasCreditos(rfcDest, out nroerror);
Can somebodoy tell me what could be the issue here? Could it be that the field comes empty from the SAP table and if i try to copy it somewhere the whole thing crashes? Is there a safe way to check if the field is null before trying to copy it?
in your line
status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
you are adressing the IRfcTable tableHstatus with index 0, which means you're trying to read the first entry from that table object. That only works if the table has entries. You should first check whether the table contains any rows and only then continue. After invoking the RFC function, check property RowCount of your IRfcTable.
functionStatus.Invoke(rfcDest);
if (tableHstatus.RowCount > 0) {
status_doc = Convert.ToString(tableHstatus[0].GetValue("GBSTK")).ToUpper();
}
there's a number of reasons why a RFC function might return an empty table. It could be standard behavior in some cases, or it could be because some query parameter was wrong and no data was found, or because an error occurred in the function. Standard SAP BAPIs usually return a status structure or table (often of type BAPIRETURN or BAPIRET2) that contains additional information about error codes, messages etc.

PayPal API Validation Error: Item amount must add up to specified amount subtotal (or total if amount details not specified)

I'm currently having an error(as mentioned in the title). I don't know what's wrong or which line I'm having problems with. I'm sure all the values mentioned in the error are the same but it still pops up the same error.
My aim is to make the all the values the same to solve the error.
Here's the full description of my error:
{
"name": "https://stackoverflow.com/VALIDATION_ERROR",
"details": [
{
"field": "transactions[0]",
"issue": "Item amount must add up to specified amount subtotal (or total if amount details not specified)"
}
],
"message": "Invalid request - see details",
"information_link": "https://developer.paypal.com/docs/api/payments/#errors",
"debug_id": "67a354f1a8fc7"
}
Please help!
Here are my codes for reference:
// scList has 2 values inside.
List<UserShoppingCartDAO> scList = UserShoppingCartDAO.GetCartOfUser(uID);
List<Item> itms = new List<Item>();
ItemList itemList = new ItemList();
decimal deliveryCharges = 0;
string deliveryChargesStr = "";
decimal postagePackingCost = 0;
decimal examPaperPrice = 0;
int quantityOfExamPapers = 0;
decimal subtotal = 0;
decimal total = 0;
var transactionDetails = new Details();
var transactionAmount = new Amount();
Transaction tran = new Transaction();
for (int iCounter = 0; iCounter < scList.Count; iCounter++)
{
Product p = Product.getProduct(scList[iCounter].ProductID.ToString());
deliveryCharges = p.price * 0.05m;
deliveryChargesStr = deliveryCharges.ToString();
postagePackingCost = 10m;
examPaperPrice = p.price;
quantityOfExamPapers = scList[iCounter].ProductQuantity;
subtotal = (quantityOfExamPapers * examPaperPrice);
total = subtotal + postagePackingCost;
var examPaperItem = new Item();
examPaperItem.name = p.productName;
examPaperItem.currency = "SGD";
examPaperItem.price = total.ToString();
examPaperItem.sku = p.productID.ToString();
examPaperItem.description = p.productDesc;
examPaperItem.quantity = quantityOfExamPapers.ToString();
itms.Add(examPaperItem);
itemList.items = itms;
// var transactionDetails = new Detail
transactionDetails.tax = "0";
if (transactionDetails.shipping == null)
{
transactionDetails.shipping = postagePackingCost.ToString();
}
else
{
decimal total2 = decimal.Parse(transactionDetails.shipping) + postagePackingCost;
transactionDetails.shipping = total2.ToString();
}
if (transactionDetails.subtotal == null)
{
transactionDetails.subtotal = subtotal.ToString();
}
else
{
decimal total2 = decimal.Parse(transactionDetails.subtotal) + subtotal;
transactionDetails.subtotal = total2.ToString();
}
//transactionDetails.shipping = postagePackingCost.ToString();
//transactionDetails.subtotal = subtotal.ToString("0.00");
// var transactionAmount = new Amount();
transactionAmount.currency = "SGD";
if (transactionAmount.total == null)
{
transactionAmount.total = total.ToString();
}
else
{
decimal total2 = decimal.Parse(transactionAmount.total) + total;
transactionAmount.total = total2.ToString();
}
transactionAmount.details = transactionDetails;
// Transaction tran = new Transaction();
tran.description = scList[0].ProductDescription;
tran.invoice_number = Guid.NewGuid().ToString();
tran.amount = transactionAmount;
// transaction.item_list = new List<Item>();
tran.item_list = itemList;
transactionList.Add(tran);
}
transactionList.Remove(transactionList[1]);
//Authenticate with paypal
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
//Get APIContent Object
var apiContext = new APIContext(accessToken);
var payer = new Payer();
payer.payment_method = "paypal";
var redirectUrls = new RedirectUrls();
redirectUrls.cancel_url = "http://localhost60681/Cancel.aspx";
redirectUrls.return_url = "http://localhost:60681/CompletePurchase.aspx";
var payment = Payment.Create(apiContext, new Payment
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirectUrls
});
Session["paymentId"] = payment.id;
foreach (var link in payment.links)
{
if (link.rel.ToLower().Trim().Equals("approval_url"))
{
Response.Redirect(link.href);
}
}

Can't save invoice on qb online correctly

I am able to create invoice on quickbook online, but when i check quickbook invoice online, it create the item lists but the price and description is missing. What i am doing wrong.
invoice.AlternateId = new NameValue[] { new NameValue() { Name = "InvoiceID", Value = service.InvoiceId } };
InvoiceHeader Header = new InvoiceHeader();
Header.DocNumber = service.InvoiceId;
Header.TxnDate = service.ServiceDate;
Header.TxnDateSpecified = true;
Header.Msg = service.Note.ToString();
Header.Note = service.Note.ToString();
Header.CustomerId = new IdType() { idDomain = idDomainEnum.QBO, Value = service.CustomerNumberOnline };
Header.Currency = currencyCode.USD;
Header.ShipDate = service.ServiceDate;
Header.ShipDateSpecified = true;
//Print and Email Option fields
if (EmailPrintOption == "Both")
{
Header.ToBeEmailed = true;
Header.ToBeEmailedSpecified = true;
Header.ToBePrinted = true;
Header.ToBePrintedSpecified = true;
}
else if (EmailPrintOption == "print")
{
Header.ToBePrinted = true;
Header.ToBePrintedSpecified = true;
}
else if (EmailPrintOption == "email")
{
Header.ToBeEmailed = true;
Header.ToBeEmailedSpecified = true;
}
else
{
Header.ToBeEmailed = false;
Header.ToBeEmailedSpecified = false;
Header.ToBePrinted = false;
Header.ToBePrintedSpecified = false;
}
PhysicalAddress BillAddress = new PhysicalAddress();
BillAddress.Line1 = service.SoldToAddress;
BillAddress.City = service.SoldToCity;
BillAddress.CountrySubDivisionCode = service.SoldToState;
BillAddress.PostalCode = service.SoldToZip;
BillAddress.Tag = new string[] { "Billing" };
Header.BillAddr = BillAddress;
PhysicalAddress ShipAddress = new PhysicalAddress();
ShipAddress.Line1 = service.JobJocationAddress;
ShipAddress.City = service.JobLocationCity;
ShipAddress.CountrySubDivisionCode = service.JobLocationState;
ShipAddress.PostalCode = service.JobLocationZip;
BillAddress.Tag = new string[] { "Shipping" };
Header.ShipAddr = ShipAddress;
decimal discount;
if (decimal.TryParse(service.Discount, out discount))
{
Header.ItemElementName = ItemChoiceType2.DiscountAmt;
Header.Item = -1 * discount; //discount must be negative
}
InvoiceLine[] InvoiceLines = new InvoiceLine[TaskOnlineIDs.Count];
object[] intuitItems = new object[TaskOnlineIDs.Count];
for (int i = 0; i < InvoiceLines.Length; i++)
{
InvoiceLines[i] = new InvoiceLine();
InvoiceLines[i].ItemsElementName = new Intuit.Ipp.Data.Qbo.ItemsChoiceType2[]
{
Intuit.Ipp.Data.Qbo.ItemsChoiceType2.ItemId
};
InvoiceLines[i].Items = new object[] {
new IdType(){idDomain=idDomainEnum.QBO, Value=TaskOnlineIDs[i]}
};
}
invoice.Line = InvoiceLines;
Header.SalesTermName = service.PaymentType;
Header.SalesRepName = service.TechName;
Header.PONumber = service.PurchaseOrder;
invoice.Header = Header;
What i miss here ??
Here what happen is, when i add invoice lines to invoice it set amount of each item to "0" by default, so when i try to add discount pragmatically it give's me error of discount greater than subtotal which is correct. But my question is why do i need to add cost for each item explicitly when i have already defined them earlier. We should add only itemId and rest QB should calculate.

Categories