Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Development of WeChat Pay and Alipay in PHP

Shulou Source: shulou.com Published: 2022-06-03 20:19:02 10月15日 Update

This article will explain in detail how to achieve background WeChat Pay and Alipay pay development in PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

WeChat Pay

Project premise: I use tp framework, PHP language

Download the WeChat Pay interface file provided by Wechat platform and put it in the tp third-party class library vendor, named WxpayAPI

WxpayAPI/lib/WxPay.Api.php interface access class; WxpayAPI/lib/WxPay.Config.php configuration account information; WxpayAPI/lib/WxPay.Data.php data object basic class; WxpayAPI/lib/WxPay.Exception.php WeChat Pay API exception class; WxpayAPI/lib/WxPay.Notify.php callback basic class WxpayAPI/example/WxPay.JsApiPay.php JSAPI payment implementation class

1. Partial changes have been made to the source code

(1) comment out in WxPay.Api.php

/ / require_once "WxPay.Exception.php"

/ / require_once "WxPay.Config.php"

/ / require_once "WxPay.Data.php"

(2) in WxPay.Config.php

APPID, MCHID, KEY and APPSECRET need to be configured according to merchant information.

(3) comment out in WxPay.Data.php

/ / require_once "WxPay.Config.php"

/ / require_once "WxPay.Exception.php"

(4) comment out in WxPay.JsApiPay.php

/ / require_once ".. / lib/WxPay.Api.php"

After configuring these, here is our key part.

two。 There are order function sure () and callback information function Callback_url () in order controller GoodsController.class.php.

/ * submit order function * / public function sure () {$o_model = D ("Wine/Orders"); if (IS_AJAX) {$data = I ("post."); if ($if model-> create ($data)) {if (! sp_check_verify_code ()) {$this- > error ("CAPTCHA error!") ;} # generate random order number $order_code ='O'. Date ('YmdHis'). Get_order_code (4); while ($ostensible model-> findone (array ("order_code" = > $order_code)) {$order_code ='O'. Date ('YmdHis'). $ostatic model-> get_order_code (4);} $data ['order_code'] = $order_code; $addr [0] = $_ POST [' prov']; $addr [1] = $_ POST ['city']; $addr [2] = $_ POST [' dist']; $addr [3] = $_ POST ['area']; $data [' area'] = serialize ($addr); $data ['create_time'] = time () $data ['update_time'] = time (); if ($data [' pay_id'] = = 1) {$data ['order_status'] = 11; / paid $data [' status'] = '1payment;} else {$data [' order_status'] = 10; / pending payment $data ['status'] =' 1payment;} / function call return information $this- > Callback_url ($data) } else {$this- > error ($ostensible model-> getError ());}} else {$this- > error ($oiled model-> getError ());}} / * callback information function * @ param type $data * / public function Callback_url ($data) {$o_model = D ("Wine/Orders"); $add_id = $ostatic model-> add ($data) If (! $add_id) {$this- > error ("order submission failed, please try again later!") ;} if ('4' = = $data ['pay_id']) {if (' 4' = = $data [payout _ id']) {/ / WeChat Pay $msg ='is jumping to the WeChat Pay page for you, please wait …' ; $url = "/ index.php/wine/wxpay/index/?o_id=$add_id";} $this- > success ("order submitted successfully!" . $msg, $url);}

3. [key points!] WxpayController. Class.php WeChat Pay controller to realize the call to Wechat interface

Tags: Payment order page information function success interface configuration function parameter data file server comment class name path processing service price address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft MySQL Shulou Technology Huawei