search
HomeBackend DevelopmentPHP TutorialSpecific implementation of Paypal payment demo developed in PHP language, phppaypal payment demo_PHP tutorial

The specific implementation of Paypal payment demo developed in PHP language, phppaypal payment demo

If our application is for international applications, we usually consider using paypal when making payments. The following is a PayPal payment example written by an individual, which has been personally tested and is feasible. A very good thing about PayPal is that it provides developers with a sandbox testing function. (That is, it provides developers with a virtual seller account and amount, a virtual buyer account and amount, virtual card number, etc. in the development environment. This allows us to test without using real money.)

1. Preparation before development

  • https://developer.paypal.com/ Go to the official developer website of paypal to register a developer account.
  • After logging in with your account, click dashboard on the navigation to enter the dashboard. As shown in the screenshot below, subsequent operations are performed in this panel.
  • You can see the buyer account and seller account of your sandbox test in Accounts under the menu Sandbox in the screenshot above. Both test accounts have a profile option with changepassword to set the password for the virtual account.
  • Transactions under 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 app. After creation, you will be provided with a Client ID and Secret. These two can be configured as PHP constants and will be used in later development.

2. Enter payment demo development

  • Create a development code root directory locally. First create an index.html and put two simple input items: product name and product price. The code and screenshots are as follows:
  • <span><!</span><span>DOCTYPE html</span><span>></span>
    <span><</span><span>html </span><span>lang</span><span>="en"</span><span>></span>
        <span><</span><span>head</span><span>></span>
            <span><</span><span>meta </span><span>charset</span><span>="utf-8"</span><span>></span>
            <span><</span><span>title</span><span>></span>支付页面<span></</span><span>title</span><span>></span>
        <span></</span><span>head</span><span>></span>
        <span><</span><span>body</span><span>></span>
            <span><</span><span>div</span><span>></span>
                <span><</span><span>form </span><span>action</span><span>="checkout.php"</span><span> method</span><span>="post"</span><span> autocomplete</span><span>="off"</span><span>></span>
                    <span><</span><span>label </span><span>for</span><span>="item"</span><span>></span><span>
                        产品名称
                        </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="product"</span><span>></span>
                    <span></</span><span>label</span><span>></span>
                    <span><</span><span>br</span><span>></span>
                    <span><</span><span>label </span><span>for</span><span>="amount"</span><span>></span><span>
                        价格
                        </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="price"</span><span>></span>
                    <span></</span><span>label</span><span>></span>
                    <span><</span><span>br</span><span>></span>
                    <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="去付款"</span><span>></span>
                <span></</span><span>form</span><span>></span>
            <span></</span><span>div</span><span>></span>
        <span></</span><span>body</span><span>></span>
    <span></</span><span>html</span><span>></span>

  • Enter product name and price. Click to pay and you will go 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 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 closer look at how php is implemented. First, you need 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 the latest SDK. Of course, you can obtain the latest paypal PHP-SDK from other channels such as github.
  • Composer is already installed on your computer by default. If you don’t have it, go to Du Niang or google to install it with composer.
  • Then write a composer.json file in your code root directory to get the package content. The json file code is as follows:
    <span>{
        </span>"require" :<span> {
            </span>"paypal/rest-api-sdk-php" : "1.5.1"<span>
        }
    }</span>

    If this is a linux/unix system, just execute composer install directly in the root directory to obtain the package content.

  • After installation. A vendor directory will be 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, you can customize it in your project). In fact, it just implements the autoload.php of sdk to automatically load and create the client mentioned above. Paypal payment object instance generated by id and secret. The start.php code is as follows:
  • <?<span>php
    </span><span>require</span> "vendor/autoload.php"; <span>//</span><span>载入sdk的自动加载文件</span>
    <span>define</span>('SITE_URL', 'http://www.paydemo.com'); <span>//</span><span>网站url自行定义
    //创建支付对象实例</span>
    <span>$paypal</span> = <span>new</span><span> \PayPal\Rest\ApiContext(
        </span><span>new</span><span> \PayPal\Auth\OAuthTokenCredential(
            </span>'你的Client ID'
            '你的secret'<span>
        )
    );</span>
  • The next step is to implement the processing file checkout.php submitted in the form. The code content is as follows:
  • <?<span>php
    </span><span>/*</span><span>*
     * @author xxxxxxxx
     * @brief 简介:
     * @date 15/9/2
     * @time 下午5:00
     </span><span>*/</span>
    <span>use</span><span> \PayPal\Api\Payer;
    </span><span>use</span><span> \PayPal\Api\Item;
    </span><span>use</span><span> \PayPal\Api\ItemList;
    </span><span>use</span><span> \PayPal\Api\Details;
    </span><span>use</span><span> \PayPal\Api\Amount;
    </span><span>use</span><span> \PayPal\Api\Transaction;
    </span><span>use</span><span> \PayPal\Api\RedirectUrls;
    </span><span>use</span><span> \PayPal\Api\Payment;
    </span><span>use</span> \PayPal\<span>Exception</span><span>\PayPalConnectionException;
    
    </span><span>require</span> "app/start.php"<span>;
    </span><span>if</span> (!<span>isset</span>(<span>$_POST</span>['product'], <span>$_POST</span>['price'<span>])) {
        </span><span>die</span>("lose some params"<span>);
    }
    </span><span>$product</span> = <span>$_POST</span>['product'<span>];
    </span><span>$price</span> = <span>$_POST</span>['price'<span>];
    </span><span>$shipping</span> = 2.00; <span>//</span><span>运费</span>
    
    <span>$total</span> = <span>$price</span> + <span>$shipping</span><span>;
    
    </span><span>$payer</span> = <span>new</span><span> Payer();
    </span><span>$payer</span>->setPaymentMethod('paypal'<span>);
    
    </span><span>$item</span> = <span>new</span><span> Item();
    </span><span>$item</span>->setName(<span>$product</span><span>)
        </span>->setCurrency('USD'<span>)
        </span>->setQuantity(1<span>)
        </span>->setPrice(<span>$price</span><span>);
    
    </span><span>$itemList</span> = <span>new</span><span> ItemList();
    </span><span>$itemList</span>->setItems([<span>$item</span><span>]);
    
    </span><span>$details</span> = <span>new</span><span> Details();
    </span><span>$details</span>->setShipping(<span>$shipping</span><span>)
        </span>->setSubtotal(<span>$price</span><span>);
    
    </span><span>$amount</span> = <span>new</span><span> Amount();
    </span><span>$amount</span>->setCurrency('USD'<span>)
        </span>->setTotal(<span>$total</span><span>)
        </span>->setDetails(<span>$details</span><span>);
    
    </span><span>$transaction</span> = <span>new</span><span> Transaction();
    </span><span>$transaction</span>->setAmount(<span>$amount</span><span>)
        </span>->setItemList(<span>$itemList</span><span>)
        </span>->setDescription("支付描述内容"<span>)
        </span>->setInvoiceNumber(<span>uniqid</span><span>());
    
    </span><span>$redirectUrls</span> = <span>new</span><span> RedirectUrls();
    </span><span>$redirectUrls</span>->setReturnUrl(SITE_URL . '/pay.php?success=true'<span>)
        </span>->setCancelUrl(SITE_URL . '/pay.php?success=false'<span>);
    
    </span><span>$payment</span> = <span>new</span><span> Payment();
    </span><span>$payment</span>->setIntent('sale'<span>)
        </span>->setPayer(<span>$payer</span><span>)
        </span>->setRedirectUrls(<span>$redirectUrls</span><span>)
        </span>->setTransactions([<span>$transaction</span><span>]);
    
    </span><span>try</span><span> {
        </span><span>$payment</span>->create(<span>$paypal</span><span>);
    } </span><span>catch</span> (PayPalConnectionException <span>$e</span><span>) {
        </span><span>echo</span> <span>$e</span>-><span>getData();
        </span><span>die</span><span>();
    }
    
    </span><span>$approvalUrl</span> = <span>$payment</span>-><span>getApprovalLink();
    </span><span>header</span>("Location: {<span>$approvalUrl</span>}");

    checkout.php initializes and sets the specific payment details and parameters through the parameters submitted through the form. Only the commonly used parts are listed here. PayPal provides many parameter settings. For more details, you can refer to Paypal's official developer documentation.

  • After checkout.php sets the parameters. A payment link will be generated. Use the header to jump to the payment link (that is, the payment page of Paypal). When you go to the payment page, you can use the buyer account provided by your sandbox to pay. The screenshot is as follows:
  • After the payment is completed using the buyer account. Go check the merchant account balance of your sandbox. You will find that you have received the money minus the handling fee.
  • There is a callback processing after the payment is successful or failed. The php file for callback processing is set at setReturnUrl in 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. First paste the pay.php code:
  • <?<span>php
    
    </span><span>require</span> 'app/start.php'<span>;
    
    </span><span>use</span><span> PayPal\Api\Payment;
    </span><span>use</span><span> PayPal\Api\PaymentExecution;
    
    </span><span>if</span>(!<span>isset</span>(<span>$_GET</span>['success'], <span>$_GET</span>['paymentId'], <span>$_GET</span>['PayerID'<span>])){
        </span><span>die</span><span>();
    }
    
    </span><span>if</span>((bool)<span>$_GET</span>['success']=== 'false'<span>){
    
        </span><span>echo</span> 'Transaction cancelled!'<span>;
        </span><span>die</span><span>();
    }
    
    </span><span>$paymentID</span> = <span>$_GET</span>['paymentId'<span>];
    </span><span>$payerId</span> = <span>$_GET</span>['PayerID'<span>];
    
    </span><span>$payment</span> = Payment::get(<span>$paymentID</span>, <span>$paypal</span><span>);
    
    </span><span>$execute</span> = <span>new</span><span> PaymentExecution();
    </span><span>$execute</span>->setPayerId(<span>$payerId</span><span>);
    
    </span><span>try</span><span>{
        </span><span>$result</span> = <span>$payment</span>->execute(<span>$execute</span>, <span>$paypal</span><span>);
    }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>){
        </span><span>die</span>(<span>$e</span><span>);
    }
    </span><span>echo</span> '支付成功!感谢支持!';

    Okay. At this point, a simple PayPal payment demo has actually worked. After understanding the payment principle, if you want to make richer extensions in your own project, go to Paypal's official documentation to view more specific development project settings. Including the acquisition of transaction details, etc. are all achievable. I won’t go into details here.

The demo in this article was tested on 2015-9-2 using the latest paypal php-sdk version 1.5.1. If the SDK versions are far apart, there may be slight differences.

Borrowers can just use it for reference! I hope it can bring some help to those who just want to develop PayPal payment but really have no idea where to start!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1054178.htmlTechArticleThe specific implementation of Paypal payment demo developed in PHP language, phppaypal payment demo If our application is for international, then payment I usually consider using paypal. The following is written by an individual...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
使用PHP和PayPal实现在线支付使用PHP和PayPal实现在线支付May 11, 2023 pm 03:37 PM

随着互联网的迅猛发展,越来越多的企业选择将产品和服务线上销售,这使得在线支付成为企业的一大需求。而PayPal作为世界领先的在线支付平台,也成为了许多企业的首选。本文将介绍如何使用PHP和PayPal实现在线支付。我们将分为以下几个步骤:创建PayPal账号和应用集成PayPalSDK获取付款Token处理付款处理付款确认创建PayPal账号和应用要使用P

paypal无法付款的原因是什么paypal无法付款的原因是什么Sep 01, 2023 pm 05:00 PM

paypal无法付款的原因是账户余额不足、付款方式被限制、交易被风控系统拦截、收款方账户问题、网络连接问题以及用户账户异常等。详细介绍:1、账户余额不足,可以通过银行转账或信用卡充值来增加账户余额;2、付款方式被限制,查看付款设置并确保所选的付款方式没有受到限制;3、交易被风控系统拦截,联系PayPal客服,提供相关信息以证明交易的合法性,并请求解除付款限制等等。

GCash launches PayPal's stable coin, allowing Filipinos to trade cryptocurrency protected from price volatilityGCash launches PayPal's stable coin, allowing Filipinos to trade cryptocurrency protected from price volatilityJul 31, 2024 am 06:36 AM

GCash on Tuesday said PayPal USD (PYUSD) tokens could now be traded via GCrypto, an in-app feature powered by the Philippine Digital Asset Exchange, at “low transaction fees.”

paypal无法付款是什么原因paypal无法付款是什么原因Oct 16, 2023 pm 03:23 PM

paypal无法付款是因为付款方式、账户余额、Paypal余额、付款信息、网络问题、Paypal系统、商家和浏览器等问题造成的。详细介绍:1、付款方式,请确使用的付款方式已经添加到Paypal账户中;2、账户余额,确保Paypal账户余额足够支付订单金额;3、Paypal余额,查看账户状态,了解是否存在异常情况;4、付款信息,确保输入的付款信息正确无误,如信用卡号、到期日期等。

欧洲人用paypal吗欧洲人用paypal吗Nov 10, 2022 am 10:52 AM

欧洲人用paypal,但不是通用的,只有开通的地区才可以使用;PayPal是一个总部在美国加利福尼亚州圣荷塞市的在线支付服务商;PayPal账户是PayPal公司推出的安全的网络电子账户,使用它可有效降低网络欺诈的发生;PayPal账户所集成的高级管理功能,能掌控每一笔交易详情。

Paypal's PYUSD Nears $1B MilestonePaypal's PYUSD Nears $1B MilestoneAug 17, 2024 am 06:10 AM

The stablecoin asset issued by Paypal is now the sixth largest stablecoin asset today after growing significantly over the past ten days.

paypal官方app下载paypal官方app下载Apr 23, 2024 am 10:00 AM

要下载 PayPal 官方应用程序,请访问 PayPal 官方网站:https://www.paypal.com/ 单击“下载”,根据您的设备选择相应应用程序商店,搜索“PayPal”,下载并安装,最后登录您的 PayPal 账户。该应用程序可让您轻松管理账户、确保安全、跟踪支出、无缝付款,并适用于 iOS 和 Android 设备。

PayPal联手苹果Tap to Pay,数百万美国小企业实现iPhone免接触支付PayPal联手苹果Tap to Pay,数百万美国小企业实现iPhone免接触支付Apr 10, 2024 pm 12:10 PM

3月8日消息,贝宝(PayPal)控股有限公司近日发布公告,宣布数百万美国小企业,这些企业均是Venmo和PayPalZettle的用户,现在无需任何额外硬件如扩展配件或蓝牙读卡器,只需通过支持苹果的TaptoPay功能,便可仅靠一台iPhone实现免接触式支付。苹果公司于2022年5月推出的TaptoPay功能,使得美国商家可以利用iPhone及与支持商家的iOS应用程序来接受ApplePay和其他免接触式支付方式。通过此服务,使用兼容iPhone设备的用户能够安全地处理免接触式支付以及已启用

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)