2

Currently I'm using PayPal buttons for receiving payments from users. I have a website that automatically calculates postage cost for a particular item, and I want to update the cost on the PayPal page to reflect the shipping costs. How do I accomplish this?

I'm thinking there should be a way to dynamically create an encrypted PayPal "Buy Now" button in PayPal, then display that form to the user, but the documentation on how to do this is scattered.

If possible I want to avoid recording transactions in a database and verifying. I just want a PayPal button that I can securely change the shipping cost of, disallowing users from setting the cost manually.

prunmit
  • 31
  • 4

2 Answers2

1

Create PayPal certificates following instructions: https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/encryptedwebpayments/#id08A3I0P20E9

In PHP:

define('OPENSSL', '/usr/bin/openssl');

class PayPal {
    public function __construct() {
        $this->key_file = 'my-prvkey.pem';
        $this->cert_file = 'my?pubcert.pem';
        $this->paypal_key = 'paypal_cert.pem';
        $this->button = array(
            'cert_id' => 'YOUR CERT ID',
            'cmd' => '_xclick',
            'business' => 'YOUR PAYPAL EMAIL',
            'lc' => 'US',
            'item_name' => 'ITEM NAME',
            'amount' => 'X',
            'currency_code' => 'USD',
            'button_subtype' => 'services',
            'no_note' => '0',
            'bn' => 'PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest'
        );
    }

    public function create_form($shipping) {
        $this->button['shipping'] = $shipping;
        return '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"><input name="cmd" type="hidden" value="_s-xclick" />
<input name="encrypted" type="hidden" value="'.$this->encrypt().'" />
<input type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" alt="" width="1" height="1" border="0" /></form>';
    }

    private function encrypt() {
        $data = '';
        foreach ($this->button as $key => $value) {
            if ($value) $data .= "$key=$value\n";
        }

        $cmd = '('.OPENSSL." smime -sign -signer {$this->cert_file} -inkey {$this->key_file} " .
            "-outform der -nodetach -binary <<_EOF_\n{$data}\n_EOF_\n) | " .
            OPENSSL." smime -encrypt -des3 -binary -outform pem {$this->paypal_key}";

        exec($cmd, $output, $error);
        return implode("\n", $output);
    }
}

Then to output the dynamic PayPal form:

$paypal = new Paypal();
echo $paypal->create_form(20);
prunmit
  • 31
  • 4
0

Consider using braintree which was acquired by PayPal for a more simplified PayPal implementation.

To answer your question, you should have the postage calculation occur before sending the purchase data to PayPal so that the user confirms the final amount.

I'm unfamiliar with the buttons but their implementation needs to allow for shipping costs to fluctuate.

Obviously PayPal will not allow a price change after the user has confirmed a lower/different price.

Vladimir Ramik
  • 1,920
  • 2
  • 13
  • 23
  • Braintree doesn't seem any easier. I already have postage calculation, I want to know how to send that purchase data to PayPal. That's what my question is asking. – prunmit Mar 08 '15 at 06:03
  • The code isn't relevant. I'm looking for code that'll create a button, or redirect a user to a PayPal checkout page. An API, a library, *anything*. The PayPal documentation is so poor it's unbearable. – prunmit Mar 08 '15 at 06:48
  • I'll revert back to suggesting you take a look at braintree's PayPal php library. Very straightforward. – Vladimir Ramik Mar 08 '15 at 07:39
  • That may be so, but I can't use Braintree at this time due to business constraints. I'm specifically looking for a PayPal solution, not a Braintree PayPal solution. – prunmit Mar 08 '15 at 21:46