api: adding product to user, how?

Discussion in 'aMember Pro v.4' started by frdako, Jan 24, 2012.

  1. frdako

    frdako New Member

    Joined:
    Aug 17, 2006
    Messages:
    7
    Hi all,
    I'm using aMember API to create new user accounts, what I'm struggling with is adding a product to such a new account. Hope somebody can help.

    This is my code to create new account:
    $userrecord = Am_Di::getInstance()->userRecord;
    $userrecord->setForInsert( array('email'=>$email, 'name_f'=>$name_f, 'name_l'=>$name_l,
    'status'=>1));
    $userrecord->generateLogin();
    $userrecord->generatePassword();
    $userrecord->insert();


    thanks,
    David
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    it is quite complex, but you can look for example to SignupController.php
    enable 'offline' plugin to try it.

    PHP:
    $di Am_Di::getInstance();
    $invoice $di->invoiceRecord;
    $invoice->setUser($userrecord);
    $invoice->add($di->productTable->load(123));
    $invoice->add($di->productTable->load(245));
    $invoice->paysys_id 'offline';
    $invoice->calculate();
    $invoice->insert();

    $tr = new Am_Paysystem_Transaction_Manual($di->plugins_payment->loadGet('offline'));
    $tr->setAmount($invoice->first_total);
    $tr->setReceiptId('test');

    $invoice->addPayment($tr); // add "payment" record, it starts invoice and opens access
  3. frdako

    frdako New Member

    Joined:
    Aug 17, 2006
    Messages:
    7
    thanks Alex,
    I get the following error-message when I try to a use your example from the 'offline' plugin:
    "Existing payments count [0] exceeds number of allowed rebills [0]+1, could not add new payment"

    This what is written to error log:
    Exception Am_Exception_Paysystem
    Invoice->addPaymentWithoutAccessPeriod [ application/default/models/Invoice.php : 941 ]
    Invoice->addPayment [ /Users/david/yabubble.com/htdocs/dev2.yabubble.com/include/_membership.php : 87 ]
    Exception Am_Exception_Paysystem
    Invoice->addPaymentWithoutAccessPeriod [ application/default/models/Invoice.php : 941 ]
    Invoice->addPayment [ /Users/david/yabubble.com/htdocs/dev2.yabubble.com/include/_membership.php : 87 ]


    This is my curent code:
    PHP:
    $di Am_Di::getInstance();
    $userrecord $di->userRecord;
    $userrecord->setForInsert( array('email'=>$email'name_f'=>$name_f'name_l'=>$name_l,'status'=>1));
    $userrecord->generateLogin();
    $userrecord->generatePassword();
    $userrecord->insert();

    $invoice $di->invoiceRecord;
    $invoice->setUser($userrecord);
    $invoice->add($di->productTable->load(2)); //product_id of the product that I want to add
    $invoice->paysys_id 'offline';
    $invoice->calculate();
    $invoice->insert();

    $tr = new Am_Paysystem_Transaction_Manual($di->plugins_payment->loadGet('offline'));
    $tr->setAmount($invoice->first_total);
    $tr->setReceiptId('test');

    $invoice->addPayment($tr);
  4. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Please replace
    $invoice->calculate();
    $invoice->insert();

    to
    $invoice->calculate();
    print_rr((string)$invoice);
    $invoice->insert();

    and copy/paste output here.
  5. frdako

    frdako New Member

    Joined:
    Aug 17, 2006
    Messages:
    7
    thanks

    Code:
    Array
    (
        [currency] => USD
        [user_id] => 42
        [paysys_id] => offline
        [tax_rate] => 0
        [first_subtotal] => 0
        [first_discount] => 0
        [first_tax] => 0
        [first_shipping] => 0
        [first_total] => 0
        [second_subtotal] => 0
        [second_discount] => 0
        [second_tax] => 0
        [second_shipping] => 0
        [second_total] => 0
        [first_period] => 10y
        [second_period] =>
        [rebill_times] => 0
        [base_currency_multi] => 1
        [_items] => Array
            (
                [0] => Array
                    (
                        [invoice_id] =>
                        [item_id] => 2
                        [item_title] => yabubble.com
                        [item_type] => product
                        [item_description] =>
                        [first_price] => 0.00
                        [first_period] => 10y
                        [rebill_times] => 0
                        [second_price] =>
                        [second_period] =>
                        [currency] => USD
                        [no_tax] =>
                        [trial_group] =>
                        [renewal_group] =>
                        [is_tangible] =>
                        [is_countable] =>
                        [billing_plan_id] => 3
                        [billing_plan_data] => a:0:{}
                        [qty] => 1
                        [first_discount] => 0
                        [first_tax] => 0
                        [first_total] => 0
                        [first_shipping] => 0
                        [second_discount] => 0
                        [second_tax] => 0
                        [second_total] => 0
                        [second_shipping] => 0
                    )
    
            )
    
    )
    
  6. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    You get that error because you try to add payment to invoice that should not have any payments.
    You have free product, so there should not be any payments.
    Here is working example for your situation:
    PHP:
    $di Am_Di::getInstance();
    $userrecord $di->userRecord;
    $userrecord->setForInsert( array('email'=>'t@t.com''name_f'=>'t''name_l'=>'t','status'=>1));
    $userrecord->generateLogin();
    $userrecord->generatePassword();
    $userrecord->insert();

    $invoice $di->invoiceRecord;
    $invoice->setUser($userrecord);
    $invoice->add($di->productTable->load(2)); //product_id of the product that I want to add
    $invoice->paysys_id 'free';
    $invoice->calculate();
    $invoice->insert();

    $invoice->addAccessPeriod(new Am_Paysystem_Transaction_Free($di->plugins_payment->loadGet('free')));

  7. frdako

    frdako New Member

    Joined:
    Aug 17, 2006
    Messages:
    7
    Thanks! this is really great....

Share This Page