Objective-C Stripe: Pay an Invoice

Back to Index

Attempts payment on an invoice out of the normal collection schedule.

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

CURL Command

curl https://api.stripe.com/v1/invoices/in_1BnETLGswQrCoh0X6M67Qy9c/pay \
   -u STRIPE_SECRET_KEY: \
   -X POST

Objective-C Example

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

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

//  URL: https://api.stripe.com/v1/invoices/in_1BnETLGswQrCoh0X6M67Qy9c/pay
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: @""];

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"POST" uriPath: @"/v1/invoices/in_1BnETLGswQrCoh0X6M67Qy9c/pay" sb: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoJsonObject *jsonResponse = [[CkoJsonObject alloc] init];
[jsonResponse LoadSb: sbResponseBody];

NSString *id = 0;
NSString *object = 0;
int amount_due;
BOOL application_fee;
int attempt_count;
BOOL attempted;
NSString *billing = 0;
NSString *charge = 0;
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;
BOOL last_payment_attempt;
int i;
int count_i;
int amount;
NSString *description_str = 0;
BOOL discountable;
int periodStart;
int periodEnd;
NSString *planId = 0;
NSString *planObject = 0;
int planAmount;
int planCreated;
NSString *planCurrency = 0;
NSString *planInterval = 0;
int planInterval_count;
BOOL planLivemode;
NSString *planName = 0;
BOOL planStatement_descriptor;
BOOL planTrial_period_days;
BOOL proration;
int quantity;
NSString *subscription_item = 0;
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 StringOf: @"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"];
last_payment_attempt = [jsonResponse IsNullOf: @"last_payment_attempt"];
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];
    planId = [jsonResponse StringOf: @"lines.data[i].plan.id"];
    planObject = [jsonResponse StringOf: @"lines.data[i].plan.object"];
    planAmount = [[jsonResponse IntOf: @"lines.data[i].plan.amount"] intValue];
    planCreated = [[jsonResponse IntOf: @"lines.data[i].plan.created"] intValue];
    planCurrency = [jsonResponse StringOf: @"lines.data[i].plan.currency"];
    planInterval = [jsonResponse StringOf: @"lines.data[i].plan.interval"];
    planInterval_count = [[jsonResponse IntOf: @"lines.data[i].plan.interval_count"] intValue];
    planLivemode = [jsonResponse BoolOf: @"lines.data[i].plan.livemode"];
    planName = [jsonResponse StringOf: @"lines.data[i].plan.name"];
    planStatement_descriptor = [jsonResponse IsNullOf: @"lines.data[i].plan.statement_descriptor"];
    planTrial_period_days = [jsonResponse IsNullOf: @"lines.data[i].plan.trial_period_days"];
    proration = [jsonResponse BoolOf: @"lines.data[i].proration"];
    quantity = [[jsonResponse IntOf: @"lines.data[i].quantity"] intValue];
    subscription = [jsonResponse IsNullOf: @"lines.data[i].subscription"];
    subscription_item = [jsonResponse StringOf: @"lines.data[i].subscription_item"];
    type = [jsonResponse StringOf: @"lines.data[i].type"];
    i = i + 1;
}

Sample JSON Response Body

{
  "id": "in_1BnETLGswQrCoh0X6M67Qy9c",
  "object": "invoice",
  "amount_due": 0,
  "application_fee": null,
  "attempt_count": 0,
  "attempted": true,
  "billing": "charge_automatically",
  "charge": "ch_1BnETLGswQrCoh0XYoMbCKZ3",
  "closed": false,
  "currency": "usd",
  "customer": "cus_CBbggkUyLoz3pe",
  "date": 1516662783,
  "description": null,
  "discount": null,
  "due_date": null,
  "ending_balance": null,
  "forgiven": false,
  "lines": {
    "data": [
      {
        "id": "sub_CBbgC3EaFZqN3Q",
        "object": "line_item",
        "amount": 999,
        "currency": "usd",
        "description": "1x Silver Express (at $9.99)",
        "discountable": true,
        "livemode": true,
        "metadata": {},
        "period": {
          "start": 1521760383,
          "end": 1524438783
        },
        "plan": {
          "id": "gold",
          "object": "plan",
          "amount": 2000,
          "created": 1516662783,
          "currency": "usd",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {},
          "name": "T-shirt",
          "statement_descriptor": null,
          "trial_period_days": null
        },
        "proration": false,
        "quantity": 1,
        "subscription": null,
        "subscription_item": "si_CBbgZxCVF3Ha7P",
        "type": "subscription"
      }
    ],
    "has_more": false,
    "object": "list",
    "url": "/v1/invoices/in_1BnETLGswQrCoh0X6M67Qy9c/lines"
  },
  "livemode": false,
  "metadata": {},
  "next_payment_attempt": 1516666383,
  "number": "330a34b8d5-0001",
  "paid": true,
  "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,
  "last_payment_attempt": null
}