Replace shortcode with HTML multiple times on a page - php

My client wants the ability to put PayPal add-to-cart buttons anywhere on a page, with potentially multiple buttons on a single page. He will use a shortcode like [price MONKEY] to have a PaylPal button replace that text, with the price and description for "monkey".
So every time we find a [price] shortcode on the page, I need to query the database to get value, insert then into the following PayPal button code, and display the button at that place on the page. Then move to the next replacement, if any.
Here's the PayPal button code:
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" value="_cart" name="cmd" />
<input type="hidden" value="me#mydomain.com" name="business" />
<input type="hidden" value="1" name="add" />
<input type="hidden" value="MONKEY" name="item_name" />
<input type="hidden" value="" name="item_number" />
<input type="hidden" value="17.00" name="amount" />
<input type="hidden" value="2" name="no_shipping" />
<input type="hidden" value="USD" name="currency_code" />
<input border="0" type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it's fast, free and secure!" />
</form>
I've been trying to do this with a preg_replace_callback() function, but having problems inserting all the HTML for the button, and then with looking for more shortcode instances on the page and replacing them.
Appreciate any help on this! It's so simple in Wordpress where all this code is already written (but where?) -- this is an application that I created from scratch and now needs some enhancement. Thanks!
Here's my regex:
$pattern = '/\[price (.*?)\]/';

You can just use a basic preg_replace to accomplish this task using your pattern. something like so:
<?php
function myexamplefunction($html){
$pattern = '/\[price (.*?)\]/';
$replace = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" value="_cart" name="cmd" />
<input type="hidden" value="me#mydomain.com" name="business" />
<input type="hidden" value="1" name="add" />
<input type="hidden" value="$1" name="item_name" />
<input type="hidden" value="" name="item_number" />
<input type="hidden" value="17.00" name="amount" />
<input type="hidden" value="2" name="no_shipping" />
<input type="hidden" value="USD" name="currency_code" />
<input border="0" type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it\'s fast, free and secure!" />
</form>';
return preg_replace($pattern,$replace,$html);
}

Related

Intermittent issue of PayPal Checkout Button not posting to PayPal

I'm having a weird problem that is very intermittent. In fact, I've never been able to reproduce it on my end, but a variety of customers have experienced it. Typically, it's with mobile IOS - new and old. Basically, when the customer reaches checkout, they click Check Out and they end up on PayPal's login page. No values from the form are passed, as if they just went to PayPal.com. We do get a lot of sales via PayPal, so not sure what percentage has this issues, but want to make sure we capture those sales. Doing a Google, I have seen people mention this, but not seeing any solid solutions.
Here is my form code:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">
<input type="hidden" name="item_name_1" value="Product Test 1" />
<input type="hidden" name="amount_1" value='10.00' />
<input type="hidden" name="quantity_1" value="1" />
<input type='hidden' name='item_number_1' value='' />
<input type="hidden" name="shipping_1" value='8.00' />
<input type="hidden" name="no_shipping" value="2" />
<input type="image" id="PayPalButton" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" name="submit" class="wp_cart_checkout_button" alt="Make payments with PayPal - it's fast, free and secure!" />
<input type="hidden" name="return" value="https://www.example.com/thankyou/?a=1" />
<input type="hidden" name="notify_url" value="https://www.example.com/paypal/" />
<input type="hidden" name="business" value="paypal#example.com" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="charset" value="utf-8" />
<input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
<input type="hidden" name="custom" value="cart=58597ef8bdd56c789946533a9c1d0d1c&x=337917&email=test#example.com&ip=6xxx.xxx.xxx.xxx" />
</form>
I've seen people remove target="paypal" and use target="_self". I've only used target="_blank". I don't want to use GET method since I could have lot of variables depending on how large the cart is. Every time I test (desktop, table, phone), the paypal process works. So not sure what is stopping some of the users.

how to use paypal subscription form for multiple subscriptions?

I am trying to use paypal subscription form for multiple product, but its not working.
Here is my form which i used.
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="archirayan9#gmail.com">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_shipping" value="1">
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="item_name" value="Baseball Hat Monthly - test2 " />
<input type="hidden" name="item_number" value="asd">
<input type="hidden" name="a3" value="10.00">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="t3" value="M">
<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1">
</form>
This form is for 1 product subscription. I want to make it for multiple products with different item_id.
I cant found any thing that help me to setup multiple product subscription using this form.
Anyone have solution how to do?

Want to connect my shopping cart total price to a paypal checkout

I have a displaycart.php page where it displays all items I have in a shopping cart, each item has an ItemUnitPrice field where it shows the unit price of the item.
If I have say 6 items in the cart then I want to add up the total ItemUnitPrice fields and set that amount to be paid with PayPal.
I have checked out PayPals button features which they allow and you can only set a given price for the user to pay for when you create the button.
Is there a way I can link the PayPal button to my database so it shows the total of all the ItemUnitPrice fields.
The variable which holds the total ItemUnitPrice:
<?php
$Total = $Quantity * $ItemUnitPrice;
?>
The PayPal 'Pay Now' Button:
<html>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="KAMQ5PVWVDYYN">
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
</html>
Edit:
Yeah I've done something stupidly wrong!
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_BLANK">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="info#asianweddingservices.org" />
<input type="hidden" name="item_name" value="<? $ItemDesc ?>" />
<input type="hidden" name="item_number" value="<?$ItemCode ?>" />
<input type="hidden" name="amount" value="<?$Total ?>" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="lc" value="GB" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
<input src="paypal/purchase.png" name="Submit" type="image" value="purchase" alt="Purchase" />
</form>
Yes. Change the set value to your total.
Show your code and I can edit the answer to fix it for you.
Add this:
<input type="hidden" name="amount" value="<? echo $Total; ?>" />
If that doesn't work:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_BLANK">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="PAYPAL EMAIL" />
<input type="hidden" name="item_name" value="ITEM NAME" />
<input type="hidden" name="custom" value="CUSTOM VALUE" />
<input type="hidden" name="item_number" value="ITEM NUMBER" />
<input type="hidden" name="amount" value="TOTAL" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="lc" value="GB" />
<input type="hidden" name="bn" value="PP-BuyNowBF" />
<input src="paypal/purchase.png" name="Submit" type="image" value="pruchase" alt="Purchase" />
</form>
Replacing the values to yours of-course.

Multiple PayPal buttons inside single Form Element

Currently I can have single paypal button in a form element like:
<form id="pkg1" name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"/>
<input type="hidden" name="business" value="seller_1360303883_biz#gmail.com"/>
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="item_name" value="Digital Download"/>
<input type="hidden" name="amount" value="$<?php echo $price1 ?>"/>
<input type="hidden" name="return" value="https://www.sandbox.paypal.com/cgi-bin/success.php"/>
<input type="hidden" name="notify_url" value="notify.php"/>
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"/>
</form>
I wonder if there is a way to place more than one paypal buttons in single form??
I'm no talking about adding to cart.
So is there a way that i be able to make individual transactions while putting them in single form?
Thanx in Advance

Paypal return URL - using GET parameters?

Here's some simple code I'm using to test the Paypal Website Payments Standard upload thingy.
My return URL is http://mysite/index.php?module=store&show=order_confirm
I go through the payment process, and when I get to the end and it returns me to the page, it instead just returns me to index.php (i.e. without the extra parameters).
Anyone know what the deal with this is
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1">
<input type="hidden" name="return" value="http://mysite/index.php?module=store&show=order_confirm" />
<input type="hidden" name="currency_code" value="EUR" />
<input type="hidden" name="business" value="b.coug_1277121937_biz#gmail.com">
<input type="hidden" name="item_name_1" value="adaddada" />
<input type="hidden" name="amount_1" value="30.00" />
<input type="hidden" name="quantity_1" value="1" />
<input type="hidden" name="item_name_2" value="wuiui" />
<input type="hidden" name="amount_2" value="50.00" />
<input type="hidden" name="quantity_2" value="1" />
<input type="hidden" name="custom" value="19" />
<input type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" alt="PayPal - The safer, easier way to pay online">
</form>
I'm not sure why Paypal is doing what it's doing, but I suspect their desire to add their own GET parameters is wiping out your own. You might want to try something like this, if mod_rewrite or something similar is available to you:
write a rule that changes this:
http://mysite/store/order_confirm/?merchant_return_link=Test+Store
to this
http://mysite/index.php?merchant_return_link=Test+Store&module=store&show=order_confirm
Try escaping the ? and & in the return URL; change them to %3F and %26.
An alternative answer is to set the paypal 'rm' [return method] variable to "2", so that paypal would POST all its own return variables, rather than sending them as GET variables

Categories