Execute script on new subscription

Discussion in 'aMember Pro v.4' started by leetspeak, Jun 14, 2012.

  1. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    How would i go about executing my own script once a new subscription is successful (paypal only payment).

    I was thinking of putting a php exec into the paypal.php file within the processValidated function but not sure how to get the product and user id's passed, what i was thinking is something like this:

    Code:
    public function processValidated()
        {
            switch ($this->txn_type) {
                case self::TXN_SUBSCR_SIGNUP:
                    if ($this->invoice->first_total <= 0) // no payment will be reported
                        if ($this->invoice->status == Invoice::PENDING) // handle only once
                            $this->invoice->addAccessPeriod($this); // add first trial period
                            exec('path/to/my/script.php?id=$payer_id&product=$product_id'); // this is my bit
                break;
    
    but of course the question would be, how do i get the payer_id and product_id variables?

    ANY help would be much appreciated.
  2. robw

    robw CGI-Central Partner

    Joined:
    Dec 18, 2007
    Messages:
    287
    Something like this:

    PHP:
    public function processValidated()
        {
            switch (
    $this->txn_type) {
                case 
    self::TXN_SUBSCR_SIGNUP:
                    if (
    $this->invoice->first_total <= 0// no payment will be reported
                        
    if ($this->invoice->status == Invoice::PENDING) { // handle only once
                            
    $this->invoice->addAccessPeriod($this); // add first trial period
                            
    $payer_id $this->invoice->user_id;
                            
    $product_ids $this->invoice->getProducts();
                            foreach (
    $product_ids as $product_id) {
                                
    // NB: Can have multiple products per invoice
                                
    exec('path/to/my/script.php?id=$payer_id&product=$product_id'); // this is my bit
                            
    }
                        }
                break;
    But custom stuff like this should really be implemented as a plugin or in site.php using the paymentAfterInsert hook.

    e.g. site.php code might look something like:

    PHP:
    function customPaymentAfterInsert(Am_Event_PaymentAfterInsert $event)
        {
            
    $invoice $event->getInvoice();
     
            
    $payer_id $invoice->user_id;
            
    $product_ids $invoice->getProducts();
            foreach (
    $product_ids as $product_id) {
                
    // NB: Can have multiple products per invoice
                
    exec('path/to/my/script.php?id=$payer_id&product=$product_id'); // this is my bit
            
    }
        }
    Am_Di::getInstance()->hook->add('paymentAfterInsert''customPaymentAfterInsert');
    Cheers
    Rob

    Ps - using exec is not advisable as it adds a potential security risk. Better to use Am_HttpRequest

    PHP:
    $http = new Am_HttpRequest("URL/to/my/script.php?id=$payer_id&product=$product_id");
    $response $http->send();
    alex likes this.

Share This Page