Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes to match the current API version #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Development Fork of StripeForce - A Stripe SDK for the Force.com Platform
========================================
The groundwork for this library was done by Ryan Huff. Feel free to use my development fork, but please consider helping to improve the original StripeForce by sending back all significant improvements as pull requests. So much said, here comes the original README content...
<p>
<a href="https://githubsfdeploy.herokuapp.com?owner=dstdia&repo=stripeforce">
<img alt="Deploy to Salesforce"
src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/src/main/webapp/resources/img/deploy.png">
</a></p>

Stripe SDK for Force.com
========================================

Expand Down
22 changes: 22 additions & 0 deletions src/classes/StripExceptionTests.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@isTest
private class StripExceptionTests {

@isTest
private static void testStripeException() {
Test.startTest();
StripeError err = new StripeError();
err.code = '123';
err.message = 'void';
err.param = 'none';
err.stripeType = 'Charge';

StripeException ex = new StripeException(err);
String s = ex.getMessage();

StripeError res = ex.getError();

Test.stopTest();
}


}
5 changes: 5 additions & 0 deletions src/classes/StripExceptionTests.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<status>Active</status>
</ApexClass>
133 changes: 68 additions & 65 deletions src/classes/StripeAPI.cls
Original file line number Diff line number Diff line change
@@ -1,69 +1,72 @@
global class StripeAPI {

global static String ApiKey {
get {
Stripe_Settings__c s = Stripe_Settings__c.getInstance();
if (s != null) {
if (StripeAPI.isTest) {
return s.Stripe_Secret_Test_Key__c;
} else {
return s.Stripe_Secret_Live_Key__c;
}
}
return null;
}
}

global static String PublishableKey {
get {
Stripe_Settings__c s = Stripe_Settings__c.getInstance();
if (s != null) {
if (StripeAPI.isTest) {
return s.Stripe_Publishable_Test_Key__c;
} else {
return s.Stripe_Publishable_Live_Key__c;
}
}
return null;
}
}

global static Boolean isWebhookProcessorRunning() {
Stripe_Settings__c s = Stripe_Settings__c.getOrgDefaults();
if (s != null) {
if (s.Delayed_Webhook_Processor_ID__c == null) {
return false;
} else {
System.debug(System.LoggingLevel.INFO, '\n**** stripe settings: '+s);
try {
CronTrigger sched = [Select State, NextFireTime From CronTrigger Where Id =: s.Delayed_Webhook_Processor_ID__c];
if (sched.NextFireTime != null) return true;
} catch (System.QueryException e) {}
}
}
global static String ApiKey {
get {

Stripe_Settings__c s = Stripe_Settings__c.getInstance();

if (s != null) {
if (StripeAPI.isTest) {
return s.Stripe_Secret_Test_Key__c;
} else {
return s.Stripe_Secret_Live_Key__c;
}
}
return null;
}
}

global static String PublishableKey {
get {
Stripe_Settings__c s = Stripe_Settings__c.getInstance();
if (s != null) {
if (StripeAPI.isTest) {
return s.Stripe_Publishable_Test_Key__c;
} else {
return s.Stripe_Publishable_Live_Key__c;
}
}
return null;
}
}

global static Boolean isWebhookProcessorRunning() {
Stripe_Settings__c s = Stripe_Settings__c.getOrgDefaults();
if (s != null) {
if (s.Delayed_Webhook_Processor_ID__c == null) {
return false;
} else {
System.debug(System.LoggingLevel.INFO, '\n**** stripe settings: '+s);
try {
CronTrigger sched = [Select State, NextFireTime From CronTrigger Where Id =: s.Delayed_Webhook_Processor_ID__c];
if (sched.NextFireTime != null) return true;
} catch (System.QueryException e) {}
}
}

return false;
}

global static void startWebhookProcessor() {
if (isWebhookProcessorRunning() == false) {
WebhookDelayedProcessor proc = new WebhookDelayedProcessor();
Id jobId = System.scheduleBatch(proc, 'delayed webhook processor', 5);

Stripe_Settings__c s = Stripe_Settings__c.getOrgDefaults();
s.Delayed_Webhook_Processor_ID__c = jobId;
update s;
}
}

global static Boolean isTest {
get {
Stripe_Settings__c s = Stripe_Settings__c.getInstance();
if (s != null) {
return !s.Is_Live_Environment__c;
}
return true;
}
}

return false;
}

global static void startWebhookProcessor() {
if (isWebhookProcessorRunning() == false) {
WebhookDelayedProcessor proc = new WebhookDelayedProcessor();
Id jobId = System.scheduleBatch(proc, 'delayed webhook processor', 5);

Stripe_Settings__c s = Stripe_Settings__c.getOrgDefaults();
s.Delayed_Webhook_Processor_ID__c = jobId;
update s;
}
}

global static Boolean isTest {
get {
Stripe_Settings__c s = Stripe_Settings__c.getInstance();

if (s != null) {
return !s.Is_Live_Environment__c;
}
return true;
}
}

}
2 changes: 1 addition & 1 deletion src/classes/StripeAPI.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>35.0</apiVersion>
<status>Active</status>
</ApexClass>
39 changes: 39 additions & 0 deletions src/classes/StripeBalance.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
global class StripeBalance {

global String SripeObject;
global StripeTransaction[] available;
global StripeTransaction[] pending;
global Boolean livemode;

global class StripeBalanceTransaction {
global string id;
global string stripeObject;
global integer amount;
global string timestamp;
global string StripeCurrency;
global string description;
global integer fee;
global String[] fee_details;
global integer net;
global string source;
global String[] sourced_transfers; // child attributes
global String status;
global String type;
}

global class stripeTransaction {
global integer amount;
global string stripeCurrency;
}

global class StripeFeeDetails {
global integer amount;
global string StripeCurrency;
global string description;
global string type;
global integer net;
global string source;
global Map<String, String> sourced_transfers; // TODO: Sourced Transfers object
}

}
5 changes: 5 additions & 0 deletions src/classes/StripeBalance.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/StripeCard.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>35.0</apiVersion>
<status>Active</status>
</ApexClass>
9 changes: 9 additions & 0 deletions src/classes/StripeCardTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ public class StripeCardTests {
'}';
}
}

@isTest
public static void createStripeCardTest () {


Test.startTest();
StripeCard.create('cus_1234567abcdef', 'tok_8901234ghijk');
Test.stopTest();
}
}
2 changes: 1 addition & 1 deletion src/classes/StripeCardTests.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>28.0</apiVersion>
<apiVersion>31.0</apiVersion>
<status>Active</status>
</ApexClass>
90 changes: 63 additions & 27 deletions src/classes/StripeCharge.cls
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
global class StripeCharge {
private static final String SERVICE_URL = 'https://api.stripe.com/v1/charges';

global Integer amount;
global Integer created;
global String stripeCurrency;
global String customer_id;
global Integer fee; // legacy
global String api_version; // legacy
global StripeCard card; // legacy
global List<Fee_details> fee_details; // legacy
global StripeError error; //legacy

global String id; //
global String stripeObject;
global Integer amount;
global Integer amount_refunded;
global String application_fee;
global String balance_transaction;
global Boolean captured;
global Integer created;
global String stripeCurrency;
global String customer_id; // legacy
global StripeCustomer customer;
global String description;
global Boolean disputed;
global Integer fee;
global String id;
global Boolean livemode;
global String stripeObject;
global Boolean paid;
global Boolean refunded;
global Integer amount_refunded;
global StripeCard card;
global List<Fee_details> fee_details;
global StripeError error;
global String description;
global String destination;
global StripeDispute dispute;
global Boolean disputed; // legacy
global String failure_code;
global String failure_message;
global Map<String, String> fraud_details;
global String invoice_id;
global StripeInvoice invoice;
global String failure_message;
global Boolean livemode;
global Map<String, String> metadata;

global String order;
global Boolean paid;
global String receipt_email;
global String receipt_number;
global Boolean refunded;
global Refunds refunds;
global Shipping shipping;
global StripeCard source;
global String statement_descriptor;
global String status;
global String transfer;

// This has changed in the 2013-02-13 API
// https://stripe.com/docs/upgrades#2013-02-13
// And more recently on 2013-08-13
// https://stripe.com/docs/upgrades#2013-08-13

global class Fee_details {
global String stripeType;
global String description;
Expand All @@ -35,6 +54,20 @@ global class StripeCharge {
global String stripeCurrency;
}

global class Shipping {

}

global class StripeDispute {

}

global class Refunds {

}



global static StripeCharge getCharge(String chargeId) {
return StripeCharge.getCharge(chargeId, false);
}
Expand Down Expand Up @@ -104,23 +137,17 @@ global class StripeCharge {
}

global static StripeCharge create(String customerId, Decimal amount, Map<String, String> properties, Map<String, String> metadata) {
HttpRequest http = new HttpRequest();
http.setEndpoint(SERVICE_URL);
http.setMethod('POST');
Blob headerValue = Blob.valueOf(StripeAPI.ApiKey + ':');
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
http.setHeader('Authorization', authorizationHeader);

Integer amount_int = Math.round(amount * 100);//.intValue();

Map<String, String> payload = new Map<String, String>{
'customer' => customerId,
'amount' => String.valueOf(amount_int)
};

// set the default currency to USD if it's not specified
if (properties == null || properties.containsKey('usd') == false) {
payload.put('currency', 'usd');
payload.put('currency', 'EUR');
}

if (properties != null) {
Expand All @@ -139,7 +166,16 @@ global class StripeCharge {
}
}

http.setBody(StripeUtil.urlify(payload));
String params = StripeUtil.urlify(payload);

HttpRequest http = new HttpRequest();
http.setEndpoint(SERVICE_URL + params);
http.setMethod('POST');
Blob headerValue = Blob.valueOf(StripeAPI.ApiKey + ':');
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
http.setHeader('Authorization', authorizationHeader);

System.debug(System.LoggingLevel.INFO, '\n**** REQUEST BODY:\n'+http.getBody());

String response;
Expand Down
Loading