• NewDeveeeeer
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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