Make a Payment Value Using Unique Code

Discussion in 'Free Talk for Website Owners and Webmasters' started by masdika, Dec 8, 2016.

  1. masdika

    masdika --

    Joined:
    Oct 6, 2013
    Messages:
    18
    Hello, all, I want to add a unique code on the payment of transaction value, how?



    Thanks

    Attached Files:

  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    @masdika Do you mean you want to add some unique fraction to price for each invoice?

    You can use the following code in site.php file
    http://www.amember.com/docs/Site.php_file

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::INVOICE_BEFORE_PAYMENT_SIGNUP, function(Am_Event $e) {
       
    $invoice $e->getInvoice();
       
    $item $invoice->getItem(0);
       
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') + ($invoice->pk() % 100));
       
    $invoice->calculate();
       
    $invoice->save();
    });
    bikinwebsitemudah likes this.
  3. bikinwebsitemudah

    bikinwebsitemudah New Member

    Joined:
    Jan 18, 2016
    Messages:
    8
    Ok, thank's... it work... but how to edit email sending to client.
    because invoice on email still fix rate... not use unique .
  4. caesar

    caesar aMember Pro Developer Staff Member

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

    bikinwebsitemudah New Member

    Joined:
    Jan 18, 2016
    Messages:
    8
    @caesar ok, i will write in support :)
  6. aditya99

    aditya99 New Member

    Joined:
    Sep 9, 2016
    Messages:
    3
  7. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Here is code that will work better for such case:

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::INVOICE_CALCULATE, function(Am_Event $e) {
       if (@
    $GLOBALS['add_fraction']++) return;
       
    $invoice $e->getInvoice();
       
    $item $invoice->getItem(0);
       
    $id $invoice->pk() ?: $e->getDi()->db->selectCell("SELECT MAX(invoice_id)+1 FROM ?_invoice;");
       if (
    $item && !$item->data()->get('add_fraction')) {
           
    $item->data()->set('add_fraction'1);
           
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') + ($id 100));
           
    $invoice->calculate();
       }
       unset(
    $GLOBALS['add_fraction']);
    });
  8. buchin

    buchin New Member

    Joined:
    Sep 1, 2013
    Messages:
    1
    Hi, I'm sorry for bumping this old issue. The code above works fine for me. However, this become problem when I want only selected payment method to implement this. I.e, keep paypal payment as is, where offline payment, using fraction.

    What I need is a code to detect payment method for current invoice.
    Would you like to help me achieve that?
  9. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You can check $invoice->paysys_id ie.:

    PHP:
    if ($invoice->paysys_id == 'offline') {
       
    //add fraction
    }
  10. akbar17

    akbar17 New Member

    Joined:
    Nov 8, 2019
    Messages:
    4
    @caesar not work , could you help me please to full code?
    Last edited: Mar 11, 2021
  11. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    @akbar17 what code did you try? What is error you get with code?
  12. akbar17

    akbar17 New Member

    Joined:
    Nov 8, 2019
    Messages:
    4
    Code:
     Am_Di::getInstance()->hook->add(Am_Event::INVOICE_CALCULATE, function(Am_Event $e) {
    if ($invoice->paysys_id == 'offline') {
       if (@$GLOBALS['add_fraction']++) return;
    
       $invoice = $e->getInvoice();
       $item = $invoice->getItem(0);
       $id = $invoice->pk() ?: $e->getDi()->db->selectCell("SELECT MAX(invoice_id)+1 FROM ?_invoice;");
       if ($item && !$item->data()->get('add_fraction')) {
           $item->data()->set('add_fraction', 1);
           $item->data()->set('orig_first_price', $item->data()->get('orig_first_price') + ($id % 100));
           $invoice->calculate();
       }
    }else{
    if (@$GLOBALS['add_fraction']++) return;
    
       $invoice = $e->getInvoice();
       $item = $invoice->getItem(0);
       $id = $invoice->pk() ?: $e->getDi()->db->selectCell("SELECT MAX(invoice_id)-1 FROM ?_invoice;");
       if ($item && !$item->data()->get('add_fraction')) {
           $item->data()->set('add_fraction', 1);
           $item->data()->set('orig_first_price', $item->data()->get('orig_first_price') - ($id % 100));
           $invoice->calculate();
       }
    }
       unset($GLOBALS['add_fraction']);
    }); 
  13. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    What is error/issue you get with this code? What is exactly does not work as you expect?
  14. akbar17

    akbar17 New Member

    Joined:
    Nov 8, 2019
    Messages:
    4
    @caesar when there is an offline sign up or not it always falls to else. so when there is a signup using offline payment it doesn't work ,no error messages or error codes.

    and for example I want to use if else if using currency USD and IDR is also not working.For example, I add a unique number after a (,) or (.), for example like above but behind a (,) or (.) : $10.01 or $10.02. like this . for code I use this code

    PHP:
    Am_Di::getInstance()->hook->add(Am_Event::INVOICE_CALCULATE, function(Am_Event $e) {
        if (@
    $GLOBALS['add_fraction']++) return;
        
    $invoice $e->getInvoice();
       
    $item $invoice->getItem(0);
       
    $id $invoice->pk() ?: $e->getDi()->db->selectCell("SELECT MAX(invoice_id)-1 FROM ?_invoice;");
       
    $es $invoice->pk() ?: $e->getDi()->db->selectCell("SELECT MAX(invoice_id)+1 FROM ?_invoice;");

        if (
    $invoice->currency=='USD') {
       if (
    $item && $item->first_price && !$item->data()->get('add_fraction'))  {
                  
    $item->data()->set('add_fraction'1);
           
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') + '0.'.($es 100));
           
    $invoice->calculate();  

      }
        } elseif (
    $invoice->currency=='IDR'){

      
        if (
    $item && $item->first_price && !$item->data()->get('add_fraction'))  {
                  
    $item->data()->set('add_fraction'1);
           
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') - ($id 100));
           
    $invoice->calculate();
      }
        }
       unset(
    $GLOBALS['add_fraction']);
    }); 
  15. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Do you use these both code snippets (with offline and currency) in same time?
    Please contact us in helpdesk:
    https://www.amember.com/support
    We will check it.

    Best Regards.
  16. akbar17

    akbar17 New Member

    Joined:
    Nov 8, 2019
    Messages:
    4
    no . i tried one of them. but not work
  17. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Do you mean you have incorrect amount in case of USD?
    You need to replace line:
    PHP:
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') + '0.'.($es 100));
    with
    PHP:
    $item->data()->set('orig_first_price'$item->data()->get('orig_first_price') + ('0.'.($es 100)));
    In order to help you I need to know what you want to achieve and what you get incorrectly with your code.

Share This Page