>  기사  >  백엔드 개발  >  微信支付之公众号支付

微信支付之公众号支付

WBOY
WBOY원래의
2016-08-08 09:27:311209검색

最近项目中用到了微信支付,于是找到了这篇资料,为了防止遗忘转载一下,先谢过作者。原文地址:http://www.360us.net/article/22.html

最近把支付宝、银联和微信支付全都做了一遍,目前做的都还只涉及到消费的功能。
做下来感觉就是各个平台的支付流程都是大同小异,签名方式也是一样的。
这里主要总结一下微信支付公众号支付的一些东西。
微信公众号支付的主要流程如下:
1、生成我们自己系统的订单。
2、调用微信支付的统一下单接口把订单信息推给微信。
3、在第二部会返回一个预支付会话标识,然后凭这个标识用JS去调用支付操作。

关于支付页面的url问题,微信要求是最后必须要有“/”,我看到很多文章说不适合MVC结构的程序,我的情况是否定的,MVC结构一样可以。<br>比如url是这个:<codecourier new font-size:12.6000003814697px padding:2px color:rgb background-color:rgb>http://www.example.com/payment/wechatpay/ ,url里面<codecourier new font-size:12.6000003814697px padding:2px color:rgb background-color:rgb>payment是<codecourier new font-size:12.6000003814697px padding:2px color:rgb background-color:rgb>controller,<codecourier new font-size:12.6000003814697px padding:2px color:rgb background-color:rgb>wechatpay是<codecourier new font-size:12.6000003814697px padding:2px color:rgb background-color:rgb>action,这有问题吗?<br>一样可以访问,可以支付,是不是一个真正的目录,在微信看来就是,实际上其实不是。<phelvetica neue grande sans gb yahei micro hei font-size:14px line-height:20px><br><phelvetica neue grande sans gb yahei micro hei font-size:14px line-height:20px>好,下面进入正题。<br><phelvetica neue grande sans gb yahei micro hei font-size:14px line-height:20px><br>微信支付配置如下:<table border="0" cellpadding="0" cellspacing="0" courier new width:803px word-break:break-all color:rgb border:1px solid rgb bottom:auto float:none left:auto line-height:1.1em margin:0.3em outline:0px overflow:auto padding:0px position:relative right:auto top:auto vertical-align:baseline min-height:inherit font-size:13px background:none><tbody><tr> <td> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> </td> <tdcourier new width:766px padding:0px border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline min-height:inherit font-size:13px background:none><p></p> <p><codecourier new padding:0px color:rgb border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>$config <codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>= [</codecourier></codecourier></p> <p><codecourier new color:rgb padding:0px border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>    <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'mch_id' <codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>=> <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'1234455666'<codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>, <codecourier new padding:0px color:rgb border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>//商户号</codecourier></codecourier></codecourier></codecourier></codecourier></codecourier></p> <p><codecourier new color:rgb padding:0px border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>    <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'signType' <codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>=> <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'MD5'<codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>, <codecourier new padding:0px color:rgb border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>//签名方式,目前只有MD5</codecourier></codecourier></codecourier></codecourier></codecourier></codecourier></p> <p><codecourier new color:rgb padding:0px border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>    <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'key' <codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>=> <codecourier new padding:0px color:blue border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>'sdsfdhgjh34343krn3453tnelt'<codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>, <codecourier new padding:0px color:rgb border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>//api密钥</codecourier></codecourier></codecourier></codecourier></codecourier></codecourier></p> <p><codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none>];</codecourier></p></tdcourier> </tr></tbody></table><phelvetica neue grande sans gb yahei micro hei font-size:14px line-height:20px>Weixinpay代码清单如下:<table border="0" cellpadding="0" cellspacing="0" courier new width:803px word-break:break-all color:rgb border:1px solid rgb bottom:auto float:none left:auto line-height:1.1em margin:0.3em outline:0px overflow:auto padding:0px position:relative right:auto top:auto vertical-align:baseline min-height:inherit font-size:13px background:none><tbody><tr> <td> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> <p>8</p> <p>9</p> <p>10</p> <p>11</p> <p>12</p> <p>13</p> <p>14</p> <p>15</p> <p>16</p> <p>17</p> <p>18</p> <p>19</p> <p>20</p> <p>21</p> <p>22</p> <p>23</p> <p>24</p> <p>25</p> <p>26</p> <p>27</p> <p>28</p> <p>29</p> <p>30</p> <p>31</p> <p>32</p> <p>33</p> <p>34</p> <p>35</p> <p>36</p> <p>37</p> <p>38</p> <p>39</p> <p>40</p> <p>41</p> <p>42</p> <p>43</p> <p>44</p> <p>45</p> <p>46</p> <p>47</p> <p>48</p> <p>49</p> <p>50</p> <p>51</p> <p>52</p> <p>53</p> <p>54</p> <p>55</p> <p>56</p> <p>57</p> <p>58</p> <p>59</p> <p>60</p> <p>61</p> <p>62</p> <p>63</p> <p>64</p> <p>65</p> <p>66</p> <p>67</p> <p>68</p> <p>69</p> <p>70</p> <p>71</p> <p>72</p> <p>73</p> <p>74</p> <p>75</p> <p>76</p> <p>77</p> <p>78</p> <p>79</p> <p>80</p> <p>81</p> <p>82</p> <p>83</p> <p>84</p> <p>85</p> <p>86</p> <p>87</p> <p>88</p> <p>89</p> <p>90</p> <p>91</p> <p>92</p> <p>93</p> <p>94</p> <p>95</p> <p>96</p> <p>97</p> <p>98</p> <p>99</p> <p>100</p> <p>101</p> <p>102</p> <p>103</p> <p>104</p> <p>105</p> <p>106</p> <p>107</p> <p>108</p> <p>109</p> <p>110</p> <p>111</p> <p>112</p> <p>113</p> <p>114</p> <p>115</p> <p>116</p> <p>117</p> <p>118</p> <p>119</p> <p>120</p> <p>121</p> <p>122</p> <p>123</p> <p>124</p> <p>125</p> <p>126</p> <p>127</p> <p>128</p> <p>129</p> <p>130</p> <p>131</p> <p>132</p> <p>133</p> <p>134</p> <p>135</p> <p>136</p> <p>137</p> <p>138</p> <p>139</p> <p>140</p> <p>141</p> <p>142</p> <p>143</p> <p>144</p> <p>145</p> <p>146</p> <p>147</p> <p>148</p> <p>149</p> <p>150</p> <p>151</p> <p>152</p> <p>153</p> <p>154</p> <p>155</p> <p>156</p> <p>157</p> <p>158</p> <p>159</p> <p>160</p> <p>161</p> <p>162</p> <p>163</p> <p>164</p> <p>165</p> <p>166</p> <p>167</p> <p>168</p> <p>169</p> <p>170</p> <p>171</p> <p>172</p> <p>173</p> <p>174</p> <p>175</p> <p>176</p> <p>177</p> <p>178</p> <p>179</p> <p>180</p> <p>181</p> <p>182</p> <p>183</p> <p>184</p> <p>185</p> <p>186</p> <p>187</p> <p>188</p> <p>189</p> <p>190</p> <p>191</p> <p>192</p> <p>193</p> <p>194</p> <p>195</p> <p>196</p> <p>197</p> <p>198</p> <p>199</p> <p>200</p> <p>201</p> <p>202</p> <p>203</p> <p>204</p> <p>205</p> <p>206</p> <p>207</p> <p>208</p> <p>209</p> <p>210</p> <p>211</p> <p>212</p> <p>213</p> <p>214</p> <p>215</p> <p>216</p> <p>217</p> <p>218</p> <p>219</p> <p>220</p> <p>221</p> <p>222</p> <p>223</p> <p>224</p> <p>225</p> <p>226</p> <p>227</p> <p>228</p> <p>229</p> <p>230</p> <p>231</p> <p>232</p> <p>233</p> <p>234</p> <p>235</p> <p>236</p> <p>237</p> <p>238</p> <p>239</p> <p>240</p> <p>241</p> <p>242</p> <p>243</p> </td> <tdcourier new width:752px padding:0px border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline min-height:inherit font-size:13px background:none><p></p> <p><codecourier new padding:0px color:black border:0px bottom:auto float:none left:auto line-height:1.1em margin:0px outline:0px overflow:visible position:static right:auto top:auto vertical-align:baseline width:auto min-height:inherit background:none><?php

namespace weixin\components; //这个是命名空间,可以根据需要修改

class WeixinPay

{

    //支付配置

    public $config;

     

    //支付参数

    public $params;

     

    //统一下单url

    const POST_ORDER_URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

     

    //订单查询url

    const ORDER_QUERY_URL = 'https://api.mch.weixin.qq.com/pay/orderquery';

     

    /**

     * 创建微信js发起支付参数

     * @return array

     */

    public function createJsPayData()

    {

        $this->params['nonce_str'] = $this->getRandomStr();

        $this->params['sign'] = $this->sign();

         

        $xmlStr $this->arrayToXml();

         

        $res $this->postUrl(self::POST_ORDER_URL, $xmlStr);

        $res $this->xmlToArray($res);

        if$res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS' 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.