Need to pass Amember User Data to Salesforce

Discussion in 'Customization & add-ons' started by tooliedotter, Apr 15, 2015.

  1. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    Hi everyone, I'm working on some custom PHP code that needs to capture newly added members' data and pass some of it to Salesforce. I have the code I need to properly communicate with Salesforce, but I could use some guidance on how to capture the firstname, lastname, etc. from the Signup form.

    I have the API plugin installed, and I've read most of the API documentation. Would sending the new user to a custom Thank You page from the signup form, and processing the variables and Salesforce code there be the easiest way to accomplish this data transfer?

    Toolie
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Toolie,
    The simplest way will be to use hooks.
    For example if you need to execute some code after completed paymet you can use this code in site.php:
    PHP:
    <?php 
    Am_Di
    ::getInstance()->hook->add(Am_Event::PAYMENT_AFTER_INSERT'onPaymentAfterInsert');
    function 
    onPaymentAfterInsert(Am_Event_PaymentAfterInsert $event)
    {
        
    $payment $event->getPayment();
        
    $user $event->getUser();
        
    // Your code here
    }

    ?>
  3. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    Awesome. I seem to remember that there was a way to connect, but I didn't remember what it was called or where to find the documentation. I believe I found it here:

    http://www.amember.com/docs/API/HookManager

    If I get stuck, I'll post more questions. Thanks Alexander!

    Toolie
  4. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    Hi Alexander, the code above expects that a payment will have occurred, and we're using Amember only to control access to free resources. Here is the sequence of events:
    1. User clicks on link to protected resources
    2. User must sign in or sign up for free access (lifetime)
    3. User completes the form, clicks "I accept" on a terms of use agreement
    4. User is redirected to a custom Thank You Page
    I need to capture this user information

    First name
    Last name
    Title
    Company
    Email
    Phone

    when they first sign up, but not on subsequent signups for access to other areas of the site.

    The hook that seems to be applicable to Free Trials is this:

    Code:
    /** Called when an invoice becomes active_recurring or paid, or free trial is started
    *
    *  {@link Invoice Invoice}
    *  : invoice */
    const INVOICE_STARTED = 'invoiceStarted';
    How do I get from that trigger to retrieving the above fields for the person who just added themselves?

    Toolie
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Yes you right ion this situation you should use INVOICE_STARTED hook:
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::INVOICE_STARTED'onInvoiceStarted');
    function 
    onInvoiceStarted(Am_Event $event)
    {
        
    $invoice $event->getInvoice();
        
    $user $invoice->getUser();
        
    // Then you can get all above info from User record for example: 
        // $user->email; 
        //  $user->name_f;
        //  etc... 
        // You can get custom fields the same way: 
        // SQL field: 
        // $user->fieldname;
        // Common field: 
        // $user->data()->get('fieldname');
    }
  6. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    Got it. I've incorporated the code and I'm trying to run a test to see that the trigger is happening by generating an email to myself with the field data. I'd like to first generate an email as part of the trigger for testing purposes, rather than peppering the Sales department's Salesforce account with a bunch of dummy records.

    This site is on an Amazon Web Services (AWS) EC2 instance, and I've configured Amember to use the SMTP transport to their Simple Email Service (SES) using as is required by AWS. Is there an easy way to utilize the built-in Amember email module to send these tests emails to myself? I see some mailing code in the following post, but that's for generating an unsubscribe link.

    http://www.amember.com/forum/thread...e-s-xxxxxx-unsubscribe-code.16568/#post-64290

    Given the following:
    Code:
    $firstname = $user->name_f;
    $lastname = $user->name_l;
    $email = $user->email;
    $company = $user->company;
    $title = $user->title;
    $phone = $user->phone;
    How would I construct an email to myself as Admin that contains the field data, as proof that the trigger is working?

    Toolie
    Last edited: Apr 17, 2015
  7. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Here is how you can send mail from your code:
    PHP:
        $mail = new Am_Mail();
        
    $mail->addTo('email@address.com');
        
    $mail->setSubject('Subject');
        
    $mail->setBodyText('Body of the message');
        
    $mail->send();

    Message will be sent using default transport configured in amember CP -> Setup -> Email
  8. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    Awesome, that worked beautifully. Now one, more request from the Sales Department: they want to have me include what I have deduced to be the 'item_title' from the $invoice variable. It looks like the information would be somewhere in the array, but I am not clear how to navigate the JSON to get to it. In this situation, we sell only one product per form, but I suspect I'm still going to have to address the array.

    I looked around to find an example of how to retrieve it, but came up empty. Can you suggest how to proceed?
  9. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    I went trolling through the Amember files themselves and found the one line that made it all work:

    $invoice = $event->getInvoice();
    $selected = $invoice->getItem(0)->item_title;

    This pulls out just the first item (in our case there's only ONE item per invoice) and provides the title to me.
  10. tooliedotter

    tooliedotter Member

    Joined:
    Apr 21, 2009
    Messages:
    59
    For everyone who has been following this thread, here's my complete solution:

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::INVOICE_STARTED'onInvoiceStarted');
    function 
    onInvoiceStarted(Am_Event $event)
    {
        
    $invoice $event->getInvoice();
        
    $selected $invoice->getItem(0)->item_title;   // that's item zero in the array
        
        
    $user $invoice->getUser();
       
        
    $firstname $user->name_f;
        
    $lastname $user->name_l;
        
    $email $user->email;
        
    $company $user->data()->get('company');  // company is a custom field
        
    $title $user->data()->get('title');    // title is a custom field
        
    $phone $user->phone;

        
    $oid "your_salesforce_id_here";
       
        
    $data = array(
            
    "oid" => $oid,
            
    "first_name" => $firstname,
            
    "last_name" => $lastname,
            
    "title" => $title,
            
    "company" =>$company,
            
    "email" => $email,
            
    "phone" => $phone,
            
    "lead_source" => "your_lead_source_here",
            
    "devproductdownload__c" => $selected  // This is a custom field I added in Salesforce
        
    );

    /********************************************
    * Use cURL to post to Salesforce web to lead
    ********************************************/

        // Create a new cURL resource
        
    $ch curl_init();   

        
    // Set Options   
        // Point to the Salesforce Web to Lead page
        
    curl_setopt($chCURLOPT_URL"https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8");
       
        
    // Set the method to POST
        
    curl_setopt($chCURLOPT_POST1);
       
        
    // Pass POST data
        
    curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($data));
       
        
    curl_exec($ch); // Post to Salesforce
       
        
    curl_close($ch); // close cURL resource
       
    /********************************************
    * Send email with values to Amember Admin
    ********************************************/

        
    $mail = new Am_Mail();
        
    $mail->addTo('me@email.com');
        
    $mail->setSubject('User data collected for Salesforce');
        
    $mail->setBodyText('The following user data was acquired.'."\r\n"
        
    "Name: "$firstname." ".$lastname."\r\n"
        
    "Title: "$title."\r\n"
        
    "Company: "$company."\r\n"
        
    "Phone: "$phone."\r\n"
        
    "Email: "$email."\r\n"
        
    "Product Selected: "$selected);
        
    $mail->send();
    }
    ?>
    alexander likes this.

Share This Page