Delphi ActiveX Stripe: Create a Card

Back to Index

Creates a new credit card belonging to a customer.

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

CURL Command

curl https://api.stripe.com/v1/customers/cus_CBbg3iRMzWBjoe/sources \
   -u STRIPE_SECRET_KEY: \
   -d source=tok_amex \
   -X POST

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
strResponseBody: WideString;
jsonResponse: TChilkatJsonObject;
id: WideString;
object: WideString;
address_city: Integer;
address_country: Integer;
address_line1: Integer;
address_line1_check: Integer;
address_line2: Integer;
address_state: Integer;
address_zip: Integer;
address_zip_check: Integer;
brand: WideString;
country: WideString;
customer: WideString;
cvc_check: Integer;
dynamic_last4: Integer;
exp_month: Integer;
exp_year: Integer;
fingerprint: WideString;
funding: WideString;
last4: WideString;
name: Integer;
tokenization_method: Integer;

begin
rest := TChilkatRest.Create(Self);

//  URL: https://api.stripe.com/v1/customers/cus_CBbg3iRMzWBjoe/sources
bTls := 1;
port := 443;
bAutoReconnect := 1;
success := rest.Connect('api.stripe.com',port,bTls,bAutoReconnect);
if (success <> 1) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

rest.SetAuthBasic('STRIPE_SECRET_KEY','');

rest.AddQueryParam('source','tok_amex');

strResponseBody := rest.FullRequestFormUrlEncoded('POST','/v1/customers/cus_CBbg3iRMzWBjoe/sources');
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

jsonResponse := TChilkatJsonObject.Create(Self);
jsonResponse.Load(strResponseBody);

id := jsonResponse.StringOf('id');
object := jsonResponse.StringOf('object');
address_city := jsonResponse.IsNullOf('address_city');
address_country := jsonResponse.IsNullOf('address_country');
address_line1 := jsonResponse.IsNullOf('address_line1');
address_line1_check := jsonResponse.IsNullOf('address_line1_check');
address_line2 := jsonResponse.IsNullOf('address_line2');
address_state := jsonResponse.IsNullOf('address_state');
address_zip := jsonResponse.IsNullOf('address_zip');
address_zip_check := jsonResponse.IsNullOf('address_zip_check');
brand := jsonResponse.StringOf('brand');
country := jsonResponse.StringOf('country');
customer := jsonResponse.StringOf('customer');
cvc_check := jsonResponse.IsNullOf('cvc_check');
dynamic_last4 := jsonResponse.IsNullOf('dynamic_last4');
exp_month := jsonResponse.IntOf('exp_month');
exp_year := jsonResponse.IntOf('exp_year');
fingerprint := jsonResponse.StringOf('fingerprint');
funding := jsonResponse.StringOf('funding');
last4 := jsonResponse.StringOf('last4');
name := jsonResponse.IsNullOf('name');
tokenization_method := jsonResponse.IsNullOf('tokenization_method');

Sample JSON Response Body

{
  "id": "card_1BnETKGswQrCoh0Xhu1A6BfL",
  "object": "card",
  "address_city": null,
  "address_country": null,
  "address_line1": null,
  "address_line1_check": null,
  "address_line2": null,
  "address_state": null,
  "address_zip": null,
  "address_zip_check": null,
  "brand": "Visa",
  "country": "US",
  "customer": "cus_CBbg3iRMzWBjoe",
  "cvc_check": null,
  "dynamic_last4": null,
  "exp_month": 8,
  "exp_year": 2019,
  "fingerprint": "F9mANtIt1TaukpRJ",
  "funding": "credit",
  "last4": "4242",
  "metadata": {},
  "name": null,
  "tokenization_method": null
}