The Complete Beginner's Guide: Step-by-Step Implementation
If you are looking to build this exact architecture
yourself, here is the complete, beginner-friendly process from start to finish.
Phase 1: Creating Your Stripe Sandbox
Before writing any code, establish a secure environment to
process test payments.
- Navigate
to the official Stripe Dashboard and sign in.
- Locate
the Test Mode toggle switch in the top right-hand corner and ensure
it is switched ON.
- Navigate
to Developers > API Keys.
- Under
the Secret Key section, click Reveal test key. Copy the
token (starting with sk_test_...) and store it securely.
Phase 2: Building the Salesforce Database Architecture
We need to track every request to ensure data integrity and
prevent duplicate payments.
- The
Safety Checkbox: Go to Setup > Object Manager > Opportunity.
Create a new Checkbox field named Payment Completed (Payment_Completed__c).
Leave it unchecked by default.
- The
Transaction Object: Create a Custom Object named Stripe Transaction (Stripe_Transaction__c).
Change the Record Name Data Type to Auto Number with the format TRX-{0000}.
- The
Tracking Fields: On the new Stripe Transaction object, create three
fields:
- Master-Detail
Relationship: Linked to the Opportunity.
- Text
(255): Named Stripe Session ID (Stripe_Session_ID__c).
- Picklist:
Named Status (Status__c) with values: Pending, Completed, and Failed.
Phase 3: High-Security Configuration (Named Credentials)
Hardcoding API keys is a security risk. We will use
Salesforce's modern vault system.
- External
Credential: Go to Setup > Named Credentials > External
Credentials. Create a new one named "Stripe External Credential" using
the custom authentication protocol.
- The
Principal: Under the Principal's section of your new External
Credential, click New. Name it Stripe_Key, set Sequence to 1, and type to Named
Principal. Save it, edit it from the dropdown, and paste your Stripe
Secret Key into the Value box.
- Custom
Header: Under Custom Headers, create a new one named Authorization.
Set the value to Bearer {'$Credential.Stripe_External_Credential. Stripe_Key'}.
- Named
Credential: Go back to the main Named Credentials tab. Create a new
one named Stripe. Set the URL to https://api.stripe.com, link it to your external credential, and uncheck "Generate Authorization
Header."
Phase 4: Granting Database Access
Your code needs explicit permission to access the security
vault.
- Go
to Setup > Permission Sets and create one named Stripe
Integration User.
- Click
External Credential Principal Access and edit it.
- Move
your Stripe_External_Credential - Stripe_Key into the Enabled column and save it.
- Click
Manage Assignments and assign this Permission Set it yourself.
Phase 5: Writing the Backend Code (Apex)
We need two classes to handle the logic and the network
callouts.
1. StripeIntegrationService.cls (Handles the secure
network call)
2. StripePaymentController.cls (Handles the business
logic)
Phase 6: Building the Frontend Interface (LWC)
Create a new LWC bundle named stripePaymentButton.
HTML, JavaScript, XML
Phase 7: Deployment and End-to-End Testing
- Go
to Setup > Object Manager > Opportunity > Buttons, Links, and
Actions.
- Click
New Action, set it to Lightning Web Component, select your c:stripePaymentButton,
label it "Pay with Stripe," and save.
- Add
the button to your Opportunity Page Layout under the Mobile &
Lightning Actions section.
- Navigate
to an active Opportunity record and click Pay with Stripe. The
component will validate the record, secure the key, generate a pending
transaction, and redirect you to the Stripe checkout screen seamlessly!