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

sync payments data #5

Open
wants to merge 1 commit 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
68 changes: 68 additions & 0 deletions src/classes/FreshbooksAPI.cls
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,76 @@ public with sharing class FreshbooksAPI {
System.assert(te.Freshbooks_Time_Entry_ID__c == '123');
}


// --- payments

public static Freshbooks_Payment__c[] getPayments(){
return getPayments(null);
}
public static Freshbooks_Payment__c[] getPayments(Map<String,String> filter){
List<Freshbooks_Payment__c> toReturn = new List<Freshbooks_Payment__c>();
Integer lastBatchSize = LIST_PAGE_COUNT_MAX;
Integer pageNumber = 1;
while(lastBatchSize == LIST_PAGE_COUNT_MAX){
Freshbooks_Payment__c[] batch = getPayments(pageNumber,filter);
pageNumber++;
lastBatchSize = batch.size();
toReturn.addAll(batch);
}
return toReturn;
}
public static final String TEST_GET_PAYMENTS_XML = '<?xml version="1.0" encoding="utf-8"?><response xmlns="http://www.freshbooks.com/api/" status="ok"><payments page="1" per_page="10" pages="23" total="221"><payment><payment_id>123</payment_id><invoice_id>123</invoice_id><date>2007-03-02 12:04:11</date><type>VISA</type><notes></notes><client_id>43</client_id><currency_code>CAD</currency_code><updated>2009-08-12 09:00:00</updated><gateway_transaction><reference_id>2156858189</reference_id><gateway_name>Authorize.Net</gateway_name></gateway_transaction><amount>301.21</amount></payment></payments></response>';
public static Freshbooks_Payment__c[] getPayments(Integer pageNumber, Map<String,String> filter){
Dom.Document requestDoc = new Dom.Document();
Dom.XmlNode root = requestDoc.createRootElement('request', null, null);
root.setAttribute('method', 'payment.list');
root.addChildElement('page', null, null).addTextNode(String.valueOf(pageNumber));
if(filter != null && !filter.isEmpty())
for(String k : filter.keySet())
root.addChildElement(k,null,null).addTextNode(filter.get(k));
if(filter == null || !filter.keySet().contains('per_page'))
root.addChildElement('per_page',null,null).addTextNode(String.valueOf(LIST_PAGE_COUNT_MAX));
Dom.Document responseDoc = ( inTest == true ? CambridgeCloudPartnersREST.domify(TEST_GET_TIME_ENTRIES_XML) : post(requestDoc) );
checkError(responseDoc,'Error occured when getting time entries');
Dom.XmlNode responseRoot = responseDoc.getRootElement();
Dom.XmlNode payments = responseRoot.getChildElement('time_entries',FRESHBOOKS_XML_API_NS);
List<Freshbooks_Payment__c> toReturn = new List<Freshbooks_Payment__c>();
if(payments != null && payments.getChildElements() != null && !payments.getChildElements().isEmpty())
for(Dom.Xmlnode payment : payments.getChildElements()){
toReturn.add(xml2Payment(payment));
}
return toReturn;
}

private static Freshbooks_Payment__c xml2Payment(Dom.Xmlnode node){
Freshbooks_Payment__c payment = new Freshbooks_Payment__c();
try{
payment.Name = node.getChildElement('payment_id',FRESHBOOKS_XML_API_NS).getText();
payment.Freshbooks_Payment_ID__c = Integer.valueOf(node.getChildElement('payment_id',FRESHBOOKS_XML_API_NS).getText());
payment.Freshbooks_Invoice__r = new Freshbooks_Invoice__c(Freshbooks_Invoice_ID__c = node.getChildElement('invoice_id',FRESHBOOKS_XML_API_NS).getText());
payment.Date__c = Date.valueOf(node.getChildElement('date',FRESHBOOKS_XML_API_NS).getText());
payment.Type__c = node.getChildElement('type',FRESHBOOKS_XML_API_NS).getText();
payment.Notes__c = node.getChildElement('notes',FRESHBOOKS_XML_API_NS).getText();
payment.Account__r = new Account(Freshbooks_Client_ID__c = node.getChildElement('client_id',FRESHBOOKS_XML_API_NS).getText());
payment.Currency_Code__c = node.getChildElement('currency_code',FRESHBOOKS_XML_API_NS).getText();
payment.Amount__c = Double.valueOf(node.getChildElement('amount',FRESHBOOKS_XML_API_NS).getText());
}catch(Exception e){
throw new FreshbooksException('Error parsing payment information response from Freshbooks: '+e.getMessage()+node);
}
return payment;
}

@isTest public static void test_xml2Payment(){
inTest = true;
Dom.Document doc = new Dom.Document();
doc.load(TEST_GET_PAYMENTS_XML);
Freshbooks_Payment__c p = xml2Payment(doc.getRootElement().getChildElement('payments',FRESHBOOKS_XML_API_NS).getChildElement('payment',FRESHBOOKS_XML_API_NS));
System.assertEquals('123', p.Name);
System.assertEquals(123, p.Freshbooks_Payment_ID__c);
}


// --- helpers

/* handle later when we can do a describe on the data types
private void zipperXmlToSObject(Dom.XmlNode node, Map<String,String> mappings, SObject sobj){
Expand Down
13 changes: 13 additions & 0 deletions src/classes/FreshbooksSyncBatch.cls
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ global class FreshbooksSyncBatch implements Schedulable, Database.Batchable<Inte
if(invoices.size() == FreshbooksAPI.LIST_PAGE_COUNT_MAX)
taskQueue.add(new FreshbooksSyncTask('invoices_by_client',t.id,t.page+1));
upsert invoices Freshbooks_Invoice_ID__c;
}else if(t.action.equals('payments_by_client')){
Freshbooks_Payment__c[] payments;
try{
payments = FreshbooksAPI.getPayments(t.page, new Map<String,String>{'client_id'=>t.id});
}catch(Exception e){
requeue(t, e);
continue;
}
if(payments.size() == FreshbooksAPI.LIST_PAGE_COUNT_MAX)
taskQueue.add(new FreshbooksSyncTask('payments_by_client',t.id,t.page+1));
upsert payments Freshbooks_Payment_ID__c;
}
}
}
Expand All @@ -137,6 +148,8 @@ global class FreshbooksSyncBatch implements Schedulable, Database.Batchable<Inte
controller.execute(null, new List<Integer>{0});
controller.taskQueue = new List<FreshbooksSyncTask>{new FreshbooksSyncTask('invoices_by_client', '123', 0)};
controller.execute(null, new List<Integer>{0});
controller.taskQueue = new List<FreshbooksSyncTask>{new FreshbooksSyncTask('payments_by_client', '123', 0)};
controller.execute(null, new List<Integer>{0});
}

global void finish(Database.BatchableContext BC){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<layoutColumns/>
<style>CustomLinks</style>
</layoutSections>
<relatedLists>
<fields>NAME</fields>
<relatedList>Freshbooks_Payment__c.Freshbooks_Invoice__c</relatedList>
</relatedLists>
<showEmailCheckbox>false</showEmailCheckbox>
<showHighlightsPanel>true</showHighlightsPanel>
<showInteractionLogPanel>true</showInteractionLogPanel>
Expand Down
87 changes: 87 additions & 0 deletions src/layouts/Freshbooks_Payment__c-Freshbooks Payment Layout.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<Layout xmlns="http://soap.sforce.com/2006/04/metadata">
<layoutSections>
<customLabel>false</customLabel>
<detailHeading>false</detailHeading>
<editHeading>true</editHeading>
<label>Information</label>
<layoutColumns>
<layoutItems>
<behavior>Required</behavior>
<field>Name</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Freshbooks_Invoice__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Account__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Date__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Type__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Notes__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Currency_Code__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Amount__c</field>
</layoutItems>
<layoutItems>
<behavior>Edit</behavior>
<field>Freshbooks_Payment_ID__c</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
<behavior>Edit</behavior>
<field>OwnerId</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
<layoutSections>
<customLabel>false</customLabel>
<detailHeading>false</detailHeading>
<editHeading>true</editHeading>
<label>System Information</label>
<layoutColumns>
<layoutItems>
<behavior>Readonly</behavior>
<field>CreatedById</field>
</layoutItems>
</layoutColumns>
<layoutColumns>
<layoutItems>
<behavior>Readonly</behavior>
<field>LastModifiedById</field>
</layoutItems>
</layoutColumns>
<style>TwoColumnsTopToBottom</style>
</layoutSections>
<layoutSections>
<customLabel>false</customLabel>
<detailHeading>false</detailHeading>
<editHeading>true</editHeading>
<layoutColumns/>
<layoutColumns/>
<layoutColumns/>
<style>CustomLinks</style>
</layoutSections>
<showEmailCheckbox>false</showEmailCheckbox>
<showHighlightsPanel>false</showHighlightsPanel>
<showInteractionLogPanel>false</showInteractionLogPanel>
<showRunAssignmentRulesCheckbox>false</showRunAssignmentRulesCheckbox>
<showSubmitAndAttachButton>false</showSubmitAndAttachButton>
</Layout>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading