Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concrete implementation of developing Paypal payment demo with PHP language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail about the specific implementation of Paypal payment demo developed by PHP language. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

I. preparation before development

Https://developer.paypal.com/ goes to paypal's developer website to register a developer account.

After logging in with your account, click the dashboard above the navigation to enter the dashboard version. The screenshot below and the subsequent operations are all done in this panel.

The buyer account and seller account of your sandbox test can be seen in the Accounts under the menu Sandbox in the screenshot above. Both test accounts have the profile option and changepassword can set the password for the virtual account.

The Transactions below the menu Sandbox in the screenshot above is your transaction record.

Click the Create App button in the upper right corner of the screenshot page. Create an application. After the creation, you will be provided with a Client ID and Secret. These two can be configured as php constants that will be used later in development.

Enter the development of payment Demo

Create a root directory of the development code locally, first create an index.html and put a simple input item of product name and product price in it. The code and screenshots are as follows:

DOCTYPE html > payment page title > head > Product name label >

Price label >

Form > div > body > html >

Enter the product name and price. Click to pay and it will take you to the paypal payment page. Use your sandbox test buyer account to pay. You will find that the payment is successful. Then log in to your test seller account. You will find that the seller's account has received the payment. Of course, the handling fee charged by paypal will be deducted here. The handling fee is charged by the seller.

Let's take a look at how php is implemented. The first step is to get the php-sdk provided by paypal into your code directory. Here we introduce how to use php's package manager composer to obtain * sdk. Of course, you can obtain * paypal php-sdk from other channels such as github.

By default, your computer has already installed composer. If you don't go to Duniang or google to install composer.

Then write a composer.json file in your code root to get the contents of the package. The json file code is as follows:

{

"require": {"paypal/rest-api-sdk-php": "1.5.1"

}

}

Here, if it is a linux/unix system, execute composer install directly from the root directory to get the package contents.

After installation. A vendor directory is generated under the root directory. There are two subdirectories, composer and paypal. Composer implements automatic loading, and paypal is your sdk content.

Next, let's write a public file (app/start.php is used by default here, which can be customized in your project). In fact, it only implements the autoload.php automatic loading of sdk and the creation of the paypal payment object instance generated by client id and secret just above. The start.php code is as follows:

Php

Require "vendor/autoload.php"; / / load sdk's auto-load file define ('SITE_URL',' http://www.paydemo.com'); / / website url self-defined / / create payment object instance $paypal = new\ PayPal\ Rest\ ApiContext (new\ PayPal\ Auth\ OAuthTokenCredential ('your Client ID'' your secret')

)

);

The next step is to implement the processing file checkout.php submitted in the form. The code is as follows:

Php

/ * *

* @ author xxxxxxxx

* @ brief introduction:

* @ date 15-9-2

* @ time 5:00 p.m.

, /

Use\ PayPal\ Api\ Payer

Use\ PayPal\ Api\ Item

Use\ PayPal\ Api\ ItemList

Use\ PayPal\ Api\ Details

Use\ PayPal\ Api\ Amount

Use\ PayPal\ Api\ Transaction

Use\ PayPal\ Api\ RedirectUrls

Use\ PayPal\ Api\ Payment

Use\ PayPal\ Exception\ PayPalConnectionException

Require "app/start.php"; if (! isset ($POST ['product'], $_ POST [' price']) {die ("lose some params");} $product = $_ POST ['product']; $price = $_ POST [' price']; $shipping = 2.00; / / Freight $total = $price + $shipping; $payer = new Payer (); $payer- > setPaymentMethod ('paypal'); $item = new Item () $item- > setName ($product)-> setCurrency ('USD')-> setQuantity (1)-> setPrice ($price); $itemList = new ItemList (); $itemList- > setItems ([$item]); $details = new Details (); $details- > setShipping ($shipping)-> setSubtotal ($price); $amount = new Amount (); $amount- > setCurrency (' USD')-> setTotal ($total)-> setDetails ($details); $transaction = new Transaction (); $transaction- > setAmount ($amount)-> setItemList ($itemList)-> setDescription ("payment description")-> setDescription () $redirectUrls = new RedirectUrls (); $redirectUrls- > setReturnUrl (SITE_URL. '/ pay.php?success=true')-> setCancelUrl (SITE_URL. '/ pay.php?success=false'); $payment = new Payment (); $payment- > setIntent (' sale')-> setPayer ($payer)-> setRedirectUrls ($redirectUrls)-> setTransactions ([$transaction]); try {$payment- > create ($paypal);} catch (PayPalConnectionException $e) {echo $e > getData (); die ();} $approvalUrl = $payment- > getApprovalLink (); header ("Location: {$approvalUrl}")

Checkout.php initializes and sets the payment details and parameters through the parameters submitted by the form. Only the commonly used parts are listed here. Paypal provides a number of parameter settings. For more details, you can refer to the official developer documentation of paypal.

After checkout.php sets the parameters. A payment link is generated. Use header to jump to the payment link (that is, paypal's payment page) to the payment page and you can use the buyer account provided by your sandbox to pay.

After the payment is completed with the buyer account. Check your sandbox merchant account balance. You will find that you have received money other than the service charge.

Here there is a callback after the payment succeeds or fails. The php file processed by the callback is set at setReturnUrl in the checkout.php above. What is set here is / pay.php?success=true

Next, let's take a look at how pay.php simply handles callbacks. Paste the code of pay.php first:

Php

Require 'app/start.php'

Use PayPal\ Api\ Payment

Use PayPal\ Api\ PaymentExecution

If (! isset ($_ GET ['success'], $_ GET [' paymentId'], $_ GET ['PayerID'])) {

Die ()

}

If ((bool) $_ GET ['success'] = =' false') {

Echo 'Transaction cancelledholders'

Die ()

}

$paymentID = $_ GET ['paymentId']

$payerId = $_ GET ['PayerID']

$payment = Payment::get ($paymentID, $paypal)

$execute = new PaymentExecution ()

$execute- > setPayerId ($payerId)

Try {

$result = $payment- > execute ($execute, $paypal)

} catch (Exception $e) {

Die ($e)

}

Echo 'payment succeeded! Thank you for your support.

Okay. Here a simple paypal pay demo is already available. Once you understand the principle of payment and want to expand more richly in your own project, go to paypal's official documentation to see more specific development project settings. Including the acquisition of transaction details and so on can be achieved. I won't go into details here.

On the PHP language development of Paypal payment demo how the specific implementation is shared here, I hope that the above content can be of some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report