1

I'm trying to integrate Salesforce and Stripe using Cirrus Path's Stripe SDK (https://github.com/cirruspath/stripeforce). I've successfully installed all of the Apex classes and other elements of their package. But, now I am trying to figure out how to actually call, presumably, a class which will call their API via REST in a Visualforce page (to create a customer and charge). I've done this through PHP, so I understand what needs to happen behind the scenes, but just haven't done this through Salesforce/the REST api yet.

The class designated as a @RestResource is based on this example:

@RestResource(urlMapping='/createCustomer/*')
global class CustomRestService {

@HttpPost
global static ResponseData createCustomer() {
String json = RestContext.request.requestBody.toString();
RequestPayload payload = (RequestPayload) System.JSON.deserialize(json,    RequestPayload.class);

StripeCustomer customer = StripeCustomer.create(payload.token, payload.description);
StripeCustomer.updateSubscription(customer.id, 'ANNUAL_SUBSCRIPTION');

// Update the existing account to include the Stripe Customer ID
Account a = new Account(
  Id = payload.accountId,
  Stripe_Customer_ID__c = customer.id
);
update a;

ResponseData data = new ResponseData();
data.status = 'success';

return data;
  }

  global class RequestPayload {
  global String token;
  global String description;
  global String accountId;
  }

 global class ResponseData {
 global String status;
 global String errorMessage;
 }
 }

The Visualforce page, containing the Stripe Checkout code is below:

<apex:page controller="CustomRestService">
<form action="https://cs16.salesforce.com/services/apexrest/createCustomer" method="POST">
  <script
    *** stripe checkout script ***
  </script>
</form>

When I submit the form, I'm seeing an error that says there was an invalid session Id. What am I missing? Am I completely confusing myself and going about this the wrong way? Any help would be greatly appreciated!

  • When I use the REST explorer in the workbench and test via POST, it works and I see my debug lines in the logs. I feel like I just need to set the session id somehow. – Jess Lopez Feb 1 '16 at 0:32
  • Where is your salesforce instance hosted? You can find this in your url. e.g. if your url is cs16.salesforce.com, cs16 is your instance. – Salesforcesmarty Feb 1 '16 at 2:27
  • My sandbox is on cs16, which I am using in the form action. I've also tried using a Site and exposing the page/classes that way. I encounter different error via that method: System.JSONException: No content to map to Object due to end of input (System Code) Class.JessRest.createCustomer: line 7, column 1 – Jess Lopez Feb 1 '16 at 2:42
  • When are you invoking this instance of webservice? in the js script? – Salesforcesmarty Feb 1 '16 at 10:58
  • Yes. The form action on the javascript (embedded in the Visualforce page) is pointing to "cs16.salesforce.com/services/apexrest/createCustomer" with a method of POST. The Javascript calls out to Stripe for a token and returns to this URL. I'm guessing that because the javascript calls out to Stripe first and then has to return to Salesforce that it is losing its session ID. – Jess Lopez Feb 1 '16 at 18:15
0

Solved this issue by using a Force.com site and exposing the visualforce page in the site. I then created a Remote site for the Stripe API. RestContext.request.requestBody.toString() wasn't giving me anything. Instead, I found the token in RestContext.request.params. Huge thanks to JimH for pointing this out in his question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.