Add auto-create to custom payment plugin

Discussion in 'Customization & add-ons' started by clickleaders, Oct 21, 2012.

  1. clickleaders

    clickleaders Member

    Joined:
    May 10, 2009
    Messages:
    38
    Thanks to previous help on this forum, my custom payment plugin is working perfectly for logged-in customers. Now we want to add the auto-create option so that the member can be added when the payment is received. I can't find any documentation, but I've been looking at the Clickbank plugin as an example.

    So far I've figured out that in the Paysystem class at the top I need the canAutoCreate function to return true, and in the Transaction_Incoming class I need to define the autocreatemap and add the autoCreateGetProducts and possibly fetchUserInfo functions. Is there anything else I have to do, and what do I need to do to configure those functions?

    Any assistance would be greatly appreciated.
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    There is a sample how it is done in clickbank plugin:

    PHP:
    class Am_Paysystem_Clickbank extends Am_Paysystem_Abstract
    {
    ...
       public function 
    __construct(Am_Di $di, array $config)
        {
            
    $this->defaultTitle ___("ClickBank");
            
    $this->defaultDescription ___("pay using credit card or PayPal");
            
    parent::__construct($di$config);
            
    $di->billingPlanTable->customFields()->add(
                new 
    Am_CustomFieldText(
                
    'clickbank_product_id'
                
    'ClickBank Product#'
                
    'you have to create similar product in ClickBank and enter its number here'
                
    ,array(/*,'required'*/)
                )
            );
        }
    ...
    }
     
     
    class 
    Am_Paysystem_Transaction_Clickbank_Thanks extends Am_Paysystem_Transaction_Incoming_Thanks
    {
        
    // here we have to return array of loaded products matching current transaction
        // (Product object loaded from Am_Di::getInstance()->productTable)
        // this method fetches products by value of "clickbank_product_id" billing_plan's additional field
        
    public function autoCreateGetProducts()
        {
            
    $cbId $this->request->getFiltered('item');
            if (empty(
    $cbId)) return;
            
    $pl $this->getPlugin()->getDi()->billingPlanTable->findFirstByData('clickbank_product_id'$cbId);
            if (!
    $pl) return;
            
    $pr $pl->getProduct();
            if (!
    $pr) return;
            return array(
    $pr);
        }
        public function 
    generateInvoiceExternalId()
        {
            return 
    $this->getUniqId();
        }
        
    // there we convert incoming variables into array to be used for values
        // in am_user table.
        
    public function fetchUserInfo()
        {
            
    $names preg_split('/\s+/'$this->request->get('cname'), 2);
            
    $names[0] = preg_replace('/[^a-zA-Z0-9._+-]/'''$names[0]);
            
    $names[1] = preg_replace('/[^a-zA-Z0-9._+-]/'''$names[1]);
            
    $email $this->request->get('cemail');
            
    $email preg_replace('/[^a-zA-Z0-9._+@-]/'''$email);
            return array(
                
    'name_f' => $names[0],
                
    'name_l' => $names[1],
                
    'email'  => $email,
                
    'country' => $this->request->getFiltered('ccountry'),
                
    'zip' => $this->request->getFiltered('czip'),
            );
        }
       ......}
  3. clickleaders

    clickleaders Member

    Joined:
    May 10, 2009
    Messages:
    38
    Thank you, this helps. However, the payment system we're using doesn't require it's own product id's...how can we have it use just the internal amember product id?
  4. clickleaders

    clickleaders Member

    Joined:
    May 10, 2009
    Messages:
    38
    So we decided to go with the custom product id's for our plugin, but it's still not working...our plugin is not exactly the same as Clickbank because there is no IPN. I don't know how to make it generate the invoice number, and if I try to copy the logic from this thread: http://www.amember.com/forum/threads/no-invoice-passed.14933/#post-57408
    I don't have a user id yet to pass it and don't know how to get it to look up the product by our new custom product id...plus I don't see any of this in the clickbank plugin.

    Any ideas at all would be greatly appreciated.
  5. clickleaders

    clickleaders Member

    Joined:
    May 10, 2009
    Messages:
    38
    Never mind, I solved it...I took out the code from the other thread, and in the findInvoiceId function, I replaced this code from the clickbank plugin:
    $invoice = $this->getPlugin()->getDi()->invoiceTable->findByReceiptIdAndPlugin($this->request->getEscaped('cbreceipt'), $this->plugin->getId());

    with this from the swreg plugin:
    $invoice = Am_Di::getInstance()->invoiceTable->findByReceiptIdAndPlugin($this->getReceiptId(), $this->plugin->getId());

    and now everything works.

Share This Page