x
 
Create New Page
 View Previous Changes    ( Last updated by Guy Schalnat )

Advanced PayPal

Question:

How can I overcome the limitations of the simple commerce module to use some of the more advanced PayPal features?

Answer:

Sometimes you need more PayPal options than the Simple Commerce Module can offer. In this case it can be a good idea to create the necessary form yourself instead of having the SCM do it.

You can still use the SCM to provide basic data like item price etc. Here’s an example:

{exp:weblog:entries weblog="webshop" rdf="off" disable="pagination|member_date|trackbacks" limit="1"}
{exp
:simple_commerce:purchase entry_id="{entry_id}"}

...

<
form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="paypal">

<
input type="hidden" name="cmd" value="_cart" />

<
input type="hidden" value="http://www.example.com/?ACT=20" name="notify_url" />  
<
input type="hidden" value="http://www.example.com/webshop/thank_you" name="return" />  
<
input type="hidden" value="http://www.example.com/webshop/cancel" name="cancel_return" />  

<
input type="hidden" name="business" value="me@example.com" />
<
input type="hidden" name="currency_code" value="EUR" />

<
input type="hidden" name="item_name" value="{title}" />
<
input type="hidden" name="item_number" value="{item_id}" />
<
input type="hidden" name="add" value="1" />
<
input type="hidden" name="upload" value="1" />

<
input type="hidden" name="no_shipping" value="2" />
<
input type="hidden" name="no_note" value="1" />
<
input type="hidden" name="undefined_quantity" value="1" />

<
input name="submit" alt="Make secure payments with PayPal"
onclick="window.open('','paypal','width=780,height=500,scrollbars=yes,resizable=yes,status=yes')" />

<
input type="radio" name="amount" value="{item_regular_price}" checked="checked" /> Pay full price: {item_regular_price} €<br />
<
input type="radio" name="amount" value="100" /> Pay by installments - First rate 100 €<br />

<
input type="checkbox" value="10.00" name="shipping">
Check if delivery address is outside the EU (€ 30 for S&H;)

</
form>

{/exp:simple_commerce:purchase}
{
/exp:weblog:entries}

The item_name and item_id are automatically provided by EE, as is the price. (We are offering two options here, paying in full or by installments. If you don’t just replace the radio button with a hidden field.)

Also, we provide a checkbox for adding a shipping surcharge for some customers. You could easily use a pulldown menu to let them select the country or region they’re in.

——————————————————————————————————————-

Interesting.  I attacked the problem a different way.  I used the {buy_now_url} variables and just modified it before sending it to PayPal.  Like this (slightly edited to remove unimportant detail):

Name:</td><td><input type="text" name="name_input" id="name_input_{entry_id}" size="64" maxlength="200">

Affiliation:</td><td><input type="text" name="affiliation_input" id="affiliation_input_{entry_id}" size="64" maxlength="200">

Number:<select name="quantity_input" id="quantity_input_{entry_id}">
   <
option value="1" selected>1</option>
   <
option value="2" selected>2</option>
   <
option value="3" selected>3</option>
</
select>

<
button name="buy_now" id="buy_now_{entry_id}" type="button" disabled="1" onclick="window.open( process_store_url( '{buy_now_url}', '{entry_id}' ), 'paypal' ); return false;">Buy Now</button>

where the function process_store_url is defined in the html header something like this:

function prepare_for_command_line( s )
{
   
if (encodeURIComponent)
   
{
     
return encodeURIComponent(s);
   
}

   
return escape(s);
}

function process_store_url(base_url, id)
{
   
// we're selling memberships and registering people for talks
   
var url = base_url+'&no_shipping=1';

   var
name_element = document.getElementById( 'name_input_' + id );
   if (
name_element != null && name_element.value != null && name_element.value != '')
   
{
      url
= url + '&on0=Name&os0;=' + prepare_for_command_line( name_element.value );
   
}

   
var affiliation_element = document.getElementById( 'affiliation_input_' + id );
   if (
affiliation_element != null && affiliation_element.value != null && affiliation_element.value != '')
   
{
      url
= url + '&on1=Affiliation&os1;=' + prepare_for_command_line( affiliation_element.value );
   
}

   
var quantity_element = document.getElementById( 'quantity_input_' + id );
   var
quantity_value = type_element.options[quantity_element.selectedIndex].value;

   
url = url.replace('&amount=1&', '&amount;=' + quantity_value + '&' );

   return
url;
}

I think they both work, it just matters which one is easiest.  I happen to be pretty good at javascript, and I didn’t want to break anything if expression engine changed the hidden fields in a later update, but I certainly wouldn’t complain if someone who is more familiar with paypal would want to do it the other way.

Category:Commerce -> PayPal Category:Tricks

Categories: