Billing

Read the article Paypal to learn how to configure your site for payments with Paypal in test mode and in production mode.
NOTE: This article is based on the interface with PayPal. If you use Payline, or another payment system, the principles are identical and your code will be very similar with just a few adaptations for the API of payment system.
You have validated you configuration in test mode with the donation form
.
You want to change the logo which is displayed by Paypal and give in more details to the buyer.
Add an image file, a JPG or a PNG, in the directory logos. Name it sitepaypal.jpg or sitepaypal.png. This image will appear at the top left of the payment page. It has a maximum size of 750x90 px.
Edit the file strings.inc in the folder includes.
Add the translation of payment:name and payment:desc in English:
'payment:desc' => 'Subscription 1 year',
And in French:
'payment:desc' => 'Abonnement 1 année',
Edit the function paypalcheckout in the file paypalcheckout.php of the folder actions.
Redefine $name and add $desc:
- $name=translate('payment:name', $lang);
- $desc=translate('payment:desc', $lang);
Redefine $hdrimg:
- $hdrimg=$base_url . '/logos/sitepaypal.png';
Edit the parameters HDRIMG, L_PAYMENTREQUEST_0_NAME0 and L_PAYMENTREQUEST_0_DESC0 passed by the array $params to the function paypal_setexpresscheckout:
- 'L_PAYMENTREQUEST_0_NAME0' => $name,
- 'L_PAYMENTREQUEST_0_DESC0' => $desc,
- 'HDRIMG' => $hdrimg,
Validate a complete payment by the site with the donation form. Check that the new logo is displayed at the top of the payment page by Paypal.
Create a table in the database called payment:
`payment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) UNSIGNED NOT NULL,
`paypal_id` CHAR(17) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`payed` datetime NOT NULL,
`amount` DECIMAL(7,2) UNSIGNED NOT NULL,
`vat` DECIMAL(7,2) UNSIGNED NOT NULL,
`fee` DECIMAL(7,2) UNSIGNED NOT NULL,
`currency` enum('EUR','USD','GBP') NOT NULL DEFAULT 'EUR',
`billing_name` VARCHAR(100) NOT NULL,
`billing_address` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`payment_id`)
) DEFAULT CHARSET=utf8;
NOTE: Adapt the prefix of the table to your configuration. If you accept only one currency, you can drop the field currency.
Create the file payment.inc in the folder models with the following content:
- function payment_vat_rate() {
- return 0.20;
- }
payment_vat_rate returns the VAT rate applicable to a payment.
The code of this function can take into account the type of product which is sold and deal with the case of export sales.
- function payment_log($user_id, $transactionid, $amount, $vat, $fee, $currency, $billing_name, $billing_address) {
- if (!is_numeric($user_id)) {
- return false;
- }
- $sqlpaymentid=db_sql_arg($transactionid, false);
- $sqlcurrency=db_sql_arg($currency, false);
- $sqlbillingname=db_sql_arg($billing_name, true);
- $sqlbillingaddress=db_sql_arg($billing_address, true);
- $tabpayment=db_prefix_table('payment');
- $sql="INSERT $tabpayment (user_id, paypal_id, payed, amount, vat, fee, currency, billing_name, billing_address) VALUES ($user_id, $sqlpaymentid, NOW(), $amount, $vat, $fee, $sqlcurrency, $sqlbillingname, $sqlbillingaddress)";
- $r = db_insert($sql);
- if (!$r) {
- return false;
- }
- $payment_id = db_insert_id();
- return $payment_id;
- }
payment_log saves the details of a payment in the database.
- function payment_detail($payment_id) {
- if (!is_numeric($payment_id)) {
- return false;
- }
- $tabpayment=db_prefix_table('payment');
- $sql="SELECT user_id AS payment_user_id, UNIX_TIMESTAMP(payed) AS payment_payed, amount AS payment_amount, vat AS payment_vat, fee AS payment_fee, currency AS payment_currency, billing_name AS payment_billing_name, billing_address AS payment_billing_address FROM $tabpayment WHERE payment_id=$payment_id";
- $r = db_query($sql);
- return $r ? $r[0] : false;
- }
payment_detail returns all the information saved in the database about a payment.
- function user_get_payments($user_id) {
- if (!is_numeric($user_id)) {
- return false;
- }
- $tabpayment=db_prefix_table('payment');
- $sql="SELECT payment_id AS payment_id, paypal_id AS payment_paypal_id, UNIX_TIMESTAMP(payed) as payment_payed, amount AS payment_amount, vat AS payment_vat, fee AS payment_fee, currency AS payment_currency, billing_name AS payment_billing_name, billing_address AS payment_billing_address FROM $tabpayment WHERE user_id=$user_id ORDER BY payed DESC";
- $r = db_query($sql);
- return $r;
- }
user_get_payments returns all the information about all the payments made by a user.
Edit the function paymentaccepted in the file paymentaccepted.php of the folder actions.
- require_once 'models/payment.inc';
Loads the code of the function payment_log.
- function paymentaccepted($lang, $amount, $currency, $tax, $transactionid, $fee, $context) {
Adds the arguments $tax, $transactioni and $fee to the function paymentaccepted.
- $user_id = $context['user_id'];
- $billing_name = $context['billing_name'];
- $billing_address = $context['billing_address'];
- $r = payment_log($user_id, $transactionid, $amount, $tax, $fee, $currency, $billing_name, $billing_address);
- if (!$r) {
- return run('error/internalerror', $lang);
- }
Extracts the information on the identity of the buyer from the context of the operation. Records the payment in the database.
- require_once 'serveripaddress.php';
- require_once 'emailme.php';
- global $sitename;
- $ip=server_ip_address();
- $timestamp=date('Y-m-d H:i:s', time());
- $subject = 'payment' . '@' . $sitename;
- $msg = $ip . ' ' . $timestamp . ' ' . $user_id . ' ' . $transactionid . ' ' . $amount . ' ' . $currency;
- @emailme($subject, $msg);
Sends a notification about the payment by email to the administrator of the website.
Edit the function paypalreturn in the file paypalreturn.php of the folder actions and add the parameters $taxamt, $transactionid and $feeamt to the call of the function paymentaccepted:
- $output = paymentaccepted($lang, $amt, $currencycode, $taxamt, $transactionid, $feeamt, $context);
Test
In order to test the recording of a payment, add passing the information about the buyer to the call of the function Do a payment in test mode with the donation form IMPORTANT: Block the access to the donation form by editing the file aliases.inc in the folder includes. Create the file invoice.phtml in the folder layouts with the following content: Create the version in English of the file invoice.phtml in the folder views/en with the following content: Create the version in French of the file invoice.phtml in the folder views/fr with the following content: Create the file invoice.css in the folder css with the following content: Create the file invoice.php in the folder actions with the following content: In the file aliases.inc of the folder includes, associate the action Connect as an administrator and enter the addresse /en/invoice/1 after the domain name of the site to display the invoice 1.paypalcheckout in the block donateme
and check the content of the table payment in the database.invoice to the URL /en/facture and to the URL /fr/invoice:
Comments