Link to home
Start Free TrialLog in
Avatar of Sjef Bosman
Sjef BosmanFlag for France

asked on

Can you give me an example for a simple PayPal payment?

My client wants to offer a simple payment mechanism to the visitors of his website. The idea is simple: he prepares a link to a specific page, the link contains the amount the visitor has to pay, the link is sent to the visitor (e.g. by mail), the visitor clicks OK and then control should be transferred to PayPal where the actual transaction is handled in a secure way.

Example of a URL (simplified):
     www.somesite.xyz/payment.php?amount=1000&visitor=WileyC

When the link is clicked, a page opens, asking the visitor to click OK if everything is correct,

Here's what I don't know, where it becomes fuzzy for me, and where I need your help.

When OK is clicked, the server should put a lot of data in an HTML form (presumably) and send it to PayPal. PayPal then handles the transaction and reports back OK or Not OK.

Can this be done, and if so, how? Documentation? Good PHP examples??

TIA!
SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sjef Bosman

ASKER

Hi Scott, thanks. My client doesn't want to open PayPal every time he wants to send an invoice. Coding isn't a problem, the whole site is in PHP and XML/HTML.
Gotta go now, I'll explain in more detail in 2 hours.
ALGO:

Create a link say http://site.com/paypal.php?yourkey=value&email=id
When client clicks on the link via email . it would be redirected to paypal.php of your server, which will validate the key & value and later fetch the data from db that has to posted to paypal.
Please note:Client would not be able to trace that it has been redirected via paypal.php and not directly to paypal

Please use key , value pair for validation and security concern.

Please provide the solution what you have found if possible
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@insoftservice: I don't understand your comment. I think you more or less copied my question, i.e. I asked for what you described: the file paypal.php. I'd like a sample server-side paypal.php. I did a similar thing with SystemPay, and that was fairly easy... I just can't seem to find my 'thing' in the PayPal documentation.
@Ray: thanks. I looked into the Buy Now idea, but it's not what I'm looking for. Everything will be fixed: the amount, the shipping, codes, etc. That's not what I want. I'd like to do the calculations of the URL in my code, and then send PayPal the calculated link. Is there such a possibility?

Caution: there is no on-line shop, nothing else, so the logic behind the button should send a simple URL to PayPal that opens a PayPal screen to the website visitor, which should do the rest of the transaction.
In fact, the code that generates the button for SystemPay also generates a form with many hidden fields. These fields contain data plus an encrypted key that matches the data. When clicked the button sends the form data to SystemPay, which responds with a payment page. I could show you part of the code, but it's of no importance if PayPal doesn't have this capability.
I do think you need to break down in more detail where exactly you need help and which method  you want to use.  

A) he prepares a link to a specific page, the link contains the amount the visitor has to pay,

B) the link is sent to the visitor (e.g. by mail),

C) the visitor clicks OK and then control should be transferred to PayPal where the actual transaction is handled in a secure way.


There are multiple options here.   One would be to create an invoice.  https://developer.paypal.com/docs/api/  
https://developer.paypal.com/docs/api/#create-an-invoice
Then send the invoice
https://developer.paypal.com/docs/api/#send-an-invoice

You will see on that page, there is also a link to a php sdk  https://github.com/paypal/rest-api-sdk-php

In the samples you can see the create invoice https://github.com/paypal/rest-api-sdk-php/blob/master/sample/invoicing/CreateInvoice.php
<?php

require __DIR__ . '/../bootstrap.php';
use PayPal\Api\Invoice;
use PayPal\Api\MerchantInfo;
use PayPal\Api\BillingInfo;
use PayPal\Api\InvoiceItem;
use PayPal\Api\Phone;
use PayPal\Api\Address;
use PayPal\Api\Currency;
use PayPal\Api\PaymentTerm;
use PayPal\Api\ShippingInfo;

$invoice = new Invoice();

$invoice
	->setMerchantInfo(new MerchantInfo())
	->setBillingInfo(array(new BillingInfo()))
	->setItems(array(new InvoiceItem()))
	->setNote("Medical Invoice 16 Jul, 2013 PST")
	->setPaymentTerm(new PaymentTerm())
	->setShippingInfo(new ShippingInfo());

$invoice->getMerchantInfo()
	->setEmail("PPX.DevNet-facilitator@gmail.com")
	->setFirstName("Dennis")
	->setLastName("Doctor")
	->setbusinessName("Medical Professionals, LLC")
	->setPhone(new Phone())
	->setAddress(new Address());

$invoice->getMerchantInfo()->getPhone()
	->setCountryCode("001")
	->setNationalNumber("5032141716");

$invoice->getMerchantInfo()->getAddress()
	->setLine1("1234 Main St.")
	->setCity("Portland")
	->setState("OR")
	->setPostalCode("97217")
	->setCountryCode("US");

$billing = $invoice->getBillingInfo();
$billing[0]
	->setEmail("example@example.com");

$items = $invoice->getItems();
$items[0]
	->setName("Sutures")
	->setQuantity(100)
	->setUnitPrice(new Currency());

$items[0]->getUnitPrice()
	->setCurrency("USD")
	->setValue(5);

$invoice->getPaymentTerm()
	->setTermType("NET_45");

$invoice->getShippingInfo()
	->setFirstName("Sally")
	->setLastName("Patient")
	->setBusinessName("Not applicable")
	->setPhone(new Phone())
	->setAddress(new Address());

$invoice->getShippingInfo()->getPhone()
	->setCountryCode("001")
	->setNationalNumber("5039871234");

$invoice->getShippingInfo()->getAddress()
	->setLine1("1234 Main St.")
	->setCity("Portland")
	->setState("OR")
	->setPostalCode("97217")
	->setCountryCode("US");

print(var_dump($invoice->toArray()));

try {
	$invoice->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
	echo "Exception: " . $ex->getMessage() . PHP_EOL;
	var_dump($ex->getData());
	exit(1);
}
?>
<html>
<head>
	<title>Direct Credit card payments</title>
</head>
<body>
	<div>
		Created Invoice:
		<?php echo $invoice->getId();?>
	</div>
	<pre><?php var_dump($invoice->toArray());?></pre>
	<a href='../index.html'>Back</a>
</body>
</html>

Open in new window

and send
https://github.com/paypal/rest-api-sdk-php/blob/master/sample/invoicing/SendInvoice.php
<?php

require __DIR__ . '/../bootstrap.php';

use PayPal\Api\Invoice;

try {
	$invoice = Invoice::get("INV2-9DRB-YTHU-2V9Q-7Q24", $apiContext);

	$sendStatus = $invoice->send($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
	echo "Exception: " . $ex->getMessage() . PHP_EOL;
	var_dump($ex->getData());
	exit(1);
}

?>
<html>
<head>
	<title>Send Invoice</title>
</head>
<body>
	<div>Send Invoice:</div>
	<pre><?php var_dump($invoice);?></pre>
	<a href='../index.html'>Back</a>
</body>
</html>

Open in new window


I would get yourself a sandbox account and start with the samples.  Get those to work, then update with your own details.
The A->B->C is correct, and the help I'd like to have is mostly related to A: how to prepare the page and the link. From your comments I kind of get the idea that PayPal doesn't support what I'm looking for.

I'll try to re-explain what I would like my code and PayPal to do (basically).

1. the client opens a hidden page on the site, enters the client's payment data (amout, reference, etc.)
2. the server responds with a (crypted) link that contains the payment data; the link is to the same website
3. the client copies the link into a mail and sends it to the visitor.

4. the visitor clicks the link and opens another hidden page on the website; this page displays the payment data that is enclosed in the crypted part of the link
5. on this same page, the server generates the Buy Now button
6. if the visitor accepts the transaction, he can click the Buy Now button; in that case the real payment info is sent to the bank.

So, as far as I can see, there is no need to send an invoice by PayPal, it's just a money transfer.

Hmm, I'll ask my client how the invoice is sent to the visitor. I suppose he intends to send his own.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The idea of the invoice does exactly what you outline, except it is much easier.  You can do this via the api or by logging into the paypal directly.  What you have outlined is really overly complex

If you are not logging any type of inventory and simply using the hidden page on his site to generate the request for payment/url, the easiest is my original idea of simply logging into his paypal account and generating an invoice that will auto email his customer.   It is very simple and I bill my own clients this way.

Otherwise, you still can do what you want, but it is more involved.  First you have to generate the items you want to sell or at least an amount.  Then the final amount you send to paypal's server to generate the button code (or a link).  Then you supply the button code back to your site or your client's customer who will then click the button or link and go to paypal.  

The reason it is done in this fashion is to prevent somebody from spoofing the amount.  If you were to manually generate a buy now button, notice the html generated does not include an amount, that is because it is saved on the paypal server.

You may want to look at the classic api and express checkout https://developer.paypal.com/docs/classic/products/express-checkout/
do the calculations of the URL in my code, and then send PayPal the calculated link
Entirely feasible.  You can prepopulate any of the form fields you need to use PayPal.

Without getting into the technicalities, can you please describe the business aspects of the transaction?  For example, is it a purchase of a product, or a donation?
My client produces wooden signs , elements, ornaments, etc. on demand, so there's always a quotation before anything is produced. There are therefore no prices on the website. The visitor places an order, and for the payment of this order the visitor can use PayPal, or send a cheque (only in France), or do a money transfer. Prices are around €200, that should be around $250 at the mo. The website isn't finished yet, and then there is the translation, but he hopes to open his shoppe on Sept. 1st.

The Adaptive Payments API seems what I need, what I read there is quite promising. Straightforward as well.
You might consider sending a PayPal invoice via email.  The client can pay directly with the link in the email.  And there is the "send money" feature of PayPal that can be used by the client, but that requires the client to understand the process, and it might be too much friction when compared to a web page that gives instructions and receives payments.
Would you believe me if I told you that the client now says that he's willing to consider the PayPal invoicing system?? Argh...
Like both Ray and I have suggested, if you can do this right from paypal it is super easy.  Otherwise, if you want to do this programmatically, I pointed you to the api http:#a40208814  You will still need to build the portion where you are updating rows of data to generate the amount due.
I think that Adaptive Payments are what I really meant to use, so that would be the solution to my question. The final solution to the problem is, hopefully, the PayPal invoice, as suggested by Scott. Thank you guysl!