Objective-C Stripe: Create an Invoice

Back to Index

Creates a customer invoice object.

Documentation: https://stripe.com/docs/api/curl#create_invoice

CURL Command

curl https://api.stripe.com/v1/invoices \
   -u STRIPE_SECRET_KEY: \
   -d customer=cus_CBbg9PmQ9sLbmo \
   -X POST

Objective-C Example

#import <CkoRest.h>
#import <NSString.h>
#import <CkoJsonObject.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//  URL: https://api.stripe.com/v1/invoices
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"api.stripe.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

[rest SetAuthBasic: @"STRIPE_SECRET_KEY" password: @""];

[rest AddQueryParam: @"customer" value: @"cus_CBbg9PmQ9sLbmo"];

NSString *strResponseBody = [rest FullRequestFormUrlEncoded: @"POST" uriPath: @"/v1/invoices"];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse Load: strResponseBody];

NSString *id = 0;
NSString *object = 0;
int amount_due;
BOOL application_fee;
int attempt_count;
BOOL attempted;
NSString *billing = 0;
BOOL charge;
BOOL closed;
NSString *currency = 0;
NSString *customer = 0;
int date;
BOOL description;
BOOL discount;
BOOL due_date;
BOOL ending_balance;
BOOL forgiven;
BOOL linesHas_more;
NSString *linesObject = 0;
NSString *linesUrl = 0;
BOOL livemode;
int next_payment_attempt;
NSString *number = 0;
BOOL paid;
int period_end;
int period_start;
BOOL receipt_number;
int starting_balance;
BOOL statement_descriptor;
BOOL subscription;
int subtotal;
BOOL tax;
BOOL tax_percent;
int total;
BOOL webhooks_delivered_at;
int i;
int count_i;
int amount;
NSString *description_str = 0;
BOOL discountable;
int periodStart;
int periodEnd;
BOOL plan;
BOOL proration;
BOOL quantity;
NSString *type = 0;

id = [jsonResponse StringOf: @"id"];
object = [jsonResponse StringOf: @"object"];
amount_due = [[jsonResponse IntOf: @"amount_due"] intValue];
application_fee = [jsonResponse IsNullOf: @"application_fee"];
attempt_count = [[jsonResponse IntOf: @"attempt_count"] intValue];
attempted = [jsonResponse BoolOf: @"attempted"];
billing = [jsonResponse StringOf: @"billing"];
charge = [jsonResponse IsNullOf: @"charge"];
closed = [jsonResponse BoolOf: @"closed"];
currency = [jsonResponse StringOf: @"currency"];
customer = [jsonResponse StringOf: @"customer"];
date = [[jsonResponse IntOf: @"date"] intValue];
description = [jsonResponse IsNullOf: @"description"];
discount = [jsonResponse IsNullOf: @"discount"];
due_date = [jsonResponse IsNullOf: @"due_date"];
ending_balance = [jsonResponse IsNullOf: @"ending_balance"];
forgiven = [jsonResponse BoolOf: @"forgiven"];
linesHas_more = [jsonResponse BoolOf: @"lines.has_more"];
linesObject = [jsonResponse StringOf: @"lines.object"];
linesUrl = [jsonResponse StringOf: @"lines.url"];
livemode = [jsonResponse BoolOf: @"livemode"];
next_payment_attempt = [[jsonResponse IntOf: @"next_payment_attempt"] intValue];
number = [jsonResponse StringOf: @"number"];
paid = [jsonResponse BoolOf: @"paid"];
period_end = [[jsonResponse IntOf: @"period_end"] intValue];
period_start = [[jsonResponse IntOf: @"period_start"] intValue];
receipt_number = [jsonResponse IsNullOf: @"receipt_number"];
starting_balance = [[jsonResponse IntOf: @"starting_balance"] intValue];
statement_descriptor = [jsonResponse IsNullOf: @"statement_descriptor"];
subscription = [jsonResponse IsNullOf: @"subscription"];
subtotal = [[jsonResponse IntOf: @"subtotal"] intValue];
tax = [jsonResponse IsNullOf: @"tax"];
tax_percent = [jsonResponse IsNullOf: @"tax_percent"];
total = [[jsonResponse IntOf: @"total"] intValue];
webhooks_delivered_at = [jsonResponse IsNullOf: @"webhooks_delivered_at"];
i = 0;
count_i = [[jsonResponse SizeOfArray: @"lines.data"] intValue];
while (i < count_i) {
    jsonResponse.I = [NSNumber numberWithInt: i];
    id = [jsonResponse StringOf: @"lines.data[i].id"];
    object = [jsonResponse StringOf: @"lines.data[i].object"];
    amount = [[jsonResponse IntOf: @"lines.data[i].amount"] intValue];
    currency = [jsonResponse StringOf: @"lines.data[i].currency"];
    description_str = [jsonResponse StringOf: @"lines.data[i].description"];
    discountable = [jsonResponse BoolOf: @"lines.data[i].discountable"];
    livemode = [jsonResponse BoolOf: @"lines.data[i].livemode"];
    periodStart = [[jsonResponse IntOf: @"lines.data[i].period.start"] intValue];
    periodEnd = [[jsonResponse IntOf: @"lines.data[i].period.end"] intValue];
    plan = [jsonResponse IsNullOf: @"lines.data[i].plan"];
    proration = [jsonResponse BoolOf: @"lines.data[i].proration"];
    quantity = [jsonResponse IsNullOf: @"lines.data[i].quantity"];
    subscription = [jsonResponse IsNullOf: @"lines.data[i].subscription"];
    type = [jsonResponse StringOf: @"lines.data[i].type"];
    i = i + 1;
}

Sample JSON Response Body

{
  "id": "in_1BnETLGswQrCoh0XR7p1RCtV",
  "object": "invoice",
  "amount_due": 0,
  "application_fee": null,
  "attempt_count": 0,
  "attempted": false,
  "billing": "charge_automatically",
  "charge": null,
  "closed": false,
  "currency": "usd",
  "customer": "cus_CBbg9PmQ9sLbmo",
  "date": 1516662783,
  "description": null,
  "discount": null,
  "due_date": null,
  "ending_balance": null,
  "forgiven": false,
  "lines": {
    "data": [
      {
        "id": "ii_1BnETLGswQrCoh0XsAcSzoLf",
        "object": "line_item",
        "amount": 0,
        "currency": "usd",
        "description": "My First Invoice Item (created for API docs)",
        "discountable": true,
        "livemode": false,
        "metadata": {},
        "period": {
          "start": 1516662783,
          "end": 1516662783
        },
        "plan": null,
        "proration": false,
        "quantity": null,
        "subscription": null,
        "type": "invoiceitem"
      }
    ],
    "has_more": false,
    "object": "list",
    "url": "/v1/invoices/in_1BnETLGswQrCoh0XR7p1RCtV/lines"
  },
  "livemode": false,
  "metadata": {},
  "next_payment_attempt": 1516666383,
  "number": "36eba31eaa-0001",
  "paid": false,
  "period_end": 1516662783,
  "period_start": 1516662783,
  "receipt_number": null,
  "starting_balance": 0,
  "statement_descriptor": null,
  "subscription": null,
  "subtotal": 0,
  "tax": null,
  "tax_percent": null,
  "total": 0,
  "webhooks_delivered_at": null
}