开发者

Add multiple items to Paypal in single form using hosted_button_id

开发者 https://www.devze.com 2023-03-21 01:14 出处:网络
I want to setup a single form on a site to purchase multiple products via PayPal. The products are set in开发者_运维知识库 the PayPal account and each has a hosted_button_id.

I want to setup a single form on a site to purchase multiple products via PayPal. The products are set in开发者_运维知识库 the PayPal account and each has a hosted_button_id.

The form would contain a series of descriptions and quantity fields, all initially set to zero. The customer could then add desired quantities against each product and submit the form with a single 'Buy now' button.

Is this possible with PayPal? I don't particularly want a 'cart' experience and I don't want to pass product prices (instead using the data stored at PayPal).

thanks


In the end I concluded you can't do this.

I tried everything, including AJAXing the submission and send the submission via a hidden iFrame to keep the user on my site. Nothing worked. It seems PayPal are very keen to enforce their approved shopping workflow when using their cart.


You can do this, but you'll need to do some server-side programming. You build an "encrypted button" programmatically, and submit that to PayPal.

My store page at https://secure.entrian.com/store/store.html does it - it prompts for some information, sends that to the server to be turned into an encrypted button, and then submits that to PayPal.

The JavaScript looks like this, where $license_name and $quantity are the fields that I'm prompting for, and formgen.py is a server-side Python script that builds and signs the request using OpenSSL according to PayPal's documentation:

$paypal_form.submit(function () {
   var response = $.ajax({
       type: "GET",
       url: 'formgen.py?' + serialize({
           licenseName: $license_name.val(),
           quantity: $quantity.val()
       }),
       async: false
   }).responseText;

   if (response.indexOf('PKCS7') >= 0) {
       $encrypted.val(response);
   } else {
       $payment_errors.text(response).show(easing_duration);
       return false;
   }

   return true;
}

The form looks like this (simplified rather):

<form id="paypal-form" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="upload" value="1">
<input type="hidden" id="encrypted" name="encrypted" value="">
<button id="buynow" class="submit-button btn" type="submit">Buy Now</button>
</form>

That <input name="encrypted" ...> gets populated with the AJAX response before the form gets submitted to PayPal.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号