国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

Jun 07, 2016 am 11:38 AM

大家好,微信支付系列教程四種方式已經(jīng)結(jié)束,如果你以為結(jié)束了就錯了,有同學(xué)跟我提到微信還有紅包功能,我開始也沒注意這一塊,于是看了下微信商戶平臺上有講到這一塊,微信支付平臺上也早就有了,于是趁熱打鐵,研究了下,繼續(xù)發(fā)出關(guān)于微信紅包的教程文章。接下來請看微信支付發(fā)紅包之現(xiàn)金紅包教程!
現(xiàn)在微信商戶可以向指定的openid發(fā)送紅包,目前紅包分兩種:現(xiàn)金紅包和裂變紅包。本教程是關(guān)于現(xiàn)金紅包的。

在貼代碼之前,先講幾個注意點:1.去商戶平臺里,給你的商戶充錢,沒錢是發(fā)不了紅包噠! 2.微信紅包需要證書支持,所以請大家到商戶平臺下去下載好證書后放到安全文件夾下,并且需要在配置文件中指定好證書路徑!

step1:老樣子,還是介紹配置文件WxPayConf_pub.php,看過之前微信支付教程的同學(xué)應(yīng)該很清楚這一塊了,這里我將代碼截圖出來,配置好后進行下一步!
ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

step2:下載你的證書,放到一個目錄下,對應(yīng)配置文件中,記得這里是絕對路徑!
ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

step3:之前的微信支付的demo微信官方已經(jīng)幫我們寫好了WxPayHelper.php這個類庫,我們可以很方便的調(diào)用就夠了,而微信紅包目前還沒有官方demo,所以這里我們得自己在WxPayHelper.php文件下寫自己的紅包支付方法:/**<br> ?*?現(xiàn)金紅包接口<br> ?*?@author?gaoyl101<br> ?*/<br> class?Redpack_pub?extends?Wxpay_client_pub<br> {<br> ????var?$code;//code碼,用以獲取openid<br> ????var?$openid;//用戶的openid<br> ????<br> ????function?__construct()<br> ????{<br> ????????//設(shè)置接口鏈接<br> ????????$this->url?=?"https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";<br> ????????//設(shè)置curl超時時間<br> ????????$this->curl_timeout?=?WxPayConf_pub::CURL_TIMEOUT;<br> ????}<br> <br> ????/**<br> ?????*?生成接口參數(shù)xml<br> ?????*/<br> ????function?createXml()<br> ????{<br> ????????try<br> ????????{<br> ????????????//檢測必填參數(shù)<br> ????????????if($this->parameters["mch_billno"]?==?null)<br> ????????????{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)mch_billno!"."<br>");<br> ????????????}elseif($this->parameters["nick_name"]?==?null){<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)nick_name!"."<br>");<br> ????????????}elseif?($this->parameters["send_name"]?==?null?)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)send_name!"."<br>");<br> ????????????}elseif?($this->parameters["total_amount"]?==?null)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)total_amount!"."<br>");<br> ????????????}elseif($this->parameters["min_value"]?==?null){<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)min_value!"."<br>");<br> ????????????}elseif?($this->parameters["max_value"]?==?null?)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)max_value!"."<br>");<br> ????????????}elseif?($this->parameters["total_num"]?==?null)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)total_num!"."<br>");<br> ????????????}elseif?($this->parameters["wishing"]?==?null)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)wishing!"."<br>");<br> ????????????}elseif?($this->parameters["act_name"]?==?null)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)act_name!"."<br>");<br> ????????????}elseif?($this->parameters["remark"]?==?null)?{<br> ????????????????throw?new?SDKRuntimeException("缺少發(fā)紅包接口必填參數(shù)remark!"."<br>");<br> ????????????}<br> ????????????$this->parameters["wxappid"]?=?WxPayConf_pub::APPID;//公眾賬號ID<br> ????????????$this->parameters["mch_id"]?=?WxPayConf_pub::MCHID;//商戶號<br> ????????????$this->parameters["client_ip"]?=?$_SERVER['REMOTE_ADDR'];//終端ip<br> ????????????$this->parameters["nonce_str"]?=?$this->createNoncestr();//隨機字符串<br> ????????????$this->parameters["re_openid"]?=?$this->openid;//用戶openid<br> ????????????$this->parameters["sign"]?=?$this->getSign($this->parameters);//簽名<br> ????????????return??$this->arrayToXml($this->parameters);<br> ????????}catch?(SDKRuntimeException?$e)<br> ????????{<br> ????????????die($e->errorMessage());<br> ????????}<br> ????}<br> ????<br> ????<br> ????function?sendRedpack()<br> ????{<br> ????????$this->postXmlSSL();<br> ????????$this->result?=?$this->xmlToArray($this->response);<br> ????????return?$this->result;<br> ????}<br> ????<br> ????<br> ????<br> ????/**<br> ?????*?????作用:生成可以獲得code的url<br> ?????*/<br> ????function?createOauthUrlForCode($redirectUrl)<br> ????{<br> ????????$urlObj["appid"]?=?WxPayConf_pub::APPID;<br> ????????$urlObj["redirect_uri"]?=?"$redirectUrl";<br> ????????$urlObj["response_type"]?=?"code";<br> ????????$urlObj["scope"]?=?"snsapi_base";<br> ????????$urlObj["state"]?=?"STATE"."#wechat_redirect";<br> ????????$bizString?=?$this->formatBizQueryParaMap($urlObj,?false);<br> ????????return?"https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;<br> ????}<br> ????<br> ????<br> ????<br> ????/**<br> ?????*?????作用:生成可以獲得openid的url<br> ?????*/<br> ????function?createOauthUrlForOpenid()<br> ????{<br> ????????$urlObj["appid"]?=?WxPayConf_pub::APPID;<br> ????????$urlObj["secret"]?=?WxPayConf_pub::APPSECRET;<br> ????????$urlObj["code"]?=?$this->code;<br> ????????$urlObj["grant_type"]?=?"authorization_code";<br> ????????$bizString?=?$this->formatBizQueryParaMap($urlObj,?false);<br> ????????return?"https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;<br> ????}<br> ????<br> ????/**<br> ?????*?????作用:通過curl向微信提交code,以獲取openid<br> ?????*/<br> ????function?getOpenid()<br> ????{<br> ????????$url?=?$this->createOauthUrlForOpenid();<br> ????????//初始化curl<br> ???????????$ch?=?curl_init();<br> ????????//設(shè)置超時<br> ????????curl_setopt($ch,?CURLOP_TIMEOUT,?$this->curl_timeout);<br> ????????curl_setopt($ch,?CURLOPT_URL,?$url);<br> ????????curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);<br> ????????curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);<br> ????????curl_setopt($ch,?CURLOPT_HEADER,?FALSE);<br> ????????curl_setopt($ch,?CURLOPT_RETURNTRANSFER,?TRUE);<br> ????????//運行curl,結(jié)果以jason形式返回<br> ????????$res?=?curl_exec($ch);<br> ????????curl_close($ch);<br> ????????//取出openid<br> ????????$data?=?json_decode($res,true);<br> ????????$this->openid?=?$data['openid'];<br> ????????return?$this->openid;<br> ????}<br> ????<br> ????/**<br> ?????*?????作用:設(shè)置code<br> ?????*/<br> ????function?setCode($code_)<br> ????{<br> ????????$this->code?=?$code_;<br> ????}<br> }其實這里的代碼我做的并不是很好,我并沒有封裝,因為做裂變紅包也會用到相似的代碼,這里做demo我就先不改了,有興趣的朋友可以在此基礎(chǔ)上繼續(xù)晚上!上面的代碼就是我們要用到的工具類,把他放在WxPayHelper.php最下面就可以了!

step4:創(chuàng)建控制器WxCashRedPackController
ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

控制器中的代碼:
1.引入WxPayHelper.php類庫/**<br> ?????*?初始化<br> ?????*/<br> ????public?function?_initialize()<br> ????{<br> ????????//引入WxPayPubHelper<br> ????????vendor('WxPayPubHelper.WxPayPubHelper');<br> ????}2.創(chuàng)建發(fā)送紅包方法:sendRedpack,這個方法就是發(fā)送紅包的具體功能代碼!/**<br> ?????*?發(fā)送紅包<br> ?????*/<br> ????public?function?sendRedpack()<br> ????{<br> ????????//調(diào)用請求接口基類<br> ????????$Redpack?=?new?\Redpack_pub();<br> ????????<br> ????????//=========步驟1:網(wǎng)頁授權(quán)獲取用戶openid============<br> ????????//通過code獲得openid<br> ????????if?(!isset($_GET['code']))<br> ????????{<br> ????????????//觸發(fā)微信返回code碼<br> ????????????$reduct_uri?=?WEB_HOST."/index.php/Home/WxCashRedPack/sendRedpack";<br> ????????????$url?=?$Redpack->createOauthUrlForCode($reduct_uri);<br> ????????????Header("Location:?$url");<br> ????????}else<br> ????????{<br> ????????????//獲取code碼,以獲取openid<br> ????????????$code?=?$_GET['code'];<br> ????????????$Redpack->setCode($code);<br> ????????????$openid?=?$Redpack->getOpenId();<br> ????????}<br> ?????????<br> ????????<br> ????????<br> ????????//商戶訂單號<br> ????????$Redpack->setParameter('mch_billno',?C('WxPayConf_pub.APPID')."static");<br> ????????//提供方名稱<br> ????????$Redpack->setParameter('nick_name',?"gaoyl101");<br> ????????//商戶名稱<br> ????????$Redpack->setParameter('send_name',?"gaoyl101");<br> ????????//用戶openid<br> //?????????$Redpack->setParameter('re_openid',?$parameterValue);<br> ????????//付款金額<br> ????????$Redpack->setParameter('total_amount',?100);<br> ????????//最小紅包金額<br> ????????$Redpack->setParameter('min_value',?100);<br> ????????//最大紅包金額<br> ????????$Redpack->setParameter('max_value',?100);<br> ????????//紅包發(fā)放總?cè)藬?shù)<br> ????????$Redpack->setParameter('total_num',?1);<br> ????????//紅包祝福語<br> ????????$Redpack->setParameter('wishing',?"現(xiàn)金紅包教程祝大家寫代碼快樂");<br> ????????//活動名稱<br> ????????$Redpack->setParameter('act_name',?"現(xiàn)金紅包教程");<br> ????????//備注<br> ????????$Redpack->setParameter('remark',?"現(xiàn)金紅包教程祝大家寫代碼快樂");<br> ????????//以下是非必填項目<br> ????????//子商戶號??<br> //?????????$Redpack->setParameter('sub_mch_id',?$parameterValue);<br> //????????//商戶logo的url<br> //?????????$Redpack->setParameter('logo_imgurl',?$parameterValue);<br> //?????????//分享文案<br> //?????????$Redpack->setParameter('share_content',?$parameterValue);<br> //?????????//分享鏈接<br> //?????????$Redpack->setParameter('share_url',?$parameterValue);<br> //?????????//分享的圖片<br> //?????????$Redpack->setParameter('share_imgurl',?$parameterValue);<br> ????????<br> ????????<br> ????????<br> ????????$result?=?$Redpack->sendRedpack();<br> ????????<br> ????????dump($result);<br> ????}訪問這個方法,微信就會發(fā)紅包啦
在這里我dump了微信發(fā)送紅包之后返回的結(jié)果,下面的業(yè)務(wù)邏輯就可以根據(jù)自己的需求接下去寫了,返回值的說明可以看微信紅包的接口說明,在微信支付平臺上有。
到這里微信紅包現(xiàn)金紅包代碼已經(jīng)全部結(jié)束,功能經(jīng)過測試已經(jīng)完成!

下面是成功后的紅包截圖:
ThinkPHP整合微信支付之發(fā)現(xiàn)金紅包

之前的幾篇微信支付的教程很多同學(xué)看了之后都會遇到問題,并找到我得到了解決,個人認為我發(fā)的文章得到了它應(yīng)有的價值,希望這篇文章也能幫到正在為發(fā)送紅包而發(fā)愁的同學(xué)!

有問題請留言,下面還會介紹微信發(fā)紅包之裂變紅包!

微信支付之jsapi:
http://www.thinkphp.cn/code/1321.html
微信支付教程掃碼模式一:
http://www.thinkphp.cn/code/1322.html
微信支付教程掃碼模式二:
http://www.thinkphp.cn/code/1323.html
微信支付教程刷卡支付:
http://www.thinkphp.cn/code/1324.html
微信裂變紅包教程:
http://www.thinkphp.cn/code/1330.html
歡迎大家吐槽,轉(zhuǎn)載請說明出處,請支持原創(chuàng),謝謝!
我們的微信開發(fā)群:422579975(已滿) 105195188(未滿),代碼已放在群文件中
歡迎大家加入討論問題

AD:真正免費,域名+虛機+企業(yè)郵箱=0元

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ??during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

See all articles