PHP language development of Paypal payment demo implementation, phppaypal payment demo_PHP tutorial

Source: Internet
Author: User
Tags autoload paypal php composer install
PHP language development of Paypal payment demo implementation, phppaypal payment demo. PHP language development of the specific implementation of the Paypal payment demo, phppaypal payment demo if our application is for international, then the payment usually consider using paypal. The following is the specific implementation of the Paypal payment demo developed by a user in PHP language. the phppaypal payment demo
If our application is international, paypal is usually used for payment. The following is a paypal payment example written by an individual. it has been tested and feasible. A good thing about paypal is that it provides developers with the sandbox test function. (That is, a developer provides a virtual seller account and amount in the development environment, a virtual buyer account and amount, and a virtual card number. So that we don't have to use real money for testing .) I. preparations before development
  • Https://developer.paypal.com/go to the paypal developer's official website to register a developer account.
  • After logging on with an account, click the dashboard on the navigation bar to go to the dashboard panel. The subsequent operations are as follows.
  • In the Accounts under Sandbox in the above menu, you can see the buyer account and seller account for your sandbox test. Both test accounts have the profile option and changepassword to set the password of the virtual account.
  • Transactions under Sandbox in the above menu is your transaction record.
  • Click Create App in the upper-right corner of the page. Create an application. After creation, a Client ID and Secret will be provided to you. These two can be configured as php constants which will be used later in development.
II. go to the payment Demo for development
  • In the development code root directory, you can create an index.html file and put a simple product name and product price. the code is as follows:
  •             
               Payment page                

    Product Name
    Price

  • Enter the product name and price. Click pay to go to the paypal payment page. Use your sandbox testing buyer account to pay. The payment is successful. Then log in to your test seller account. The seller's account has received the payment. Of course, the fees charged by paypal will be deducted here. The service charge is from the seller.
  • Next let's take a look at how php is implemented. First, you need to get the php-sdk provided by paypal to your code directory. This section describes how to use the php package manager composer to obtain the latest sdk. of course, you can obtain the latest paypal php-sdk from github and other channels.
  • By default, composer is installed on your computer. If you do not have to install composer on your own or google.
  • Then, write a composer. json file in the root directory of your code to get the package content. The json file code is as follows:
    {    "require" : {        "paypal/rest-api-sdk-php" : "1.5.1"    }}

    In linux/unix, execute composer install in the root directory to obtain the package content.

  • After installation. The root directory will generate a vendor Directory. There are two sub-directories: composer and paypal. In composer, automatic loading and paypal are your sdk content.
  • Next, write a public file (app/start is used by default. php, which can be customized in your project). In fact, it only implements the sdk autoload. php automatically loads and creates the paypal payment object instance generated by the client id and secret above. The start. php code is as follows:
  •    
  • Next we will implement the process file checkout. php submitted in the form. The code is as follows:
  •    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 (); $ transactio N-> setAmount ($ amount)-> setItemList ($ itemList)-> setDescription ("payment description")-> setInvoiceNumber (uniqid ()); $ 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 in the form. Only common parts are listed here. Paypal provides many parameter settings. For more details, refer to the official paypal developer documentation.

  • After parameters are set in checkout. php. A payment link is generated. Use the header to jump to the payment link (that is, the payment page of paypal) to go to the payment page, you can use the buyer account provided by your sandbox to pay. As follows:
  • After the payment is completed with the buyer account. Check your sandbox merchant account balance. You will find that you have received the money beyond the fee deduction.
  • There is a callback after the payment is successful or fails. The php file for callback processing is set at setReturnUrl in checkout. php above. Is/pay. php set here? Success = true
  • Next, let's take a look at how pay. php simply processes the callback. First paste the code of pay. php:
  •    SetPayerId ($ payerId); try {$ result = $ payment-> execute ($ execute, $ paypal);} catch (Exception $ e) {die ($ e );} echo 'payment successful! Thank you for your support! ';

    Okay. Here, a simple paypal payment demo has actually passed. After understanding the payment principles, and looking for more extensions in your project, go to the official documentation of paypal to view more specific development item settings. Including the acquisition of transaction details, etc. I will not go into detail here.

The demo in this article was tested in using the latest paypal php-sdk version 1.5.1. For example, the sdk version may be slightly different from each other. The borrower can use the reference on his own! I hope to help those who want to develop paypal payment and cannot start!

If our application is international, paypal is usually used for payment. Which of the following is written by an individual...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.