My problem as it is right now. It is that I must have made such that a customer can buy an item that is only paid once. Thus assigned Invoice id and PDf to the database.
As it is right now I only get hold of Invoice id while PDF is null.
I've read a little more about this.
Invoice Id return with null after change using Stripe
var options = new ProductCreateOptions
{
Name = "Starter Setup",
};
var service = new ProductService();
var product = service.Create(options);
var optionsA = new PriceCreateOptions
{
Product = product.Id,
UnitAmount = 2000,
Currency = "usd",
};
var serviceA = new PriceService();
var price = serviceA.Create(optionsA);
var optionsB = new CustomerCreateOptions
{
Email = model.Mail,
Name = model.FuldName,
Source = token
};
var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);
var optionsC = new InvoiceItemCreateOptions
{
Customer = customer.Id,
Price = price.Id,
};
var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);
var invoiceId = invoiceItem.Id;
var serviceE = new InvoiceService();
var f = serviceE.Get(invoiceId);
var pdf = f.InvoicePdf;// This here gives zero.
If I do it this way, I'll get this out of it. I get the Invoice ID that I want here but I get nothing on the invoice that shows that it is zero.
{
"id": "ii_1IR4UtFnB7TvDVRrzPwWo8ZW",
"object": "invoiceitem",
"amount": 2000,
"currency": "usd",
"customer": "cus_J3Aqpyt4PwqCcN",
"date": 1614815575,
"description": "Starter Setup",
"discountable": true,
"discounts": [
],
"invoice": null,
"livemode": false,
"metadata": {
},
....
}
With this, my thinking is whether I will in a way be able to make such that I make a membership which then stops immediately but that it says in the invoice that the purchase is only of a single item and not several months.
The way I have done it in relation to membership I have done like this.
var createCustomer = new CustomerCreateOptions
{
Source = token,
Name = model.FuldName,
Email = model.Mail
};
var addService = new CustomerService();
var customer = addService.Create(createCustomer);
var optionsProduct = new ProductCreateOptions
{
Name = $"Single buy - {DateTime.Now} - Kursus Id : {id}",
Type = "service",
};
var serviceProduct = new ProductService();
Product product = serviceProduct.Create(optionsProduct);
var optionsPlan = new PlanCreateOptions
{
Currency = "dkk",
Interval = Helpers.Stripe.interval,
Nickname =
$"Single buy - {DateTime.Now} - Kursus Id : {id}",
Amount = amount,
Product = product.Id,
IntervalCount = 1
};
var servicePlan = new PlanService();
Plan plan = servicePlan.Create(optionsPlan);
var items = new List<SubscriptionItemOptions>()
{
new SubscriptionItemOptions()
{
Plan = plan.Id,
Quantity = 1
},
};
var createSubscruptionA = new SubscriptionCreateOptions
{
Customer = customer.Id,
Items = items,
OffSession = true,
};
var addserviceA = new SubscriptionService();
Subscription subscription = addserviceA.Create(createSubscruptionA);
var invoiceId = subscription.LatestInvoiceId;
var service = new InvoiceService();
var pdf = service.Get(invoiceId).InvoicePdf;
That which I would like to achieve by this. It is that I can get hold of PDF and Invoice id as I will use it for my system in the future etc.
EDIT
var optionsB = new CustomerCreateOptions
{
Email = model.Mail,
Name = model.FuldName,
Source = token
};
var serviceB = new CustomerService();
var customer = serviceB.Create(optionsB);
var optionsC = new InvoiceItemCreateOptions
{
Customer = customer.Id,
Price = price.Id,
};
var serviceC = new InvoiceItemService();
var invoiceItem = serviceC.Create(optionsC);
var invoiceId = invoiceItem.Id;
var invoiceOptions = new InvoiceCreateOptions
{
Customer = customer.Id,
AutoAdvance = true,
};
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);
For one-off Invoices you need to create Invoice Items for the Customer (as you've done), but importantly you then need to create an Invoice that will contain those Items.
This line is not correct for what you're trying to accomplish:
var invoiceId = invoiceItem.Id;
Instead, you need to create the invoice as shown in the docs linked above:
var invoiceOptions = new InvoiceCreateOptions
{
Customer = "cus_123",
AutoAdvance = true,
};
var invoiceService = new InvoiceService();
var invoice = invoiceService.Create(invoiceOptions);
The Invoice object will have an invoice_pdf URL (docs) after you finalize it.
var service = new InvoiceService();
service.FinalizeInvoice(
"in_123"
);
Related
I try add payment method to my project where customer can buy product from other user. But i have problem because when i use it:
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
Price = "{{PRICE_ID}}",
Quantity = 1,
},
},
Mode = "payment",
SuccessUrl = "https://example.com/success",
CancelUrl = "https://example.com/cancel",
PaymentIntentData = new SessionPaymentIntentDataOptions
{
ApplicationFeeAmount = 123,
},
};
var requestOptions = new RequestOptions
{
StripeAccount = "{{CONNECTED_ACCOUNT_ID}}",
};
var service = new SessionService();
Session session = service.Create(options, requestOptions);
PRICE_ID can't be price's on my main stripe account and i must create product and price on the connected account.
In the case of creating a product on the main account, I do it like this:
var options = new ProductCreateOptions
{
Id = ProductId.ToString(),
Name = "Product_name",
DefaultPriceData = new ProductDefaultPriceDataOptions
{
UnitAmount = price,
Currency = "pln"
},
Expand = new List<string> { "default_price" },
};
_productService.Create(options);
How create product and price on the connected account with my api?
The Stripe API has a Stripe-Account header where you can pass in the ID of one of your connected accounts to make the call as that account. In C# that would look like this:
{
Id = ProductId.ToString(),
Name = "Product_name",
DefaultPriceData = new ProductDefaultPriceDataOptions
{
UnitAmount = price,
Currency = "pln"
},
Expand = new List<string> { "default_price" },
StripeAccount = "acct_123456789",
};
_productService.Create(options);
here is the code:-
static I used its worked fine.. how can I store product dynamically in using asp.net c#
LineItems = new List<SessionLineItemOptions>
{
for (int i = 0; i < dtOrder.Rows.Count; i++){
new SessionLineItemOptions
{
Name=dtOrder.Rows[i]["ProductName"].toString(),
Currency="cad",
Amount =Convert.toInt64(dtOrder>Rows[i]["Price"])*100,
Quantity = 1,
},
}
},
Extending the snippet shown in the API reference here, we can replace Price = 'price_123' with PriceData (API ref) like so:
var options = new SessionCreateOptions
{
SuccessUrl = "https://example.com/success",
CancelUrl = "https://example.com/cancel",
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
PriceData = new SessionLineItemPriceDataOptions
{
Currency = "usd",
UnitAmount = 50000,
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Name = "some product name",
}
},
Quantity = 2,
},
},
Mode = "payment",
};
var service = new SessionService();
service.Create(options);
You can find all the type definitions in the source code.
I integrated Mulitple Account Payment of stripe and fixed the issue like this
and its working for me now , you can also use simple checkout method like this fetching dynamically product from db
List lineItemsOptions = new List();
string cmdText = "select * from tableorder where sessionid='"+ sessionid + "'";
DataSet ds = dbm.getDs(cmdText);
foreach (DataRow row in ds.Tables[0].Rows)
{
var currentLineItem = new SessionLineItemOptions
{
Name = row["ProductName"].ToString(),
Amount = 100,//Convert.ToInt32( row["variationPrice"].ToString()),
Currency = "usd",
Quantity = Convert.ToInt32(row["Quantity"].ToString()),
};
lineItemsOptions.Add(currentLineItem);
}
StripeConfiguration.ApiKey = ".......................................";
var options = new SessionCreateOptions();
options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = lineItemsOptions,
PaymentIntentData = new SessionPaymentIntentDataOptions
{
ApplicationFeeAmount = 1,
},
Mode = "payment",
SuccessUrl = "https://www.......com/success.aspx",
CancelUrl = "https://example.com/cancel",
};
var requestOptions = new RequestOptions
{
StripeAccount = ".............",
};
var service = new SessionService();
Session session = service.Create(options, requestOptions);
Right now I am trying to create an invoice once the purchase has been reviewed.
It is the case that when the customer has bought the membership, I want invoice id to be assigned to my database in relation to the webhook being able to assign pdf invoice to the database with the link.
When I try to make invoice to last in the code. Then I am unfortunately met with this error.
Nothing to invoice for subscription
Stripe Error link
When I look at Stripe logs this comes from errors.
{
"error": {
"code": "invoice_no_subscription_line_items",
"doc_url": "https://stripe.com/docs/error-codes/invoice-no-subscription-line-items",
"message": "Nothing to invoice for subscription",
"param": "subscription",
"type": "invalid_request_error"
}
}
Thus, my Request POST body is like this:
{
"customer": "cus_J2ltIOWhr2BSGX",
"subscription": "sub_J2lto4EVvcfToq"
}
And here can u see how i post request to stripe.
var createCustomer = new CustomerCreateOptions
{
Source = token,
Name = model.FuldName,
Email = model.Mail
};
var addService = new CustomerService();
var customer = await addService.CreateAsync(createCustomer);
var optionsProduct = new ProductCreateOptions
{
Name = $"Membership - {DateTime.Now}",
Type = "service",
};
var serviceProduct = new ProductService();
Product product = await serviceProduct.CreateAsync(optionsProduct);
var optionsA = new PriceCreateOptions
{
UnitAmount = amount,
Currency = "dkk",
Recurring = new PriceRecurringOptions
{
Interval = Helpers.Stripe.interval,
},
Product = product.Id,
};
var serviceA = new PriceService();
var price = await serviceA.CreateAsync(optionsA);
var options = new SubscriptionCreateOptions
{
Customer = customer.Id,
Items = new List<SubscriptionItemOptions>
{
new SubscriptionItemOptions
{
Price = price.Id,
},
},
};
var service = new SubscriptionService();
var subscription = await service.CreateAsync(options);
var optionsI = new InvoiceCreateOptions
{
Customer = customer.Id,
Subscription = subscription.Id
};
var serviceI = new InvoiceService();
var invoice = await serviceI.CreateAsync(optionsI);//I NEED INVOICE ID FROM HERE!
I have tried to look here.
Adding shipping to first subscription invoice using stripe
EIDT:
var createCustomer = new CustomerCreateOptions
{
Source = token,
Name = model.FuldName,
Email = model.Mail
};
var addService = new CustomerService();
var customer = addService.Create(createCustomer);
var options = new ChargeCreateOptions
{
Amount = amount,
Currency = "dkk",
Description = $"Købt kursus {DateTime.Now}",
Customer = customer.Id,
};
var service = new ChargeService();
Charge charge = service.Create(options);
If you're using Subscriptions, the Invoices will be generated automatically for you based on the billing cycle.
If you print out the Subscription object you just created, you can find the Invoice ID under latest_invoice.
-- UPDATE ABOUT ONE-TIME PAYMENTS --
Charges and Invoices are two separate concepts in Stripe's API. A Charge is very simple and it's only purpose is to create and make a payment. Invoices are more complicated and have an added functionality (line items, automatic functionality, taxes, etc.) on top of just making a payment. Either one is fine to use, but it really depends on what your business needs are.
If you want to be using Invoices for one-time payments, you will need to change your code to create them (see https://stripe.com/docs/api/invoices/create). Your code currently only working with Charges - these are not part of Stripe's Billing product and will not generate an Invoice automatically.
I am working to create a sales order with a single product added to the sales order detail and attach that to the sales order.
It is throwing me an error and I am wondering if there is a proper way to performing this action?
Thanks!
public void Create(CrmContextCore _crmContext, Guid productId, UserEntityModel currentuser)
{
var detail = new Entity("salesorderdetail");
{
detail["productid"] = new EntityReference("product", productId);
}
var salesorder = new Entity("salesorder");
{
salesorder["accountid"] = new EntityReference("account", currentuser.AccountId);
salesorder["contactid"] = new EntityReference("contact", currentuser.ContactId );
salesorder["emailaddress"] = currentuser.Email;
salesorder["name"] = "DealerPO123";
salesorder["salesorderdetail"] = detail;
}
_crmContext.ServiceContext.AddObject(salesorder);
_crmContext.ServiceContext.SaveChanges();
}
Sample: Set negative prices in opportunities, quotes, and sales orders.
// Create the sales order.
SalesOrder order = new SalesOrder()
{
Name = "Faux Order",
DateFulfilled = new DateTime(2010, 8, 1),
PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName,
_priceListId),
CustomerId = new EntityReference(Account.EntityLogicalName,
_accountId),
FreightAmount = new Money(20.0M)
};
_orderId = _serviceProxy.Create(order);
order.Id = _orderId;
// Add the product to the order with the price overriden with a
// negative value.
SalesOrderDetail orderDetail = new SalesOrderDetail()
{
ProductId = new EntityReference(Product.EntityLogicalName,
_product1Id),
Quantity = 4,
SalesOrderId = order.ToEntityReference(),
IsPriceOverridden = true,
PricePerUnit = new Money(-40.0M),
UoMId = new EntityReference(UoM.EntityLogicalName,
_defaultUnitId)
};
_orderDetailId = _serviceProxy.Create(orderDetail);
That's when i need to use the Stripe API so when i need it, it will go wrong and make mistakes in the Stripe area as you can see here.
i have : v15.6.1 on Stripe.net
Where it goes wrong is here:
planservice.Create(new StripePlanCreateOptions()
to here:
PlanId = abn.PriceValueUnikId };
all the value I get by json eg userid, pric and pricId there is content in them.
[HttpPost]
public IActionResult Post([FromBody] JObject token)
{
var api = Settings.ConstName.StrinpAPIKeyTest;
StripeConfiguration.SetApiKey(api);
var chargeService = new StripeChargeService();
chargeService.ExpandBalanceTransaction = true;
chargeService.ExpandCustomer = true;
chargeService.ExpandInvoice = true;
//StripeCharge stripeCharge = chargeService.Get(api);
var customerSerive = new StripeCustomerService(api);
var subservice = new StripeSubscriptionService(api);
var planservice = new StripePlanService(api);
var pricId = (int)token.GetValue("pricid");
var pric = (int)token.GetValue("pric");
var userid = (int) Userid();
var abn = _dbContext.PriceValue.FirstOrDefault(i => i.PriceValueId == pricId || i.Price == pric);
//Finder information omkring pakken til den enkelte pakke.
var currentUser = _dbContext.Users.FirstOrDefault(i => i.UserId == userid);
if (currentUser != null)
{
if (abn != null)
{
var orderid = Settings.ValueWordsAndNumbers.OrdreValue();//Orderid
var planType = $"OrderId: {orderid} - Pris: {abn.Price} - Mdr: {abn.Months} UserId: {userid}";
planservice.Create(new StripePlanCreateOptions()//error from here
{
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
Id = abn.PriceValueUnikId
});
var newCustomer = new StripeCustomerCreateOptions
{
SourceToken = token["id"].ToString(),
Email = token["email"].ToString(),
PlanId = abn.PriceValueUnikId,
};//error to here
var stripeCustomer = customerSerive.Create(newCustomer);
}
}
var planOptions = new StripePlanCreateOptions() {
Product = new StripePlanProductCreateOptions() {
Name = "planType"
},
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
};
var planService = new StripePlanService();
StripePlan plan = planService.Create(planOptions);
API version to 2018-02-06 and add support for Product & Plan API
Now Product is REQUIRED.
you need past ID product or dictionary containing fields used to create a service product.
var planOptions = new StripePlanCreateOptions() {
ProductId ="Product Plan id",
Amount = int.Parse(abn.Price.ToString()) * 100,
Nickname = planType,
Currency = "dkk",
Interval = "month",
IntervalCount = abn.Months,
};