function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NewDeveeeeerNewDeveeeeer 

Integrate salesforce with Stripe api

The whole task sounds like this:
On Submit store this contact details on Stripe using documentation - https://stripe.com/docs/api
    a. Store the Contact Id field in Metadata property as External Id
    b. Before saving a Contact check if it exists in Stripe using Email field
        If yes, then override the details from the custom modal window
        If no, just create the contact in Stripe
    c. Contact can be saved without Email & Description
    d. Use the Custom settings to get the Credentials (Username)

But now I cannot connect to the stripe:
Apex class ContactManager
global with sharing class ContactManager {
       @AuraEnabled
	public static String postContacts(Contact contact) {
		System.debug(contact);
		String APIToken = 'pk_test_51JQsHOAe94gjKAlrxTp3djgPWrhYrwOZMx6kYdMY9RCaUBhPjcv75Z7EaT9COk47Af3DhknC8ZKzBqZ60bwRqhuy00gwnzVcih';
		String key = StripeAPI.ApiKey;
		System.debug(key);
		Http http = new Http();
		HttpRequest request = new HttpRequest();
		request.setEndpoint('https://api.stripe.com/v1/customers');
		request.setMethod('POST');
		request.setHeader('Authorization', 'Bearer ' + APIToken);
		request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.setBody('{"LastName:" + "' + contact.LastName + '", "Description:"TestCon", "Email":"admin@mail.ru"}');
		HttpResponse response = http.send(request);
		if(response.getStatusCode() != 201) {
			System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
		} else {
			System.debug('body');
			System.debug(response.getBody());
		}
		return response.getStatus();
	}
}
My From for add Contact
<aura:component controller="ContactManager" implements="lightning:actionOverride,flexipage:availableForRecordHome,force:hasRecordId" access="global">
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <aura:attribute name="contacts" type="Contact[]"/>
    <aura:attribute name="errors" type="String[]"/>
    <aura:attribute name="modalContext" type="String" default="New" />
    <aura:attribute name="newContact" type="Contact"
                    default="{'sobjectType':'Contact',
                    'LastName':'',
                    'Email':'',
                    'Description':''}"/>
    <force:recordData aura:id="forceRecord"
                      recordId="{!v.recordId}"
                      targetFields="{!v.newContact}"
                      fields="Id,LastName,Email,Description"
                      mode="EDIT" />
    <div aura:id="saveDialog" role="dialog" tabindex="-1" aria-labelledby="header43" class="slds-modal slds-fade-in-open">
        <div class="slds-modal__container">
            <div class="slds-modal__header">
                <h2 class="slds-text-heading--medium">New Record</h2>
            </div>
            <div class="slds-modal__content slds-p-around--medium slds-grid slds-wrap ">
                <lightning:input aura:id="contactName" value="{!v.newContact.LastName}" name="contactName" label="Last Name" required="true" class="slds-size--1-of-1 slds-p-horizontal_x-small" />
                <lightning:input aura:id="email" value="{!v.newContact.Email}" name="email" label="Email" class="slds-size--1-of-2 slds-p-horizontal_x-small" />
                <lightning:input aura:id="description" value="{!v.newContact.Description}" name="description" label="Description" class="slds-size--1-of-2 slds-p-horizontal_x-small" />
            </div>
            <div class="slds-modal__footer">
                <lightning:button variant="neutral" label="Cancel" onclick="{!c.cancelDialog}"/>
                <lightning:button variant="brand" label="Submit" onclick="{!c.saveRecord}" />
            </div>
        </div>
    </div>
    <div aura:id="overlay" class="slds-backdrop slds-backdrop--open"></div>
</aura:component>

Controller for form:
({
    saveRecord: function (component, event, helper) {
        if (helper.validateForm(component)) {
            let newContact = component.get("v.newContact");
            helper.sendContact(component, newContact);
        }
    }
})

helper:
({
	validateForm: function (component) {
		let valid = true;

		let nameField = component.find('contactName');
		let itemName = nameField.get("v.value");
		if ($A.util.isEmpty(itemName)){
			console.log("Error");
			valid = false;
			component.set("v.errors", [{message:"Item name can't be blank."}]);
		} else {
			component.set("v.errors", null);
		}

		let email = component.find('email');
		let itemEmail = email.get("v.value");
		if ($A.util.isEmpty(itemEmail)){
			console.log("Error");
			valid = false;
			component.set("v.errors", [{message:"Item email can't be blank."}]);
		} else {
			component.set("v.errors", null);
		}

		let description = component.find('description');
		let itemDescription = description.get("v.value");
		if ($A.util.isEmpty(itemDescription)){
			console.log("Error");
			valid = false;
			component.set("v.errors", [{message:"Item description can't be blank."}]);
		} else {
			component.set("v.errors", null);
		}

		return valid;
    },

	sendContact: function (component, newContact) {
		let action = component.get('c.postContacts');
		let jsonNewContact = JSON.stringify(newContact);
		console.log(jsonNewContact);

		action.setParams({'contact':jsonNewContact});
		action.setCallback(this, function (response) {
			console.log(response.getState());
			console.log(response.getReturnValue());
			let state = response.getState();
			if (state === "SUCCESS") {
				let contacts = component.get("v.contacts");
				console.log(response.getReturnValue());
				contacts.push(response.getReturnValue());
				component.set('{v.contacts}', contacts);
				console.log("From server: " + response.getReturnValue());
			} else {
				console.log("Fail");
				console.log(response.getError()[0].message);
			}
		});
		$A.enqueueAction(action);
	}
})

Thanks
John KlokJohn Klok
Hello,

public class StripeCustomer{
public StripeGetResponseValues StripeGetResponseValue{get;set;}
public StripeResponseModel StripeResponseModel{get;set;}
Integer statusCode;
string response;
String CUSTOMER_URL = 'https://api.stripe.com/v1/customers';
String API_KEY = 'SecretToken';
public StripeCustomer(String apikey){
API_KEY=apikey;
StripeGetResponseValue=new StripeGetResponseValues();
StripeResponseModel=new StripeResponseModel();
}
@future(callout=true)
public static void createCustomer(String cnumber,String exp_month,String exp_year,String cvc,String name,String addressLine1,String addressLine2,String zip,String state,String country,String email,String description,string oConId,String oLeadId){
Integer statusCode;
string response;
string sCusId;
Lead oLead = new Lead();
Contact oCon = new Contact();
String CUSTOMER_URL = 'https://api.stripe.com/v1/customers';
String API_KEY = 'SecretToken';
HttpRequest http = new HttpRequest();
http.setEndpoint(CUSTOMER_URL);
http.setMethod('POST');
Blob headerValue = Blob.valueOf(API_KEY + ':');
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
http.setHeader('Authorization', authorizationHeader);
http.setBody('email='+email+'&description='+description+'&source=tok_visa');
if(!Test.isRunningTest()){
Http con = new Http();
HttpResponse hs = con.send(http);
system.debug('#### '+ hs.getBody());
response = hs.getBody();
statusCode=hs.getStatusCode();
system.debug('$$statusCode='+hs.getStatusCode());
system.debug('email==========='+email );
JSONParser parser = JSON.createParser(hs.getBody());
System.JSONToken token;
string text;
while((token = parser.nextToken()) != null) {
// Parse the object
if ((token = parser.getCurrentToken()) != JSONToken.END_OBJECT) {
text = parser.getText();
if (token == JSONToken.FIELD_Name && text == 'id') {
token=parser.nextToken();
if(oCon != null){
if(parser.getText().contains('cus')){
sCusId = parser.getText();
}
}
}
}
}
if(oConId != null){
oCon = [SELECT Id, Customer_Id__c FROM Contact WHERE Email = :email AND Customer_Id__c = null LIMIT 1];
oCon.Customer_Id__c = sCusId ;
update oCon;
}
if(oLeadId != null){
oLead = [SELECT Id, Customer_Id__c FROM Lead WHERE Email = :email AND Customer_Id__c = null LIMIT 1];
oLead.Customer_Id__c = sCusId ;
update oLead ;
}
}else{
statusCode=200;
response='"created": 1317825831,';
response+='"description": "testing",';
response+='"email": "gamer.sanjay@gmail.com",';
response+='"id": "cus_wCA0LuIzUzRYZp",';
response+='"livemode": false,';
response+='"object": "customer",';
response+='"active_card": {';
response+='"address_country": "India",';
response+='"address_line1": "add1",';
response+='"address_line1_check": "pass",';
response+='"address_line2": "add22",';
response+='"address_state": "Rajasthan",';
response+='"address_zip": "123123",';
response+='"address_zip_check": "pass",';
response+='"country": "US",';
response+='"cvc_check": "pass",';
response+='"exp_month": 12,';
response+='"exp_year": 2013,';
response+='"last4": "4242",';
response+='"name": "sanjay",';
response+='"object": "card",';
response+='"type": "Visa"';
response+='}';
response+='}';
}
if(statusCode!=200){
// StripeResponseModel.errorResponse.code=statusCode;
//StripeResponseModel.errorResponse.message=StripeGetResponseValue.getValue(response,'"message":');
// StripeResponseModel.errorResponse.param =StripeGetResponseValue.getValue(response,'"param":');
//StripeResponseModel.errorResponse.error_type=StripeGetResponseValue.getValue(response,'"type":');
//StripeResponseModel.errorResponse.error_type=StripeGetResponseValue.getLastValue(response,'"type":');
//StripeResponseModel.isError=true;
}else{
/* StripeResponseModel.isError=false;
StripeResponseModel.id=StripeGetResponseValue.getValue(response,'"id":');
StripeResponseModel.rObject=StripeGetResponseValue.getValue(response,'"object":');
StripeResponseModel.description=StripeGetResponseValue.getValue(response,'"description":');
StripeResponseModel.livemode=StripeGetResponseValue.getNumValue(response,'"livemode":');
StripeResponseModel.created=StripeGetResponseValue.getNumValue(response,'"created":');
//StripeResponseModel.card.ctype=StripeGetResponseValue.getValue(response,'"type":');
StripeResponseModel.card.ctype=StripeGetResponseValue.getLastValue(response,'"type":');
StripeResponseModel.card.cObject=StripeGetResponseValue.getValue(response,'"object":');
StripeResponseModel.card.country =StripeGetResponseValue.getValue(response,'"country":');
StripeResponseModel.card.exp_month=StripeGetResponseValue.getNumValue(response,'"exp_month":');
StripeResponseModel.card.last4 =StripeGetResponseValue.getValue(response,'"last4":');
StripeResponseModel.card.exp_year=StripeGetResponseValue.getNumValue(response,'"exp_year":'); */
}
// system.debug('**StripeResponseModel='+StripeResponseModel);
//return StripeResponseModel;
}


public StripeResponseModel retrieveCustomer(String customerId){
HttpRequest http = new HttpRequest();
String mainUrl=CUSTOMER_URL+'/'+customerId;
http.setEndpoint(mainUrl);
http.setMethod('GET');
Blob headerValue = Blob.valueOf(API_KEY + ':');
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
http.setHeader('Authorization', authorizationHeader);
if(!Test.isRunningTest()){
Http con = new Http();
HttpResponse hs = con.send(http);
system.debug('#### '+ hs.getBody());
response = hs.getBody();
statusCode=hs.getStatusCode();
}
else{
statusCode=200;
response='"created": 1317825831,';
response+='"description": "testing",';
response+='"email": "gamer.sanjay@gmail.com",';
response+='"id": "cus_wCA0LuIzUzRYZp",';
response+='"livemode": false,';
response+='"object": "customer",';
response+='"active_card": {';
response+='"address_country": "India",';
response+='"address_line1": "add1",';
response+='"address_line1_check": "pass",';
response+='"address_line2": "add22",';
response+='"address_state": "Rajasthan",';
response+='"address_zip": "123123",';
response+='"address_zip_check": "pass",';
response+='"country": "US",';
response+='"cvc_check": "pass",';
response+='"exp_month": 12,';
response+='"exp_year": 2013,';
response+='"last4": "4242",';
response+='"name": "sanjay",';
response+='"object": "card",';
response+='"type": "Visa"';
response+='}';
response+='}';
}
if(statusCode!=200){
StripeResponseModel.errorResponse.code=statusCode;
StripeResponseModel.errorResponse.message=StripeGetResponseValue.getValue(response,'"message":');
StripeResponseModel.errorResponse.param =StripeGetResponseValue.getValue(response,'"param":');
//StripeResponseModel.errorResponse.error_type=StripeGetResponseValue.getValue(response,'"type":');
StripeResponseModel.errorResponse.error_type=StripeGetResponseValue.getLastValue(response,'"type":');
StripeResponseModel.isError=true;
}else{
StripeResponseModel.isError=false;
StripeResponseModel.id=StripeGetResponseValue.getValue(response,'"id":');
StripeResponseModel.rObject=StripeGetResponseValue.getValue(response,'"object":');
StripeResponseModel.description=StripeGetResponseValue.getValue(response,'"description":');
StripeResponseModel.livemode=StripeGetResponseValue.getNumValue(response,'"livemode":');
StripeResponseModel.created=StripeGetResponseValue.getNumValue(response,'"created":');
//StripeResponseModel.card.ctype=StripeGetResponseValue.getValue(response,'"type":');
StripeResponseModel.card.ctype=StripeGetResponseValue.getLastValue(response,'"type":');
StripeResponseModel.card.cObject=StripeGetResponseValue.getValue(response,'"object":');
StripeResponseModel.card.country =StripeGetResponseValue.getValue(response,'"country":');
StripeResponseModel.card.exp_month=StripeGetResponseValue.getNumValue(response,'"exp_month":');
StripeResponseModel.card.last4 =StripeGetResponseValue.getValue(response,'"last4":');
StripeResponseModel.card.exp_year=StripeGetResponseValue.getNumValue(response,'"exp_year":');
}
system.debug('**StripeResponseModel='+StripeResponseModel);
return StripeResponseModel;
}
}

Please mark Best Answer if it helps!