search
HomeBackend DevelopmentPHP Tutorialthinkphp框架中引出ping++支付server SDK

thinkphp框架中引入ping++支付server SDK

ping++支付平台整合了支付宝,微信支付,百度钱包,银联支付等主流支付方式。而且其开发文档,也很容易理解和上手,所以想写篇博客,记录下来(thinkphp)服务器端使用其SDK的步骤【test模式下】。

    开始之前 最好先看一看官方文档对于支付过程的解释:



第一步:去www.pingxx.com新注册一个账号。注册完成进入个人中心后,你将会得到一些参数:

Test Secret Key:(该参数在server端配置,用于和客户端的Test Publishable Key配合使用)

ping++公钥:(支付完成后,平台往server端返回webhooks,此参数用于验证交易安全性)

Test Publishable Key:(该参数用于客户端,与上面server端的的Test Serect Key配合使用)

④商户公钥:(用于server端和平台交易时,RSA签名的验证),至于如何产生商户公钥对,根据图中蓝色字的引导,很容易就完成了,此处不再赘述。产生了公钥对之后,把公钥填写在下图中的textarea中,私钥留着先,后面有用。

第二步:下载官方的server端SDK,PHP的下载链接是https://github.com/PingPlusPlus/pingpp-php

下载好了之后,加压出来,放在thinkphp第三方库扩展目录里面:

如左图所示,lib文件夹,init.php,还有第一步④产生的公钥对中的

私钥放进去。至于data文件夹里的文件,是使用HTTPS时用的安全证书,暂时也拿过来。

第三步:在代码中使用上面的参数,看看引入是否成功:(示例代码如下)

    public function index(){    	$api_key = '请填写自己的Test Serect Key'; //API-KEY,注册ping++时自动生成		$app_id = '请填写自己的APP的ID'; //APP_ID,注册时自动生成    	Vendor('pingpp.init'); //引入类库初始化文件    	    	//以支付宝,支付10块钱。注意amount为1000,单位是”分钱“    	$input_data = array('channel'=>'alipay', 'amount'=>1000); //$input_data应该是你从APP客户端收到的json数据(根据业务可以在服务器端定义),需json_decode    	if (empty($input_data['channel']) || empty($input_data['amount'])) {					    echo 'channel or amount is empty'; //检验支付参数		    exit();		}    		$channel = strtolower($input_data['channel']); //支付渠道:支付宝		$amount = $input_data['amount']; //支付金额10块钱		$orderNo = substr(md5(time()), 0, 12); //订单号,必须唯一。根据支付渠道不同,长度要求也不一样,请查看文档https://www.pingxx.com/api#api-c-new				\Pingpp\Pingpp::setPrivateKeyPath(VENDOR_PATH . '/pingpp/rsa_private_key.pem'); //引入你的签名私钥		//$extra用于设置支付渠道所需的额外参数,额外参数多数是可选,请根据需求来决定。详情看参考文档		// $extra = array(  //           'success_url' => 'http://example.com/success',  //           'cancel_url' => 'http://example.com/cancel'  //       );		\Pingpp\Pingpp::setApiKey($api_key); //设置API-KEY		try {		    $ch = \Pingpp\Charge::create( //create方法表示发送支付请求到ping++平台,$ch表示请求成功时返回的charge对象(json格式),服务器端如果发起请求成功,此时只需要把charge对象传递给APP客户端,交给客户端处理		        array(		            'subject'   => 'Your Subject', //关于这些参数的意义,请参考文档https://www.pingxx.com/api#api-c-new		            'body'      => 'Your Body',		            'amount'    => $amount,		            'order_no'  => $orderNo,		            'currency'  => 'cny',		            // 'extra'     => $extra,		            'channel'   => $channel,		            'client_ip' => $_SERVER['REMOTE_ADDR'],		            'app'       => array('id' => $app_id)		        )		    );		    echo $ch;		} catch (\Pingpp\Error\Base $e) { //如果发起支付请求失败,则抛出异常		    // 捕获报错信息		    if ($e->getHttpStatus() != NULL) {		        header('Status: ' . $e->getHttpStatus());		        echo $e->getHttpBody();		    } else {		        echo $e->getMessage();		    }		}		//代码到此处,如果你向ping++平台请求支付成功,并且把charge对象传递给了客户端,那么接下来客户端如果成功完成了支付,那么		//平台将会请求你填写的Webhooks回调url,在该url里,接收 Webhooks 通知,根据数据的结果做出逻辑判断:如果支付成功....,如果支付失败...

如果此时,能在浏览器返回一串json数据,那么你的初步调试就通过了。

第四步:填写你的webhook的回调地址。如果客户端发起的支付,已经完成交易,那么交易成功的json消息,会

返回到该回调地址,你根据json消息,对自己的数据库和业务作出相应的回应或者抛出支付错误信息。webhook的填

写,请到个人中心去填写,很容易的,不再赘述。





 
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment