3

I installed Payment360 and I am trying to capture a transaction from a trigger. Payment360 offers the following documentation: payment360 documentation

Through trial and error I see that i need my trigger to call a future method which then calls the capture() method from the installed payment360 package. I had to do it this way because callouts from triggers are not allowed.

I have the below code for testing using ID=a0I46000000jg6c, but ultimately i want to use the ID from the record that initiates the trigger.

  1. How would I do this?
  2. I don't think my current code is working, but I get no errors.

Apex Class:

global class FutureClass
{
    @future
    public static void captureFuture()
    {  

         try {
    bt_stripe.P360_API_v1.Tra t = bt_stripe.P360_API_v1.transactionFactory('a0I46000000jg6c');
    t.capture();

    //bt_stripe.P360_API_v1.P360_Exception if something goes wrong with committing records in database
    bt_stripe.P360_API_v1.commitWork();
} catch (Exception ex) {
    //do error handling here
}

    }
}

Trigger:

trigger CaptureTrans on bt_stripe__Transaction__c (after insert, after update) {

    for (bt_stripe__Transaction__c pt : Trigger.new) {

    if (pt.GameSet__c) {

        FutureClass.captureFuture();

    }
}

}
2
  • A future method will not throw an error in your trigger. You will have to debug it. Look in the apex jobs list for exceptions, etc. If you want to know immediatly if there is an error while testing, use Exec Anon and copy your code there and execute it instead of in the future method.
    – Eric
    Commented May 8, 2017 at 21:50
  • thanks @Eric, I checked out the apex jobs list and saw that the future job was completed. So it does seem that the trigger is calling the future job as expected. But the future job is not performing the capture as expected.
    – fandresu
    Commented May 8, 2017 at 22:29

2 Answers 2

6

The first thing to do is remove the current try/catch block you have in captureFuture if it is just a comment of //do error handling here as you show in the question. It will be catching and then ignoring any potentially useful details as it is shown. That is of course assuming the method actually appears as it does in the question. If you've got meaningful exception handling then feel free to disregard this step.

Once you know that any exceptions will actually be recorded use the Developer Console to capture the debug logs. You will see two log entries when inserting a bt_stripe__Transaction__c record. One for the trigger transactions, and one a short time later for the future method transaction.

Open the one for the future method and observe what is actually occurring.


Within your trigger, you can pass a list of bt_stripe__Transaction__c Id's to the future method. Ideally the transactionFactory method will also be able to work with a collection of Id's rather than one at a time. As a guess you might only need a single call to commitWork() for all the IDs.

1
  • 3
    Haha yeah. //do error handling here is so nefarious for something a naive programmer might assume innocent.
    – Adrian Larson
    Commented May 8, 2017 at 22:29
2

I'm Peter, the lead developer of the P360 app. The previous answer is correct, so you need to remove the try-catch block, or re-throw the exception, if captured. Actually, the exception which you are interested in is called bt_stripe.P360_API_v1.P360_Exception

As for multiple transactions -- you can't fed a list of Id's to the TransactionFactory, however you can call it multiple times, and can run the capture() method on all the created Transaction instances.

Finally, you should run the bt_stripe.P360_API_v1.commitWork() method only once (at the very end).

Other things to check:

  • Make sure that your Transactions are in 'Open' Transaction Status
  • Make sure that your Transactions have a valid Payment Method, Amount and Currency
3
  • Hi @PeterTempfli, thank you for your response. I removed the try-catch block and got it working, though i see an error in the apex log (System.AsyncException: Future method cannot be called from a future ...). But I see the transaction captured in stripe. No confirmation inside of teamwork though (no change to the transaction record at all in teamwork). What is meant by re-throw the exception. Sorry, novice programmer.
    – fandresu
    Commented May 9, 2017 at 12:58
  • Here is my current code: global class FutureClass { @future (callout=true) public static void captureFuture() { for (bt_stripe__Transaction__c a : [SELECT Id FROM bt_stripe__Transaction__c WHERE GameSet__c=true]) { bt_stripe.P360_API_v1.Tra t = bt_stripe.P360_API_v1.transactionFactory(a.ID); t.capture(); } bt_stripe.P360_API_v1.commitWork(); } }
    – fandresu
    Commented May 9, 2017 at 13:03
  • Hi @fandresu -- i'd say that you are getting this error because your trigger fires again in the future context. commitWork() touches the Transaction record, which fires your trigger again. Try to add some extra conditions, for example 'bt_stripe__Transaction__c == 'Open' Commented May 11, 2017 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .