Delphi DLL Stripe: Attach a Source

Back to Index

Attaches a Source object to a Customer. The source must be in a chargeable state.

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

CURL Command

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

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
strResponseBody: PWideChar;
jsonResponse: HCkJsonObject;
id: PWideChar;
object: PWideChar;
amount: Integer;
client_secret: PWideChar;
created: Integer;
currency: PWideChar;
flow: PWideChar;
livemode: Boolean;
ownerAddress: Boolean;
ownerEmail: PWideChar;
ownerName: Boolean;
ownerPhone: Boolean;
ownerVerified_address: Boolean;
ownerVerified_email: Boolean;
ownerVerified_name: Boolean;
ownerVerified_phone: Boolean;
receiverAddress: PWideChar;
receiverAmount_charged: Integer;
receiverAmount_received: Integer;
receiverAmount_returned: Integer;
receiverRefund_attributes_method: PWideChar;
receiverRefund_attributes_status: PWideChar;
statement_descriptor: Boolean;
status: PWideChar;
type: PWideChar;
usage: PWideChar;
bitcoinAddress: PWideChar;
bitcoinAmount: Integer;
bitcoinAmount_charged: Integer;
bitcoinAmount_received: Integer;
bitcoinAmount_returned: Integer;
bitcoinUri: PWideChar;
customer: PWideChar;

begin
rest := CkRest_Create();

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

CkRest_SetAuthBasic(rest,'STRIPE_SECRET_KEY','');

CkRest_AddQueryParam(rest,'source','src_1BnETLGswQrCoh0X5KOorXEP');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/v1/customers/cus_CBbg9PmQ9sLbmo/sources');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

id := CkJsonObject__stringOf(jsonResponse,'id');
object := CkJsonObject__stringOf(jsonResponse,'object');
amount := CkJsonObject_IntOf(jsonResponse,'amount');
client_secret := CkJsonObject__stringOf(jsonResponse,'client_secret');
created := CkJsonObject_IntOf(jsonResponse,'created');
currency := CkJsonObject__stringOf(jsonResponse,'currency');
flow := CkJsonObject__stringOf(jsonResponse,'flow');
livemode := CkJsonObject_BoolOf(jsonResponse,'livemode');
ownerAddress := CkJsonObject_IsNullOf(jsonResponse,'owner.address');
ownerEmail := CkJsonObject__stringOf(jsonResponse,'owner.email');
ownerName := CkJsonObject_IsNullOf(jsonResponse,'owner.name');
ownerPhone := CkJsonObject_IsNullOf(jsonResponse,'owner.phone');
ownerVerified_address := CkJsonObject_IsNullOf(jsonResponse,'owner.verified_address');
ownerVerified_email := CkJsonObject_IsNullOf(jsonResponse,'owner.verified_email');
ownerVerified_name := CkJsonObject_IsNullOf(jsonResponse,'owner.verified_name');
ownerVerified_phone := CkJsonObject_IsNullOf(jsonResponse,'owner.verified_phone');
receiverAddress := CkJsonObject__stringOf(jsonResponse,'receiver.address');
receiverAmount_charged := CkJsonObject_IntOf(jsonResponse,'receiver.amount_charged');
receiverAmount_received := CkJsonObject_IntOf(jsonResponse,'receiver.amount_received');
receiverAmount_returned := CkJsonObject_IntOf(jsonResponse,'receiver.amount_returned');
receiverRefund_attributes_method := CkJsonObject__stringOf(jsonResponse,'receiver.refund_attributes_method');
receiverRefund_attributes_status := CkJsonObject__stringOf(jsonResponse,'receiver.refund_attributes_status');
statement_descriptor := CkJsonObject_IsNullOf(jsonResponse,'statement_descriptor');
status := CkJsonObject__stringOf(jsonResponse,'status');
type := CkJsonObject__stringOf(jsonResponse,'type');
usage := CkJsonObject__stringOf(jsonResponse,'usage');
bitcoinAddress := CkJsonObject__stringOf(jsonResponse,'bitcoin.address');
bitcoinAmount := CkJsonObject_IntOf(jsonResponse,'bitcoin.amount');
bitcoinAmount_charged := CkJsonObject_IntOf(jsonResponse,'bitcoin.amount_charged');
bitcoinAmount_received := CkJsonObject_IntOf(jsonResponse,'bitcoin.amount_received');
bitcoinAmount_returned := CkJsonObject_IntOf(jsonResponse,'bitcoin.amount_returned');
bitcoinUri := CkJsonObject__stringOf(jsonResponse,'bitcoin.uri');
customer := CkJsonObject__stringOf(jsonResponse,'customer');

CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "id": "src_1BnETLGswQrCoh0X5KOorXEP",
  "object": "source",
  "amount": 1000,
  "client_secret": "src_client_secret_CBbgdvc1AflN5jN0wQevA2jA",
  "created": 1516662783,
  "currency": "usd",
  "flow": "receiver",
  "livemode": false,
  "metadata": {},
  "owner": {
    "address": null,
    "email": "jenny.rosen@example.com",
    "name": null,
    "phone": null,
    "verified_address": null,
    "verified_email": null,
    "verified_name": null,
    "verified_phone": null
  },
  "receiver": {
    "address": "test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N",
    "amount_charged": 0,
    "amount_received": 0,
    "amount_returned": 0,
    "refund_attributes_method": "email",
    "refund_attributes_status": "missing"
  },
  "statement_descriptor": null,
  "status": "chargeable",
  "type": "bitcoin",
  "usage": "single_use",
  "bitcoin": {
    "address": "test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N",
    "amount": 2371000,
    "amount_charged": 0,
    "amount_received": 0,
    "amount_returned": 0,
    "uri": "bitcoin:test_1MBhWS3uv4ynCfQXF3xQjJkzFPukr4K56N?amount=0.02371000"
  },
  "customer": "cus_CBbg9PmQ9sLbmo"
}