Webhooks

Discussion in 'Integration' started by lhickey, May 25, 2016.

  1. lhickey

    lhickey New Member

    Joined:
    Jan 18, 2016
    Messages:
    2
    Hiya,

    I'm trying to achieve an SMS notification via a HTTP POST URL in web hooks.

    Essentially a url of http://www.coolsmswebsite.com/http-...5678&from=3GFM&to=XXXXXXXXXX&text=Hello world

    How, from the Webhook URL data, can I insert the phone number record where I have inserted (XX) after a userAfterUpdate Event.

    e.g. is it?
    http://www.coolsmswebsite.com/http-...78&from=3GFM&to=%user.phone%&text=Hello world or http://www.coolsmswebsite.com/http-...78&from=3GFM&to=[user.phone]&text=Hello world
    etc.

    Do I need to create something else to do this for me?
    Any advice would be apprciated.
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Instead of webhooks I suggest to use the following code in site.php file
    http://www.amember.com/docs/Site.php_file

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, function(Am_Event $e) {
        
    /* @var $user User */
        
    $user $e->getUser();
        
    $data = array(
            
    'action' => 'sendsms',
            
    'user' => $user->login,
            
    'password' => $user->getPlaintextPass(),
            
    'from' => '3GFM',
            
    'to' => $user->phone,
            
    'text' => 'Hello World'
        
    );

        
    $r = new Am_HttpRequest('http://www.coolsmswebsite.com/http-api.php?' http_build_query($data));
        
    $r->send();
    });
  3. lhickey

    lhickey New Member

    Joined:
    Jan 18, 2016
    Messages:
    2
    Ah, excellent. This works perfectly. Thank you!

    One other thing. I want to pass multiple values in the 'text' = > array.

    E.g. I would like it to say, Hello [firstname], your payment of [$] has been processed.

    How do i do this?
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Do you want to send this notification after payment? In this case I suggest to use another event:
    PHP:
    Am_Event::PAYMENT_AFTER_INSERT
    Here is example of code:
    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::PAYMENT_AFTER_INSERT, function(Am_Event $e) {
        
    /* @var $user User */
        
    $user $e->getUser();
        
    /* @var $payment InvoicePayment */
        
    $payment $e->getPayment();
        
    $data = array(
            
    'action' => 'sendsms',
            
    'user' => 'user1234',
            
    'password' => 'password1234',
            
    'from' => '3GFM',
            
    'to' => $user->phone,
            
    'text' => sprintf("Hello %s, your payment of %s has been processed",
                
    $user->getName(), Am_Currency::render($payment->amount))     
        );

        
    $r = new Am_HttpRequest('http://www.coolsmswebsite.com/http-api.php?' http_build_query($data));
        
    $r->send();
    });
  5. jakumpe

    jakumpe New Member

    Joined:
    May 14, 2010
    Messages:
    13
    So is there a way to implement this same type of code only when the user receives access to a specific product?
  6. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You can use the following code to retrieve array of product ids from payment:
    PHP:
    $pids array_map(function($e){return $e->item_id;}, $payment->getInvoice()->getItems());
    Then use conditional statement to execute your code only in case of some specific products
    PHP:
    if (array_intersect(array(1,2,3), $pids)) {
        
    //..do some stuff only for products with ids 1,2,3
    }

Share This Page