Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an PayPal button with overwritable variables

Tags:

html

php

paypal

Hello I would like to create a paypal buy button which has a dynamic set amount. I would like to pass the amount by a text input field within the form and the item_number by a hidden field.

The issue is that what ever I do I get a encrypted s-xclick button from the paypal website. This button does not allow hidden variables being placed in the form.

I think what I need is a xclick button. My goal is to allow users to increase their internally credit of my website.

EDIT (moving the addition to the question from the answer to the question)(from here @tokam:

To add this to the discussion I would like to show my current solution for the problem:

Here we have some Javascript validation which helps the user with the input. Recognize that it opens a lightbox on success

function validatePaypalForm()
{
    var val = $('#paypalPaymentAmount').val().replace(/\s*$/, "").replace(/,/ , ".").replace(/€$/, "");
    var errormsg = '';
    var ret, amountField;
    if( val==='' || isNaN( parseFloat(val) ) || !isFinite(val) )
    {
        errormsg = 'Bitte geben Sie einen gültigen Betrag an';
}else if( parseFloat( val ) < <?php echo $this->minimum?>  )
{
    errormsg = 'Das Einzahlungsminimum betr&auml;gt <?php echo $this->minimum?>&euro;';
}

ret = ( errormsg === '' );


amountField = $( '#paypalAmountField' );
if( ret )
{
    amountField.removeClass( 'error' );     
    $('#paypalAmountErrorMessage').html( '&nbsp;' );
    $('#paypalPaymentAmount').val( val );
    fb.start( 
        '<p><strong>Sie werden in kürze zur Seite von Paypal weitergeleitet.</strong></p>',
        'width:700 showPrint:false modal:true showClose:false showOuterClose:true showItemNumber:false closeOnNewWindow:false outsideClickCloses:true innerBorder:0 imageClickCloses:false scrolling: no'
    );

}else{
    amountField.addClass( 'error' );
    $('#paypalAmountErrorMessage').html( errormsg );
}

return ret;

} Here comes my button now. The issues I am having with are e.g. that it is easy for the user to set an other currency code. I could handle this in my IPN Listener by refunding the payment. Are there other issues which come with an unencrypted changeable button?

<form onsubmit="return validatePaypalForm();" class="stn-form" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<fieldset id="fieldset-p"><legend><span>2.</span>myproject Guthaben aufladen per Paypal Zahlung</legend>
<div id='paypalAmountField' class="field">
<label for='paypalPaymentAmount' >Betrag &euro;:</label>
<input id='paypalPaymentAmount' type="text" name='amount' value='' />
<span style='display:block;' id='paypalAmountErrorMessage' class='errorText'>'&nbsp;</span>

</div>
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="THE_ID_OF_MY_CLIENT">
<input type="hidden" name="lc" value="DE">
<input type="hidden" name="item_name" value="myproject Advertiser Vorkasse">
<input type="hidden" name='item_number' value="11500">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_paynowCC_LG.gif:NonHosted">
<input type="hidden" name="rm" value="1">
<input type="hidden" name='cbt' value="Zu myproject.de zur&uuml;ckkehren">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="return" value="http://myproject.somedomain.net/advertiser/guthaben-aufladen/ret/success" />
<input type="hidden" name="cancel_ return" value="http://myproject.somedomain.net/advertiser/guthaben-aufladen/ret/canceled" />
<div class="actionrow">

<input type="image" src="https://www.paypalobjects.com/de_DE/DE/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="Jetzt einfach, schnell und sicher online bezahlen – mit PayPal.">
<img alt="" border="0" src="https://www.paypalobjects.com/de_DE/i/scr/pixel.gif" width="1" height="1">
</div>
</fieldset>
</form>
like image 976
Jakob Alexander Eichler Avatar asked Nov 20 '11 18:11

Jakob Alexander Eichler


People also ask

What is the benefit of using Paypal buttons?

What Are the Benefits of PayPal Smart Payment Buttons? The clear benefit is that your buyers are presented with more ways to pay. This can encourage someone to go through and make a purchase, which they otherwise may have avoided had you only offered one way to pay.


1 Answers

The reason you can't override the amount dynamically, is because you have a so-called 'PayPal hosted button'.
With a hosted button, the amount is stored on PayPal's side and can't be overwritten with the 'amount' variable. You'll either want to use a non-hosted button, or use the BMUpdateButton API call to dynamically update the button's amount.
To use a non-hosted button, simply find 'Step 2' in the button creation tool and untick 'Host button with PayPal'.

Option 2: Still use the hosted button, and use the BMUpdateButton API to update the amount. An example request for BMUpdateButton would look as follows:

USER=Your API username
PWD=Your API password
SIGNATURE=Your API signature
VERSION=82.0
HOSTEDUBTTONID=The value of <input type="hidden" name="hosted_button_id" value="">
BUTTONTYPE=The type of button. E.g. BUYNOW
BUTTONCODE=The type of code you want to get back. E.g. HOSTED
L_BUTTONVAR0=amount=The new amount with a period as separator
L_BUTTONVAR1=item_name=Optional: a new item name if you wish

Similary, you could also use the BMCreateButton API to create a new button, or use the BMButtonSearch API to search through a list of all your stored hosted buttons (to find the hosted_button_id of your button automatically, for example)

The reason to use a hosted button is because it's more secure. A non-hosted, unencrypted button would basically leave the amounts open to manipulation. Fraudulent transactions waiting to happen.

like image 120
Robert Avatar answered Sep 29 '22 08:09

Robert