Updating Mautic from aMember

Discussion in 'Customization & add-ons' started by brunopt, Aug 23, 2018.

  1. brunopt

    brunopt aMember Pro Customer

    Joined:
    Mar 19, 2012
    Messages:
    7
    I'm trying to connect aMember to Mautic to do a simple thing: each time a customer purchases a product on aMember, they will be added to a Mautic segment. But somehow it's not working. I'm loading the MauticAPI library for it with basic authentication to make things simpler for now (https://github.com/mautic/api-library)

    The code below works perfectly when done on a standalone php script (outside aMember), it connects to Mautic and updates the segment, but it's not working from inside aMember.

    Any hints on what I'm doing wrong?


    Code:
    include_once __DIR__ . '/vendor/autoload.php'; 
    
    use Mautic\Auth\ApiAuth;
    use Mautic\MauticApi;
    
    Am_Di::getInstance()->hook->add(Am_Event::PAYMENT_AFTER_INSERT, function(Am_Event $e) {
    
        $settings = array(
            'userName'   => 'myusername', 
            'password'   => 'mypassword'  
        );
    
        // Initiate the auth object specifying BasicAuth
        $initAuth = new ApiAuth();
        $auth = $initAuth->newAuth($settings, 'BasicAuth');
    
        $apiUrl   = "http://www.mydomain.com/mautic/";
    
    //start API
        $api = new MauticApi();
        $segmentApi = $api->newApi("segments", $auth, $apiUrl);
        $contactApi = $api->newApi("contacts", $auth, $apiUrl);
    
    //Mautic segmentId & contactId
    $segmentId = 1
    $contactId = 123
    
    //add client to segment
        $response = $segmentApi->addContact($segmentId, $contactId);
    
    });
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    Did you get any error?
  3. brunopt

    brunopt aMember Pro Customer

    Joined:
    Mar 19, 2012
    Messages:
    7
    Hi Caesar,
    No, couldn't see any error anywhere. It just didn't add to the segment. I also tried with other API actions (like creating a contact or getting the contact details), but they also didn't work.
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
  5. brunopt

    brunopt aMember Pro Customer

    Joined:
    Mar 19, 2012
    Messages:
    7
    Will do! Thank you caesar.
  6. johan_sa

    johan_sa aMember Pro Customer

    Joined:
    Aug 22, 2006
    Messages:
    1
    I am trying to use the Mautic plugin which was introduced in aMember 5.5.4. I find no documentation but have followed the instructions on the plugin setup page and entered the correct Mautic credentials. aMember apparently attempts to send a request to my Mautic installation when a new subscriber is added but get an "authorization denied" error in return. Any ideas about what is wrong?

    Here is an entry from the invoice log:

    Code:
    outgoing-request
    <invoice-log-item type="outgoing-request">
    <method>GET</method>
    <url>https://www.*********.com/mautic/api/segments</url>
    <headers>
    <header name="user-agent">aMember PRO/5.5.5 (http://www.amember.com)</header>
    </headers>
    <params/>
    </invoice-log-item>
    
    outgoing-request-response
    <invoice-log-item type="outgoing-request-response"> <status code="401" reason="Unauthorized"/> <headers> <header name="date">Sat, 29 Dec 2018 22:17:14 GMT</header> <header name="server">Apache</header> <header name="cache-control">no-cache</header> <header name="upgrade">h2,h2c</header> <header name="connection">Upgrade, close</header> <header name="transfer-encoding">chunked</header> <header name="content-type">application/json</header> </headers> <body><![CDATA[{"errors":[{"message":"API authorization denied.","code":401,"type":"access_denied"}],"error":"access_denied","error_description":"API authorization denied. (`error` and `error_description` are deprecated as of 2.6.0 and will be removed in 3.0. Use the `errors` array instead.)"}]]></body></invoice-log-item> 
  7. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Did you enter correct login/password in mautic plugin configuration?
    aMember CP -> Configuration -> Setup/Configuration -> Mautic

Share This Page