在近期,公司需要开发一个关于在线支付的模块,所以需要用到第三方支付平台
转载请注明出处:http://blog.csdn.net/ht_android/article/details/45307165
经过一周多的时间对这两种支付平台的研究,完成功能后将经验分享给大家,希望能帮助到有需求的朋友。
首先是支付宝的开发资料方面:
接口申请url
https://b.alipay.com/order/productDetail.htm?productId=2014110308141993
API开发文档
http://download.alipay.com/public/api/base/WS_MOBILE_PAY_SDK_BASE.zip
申请流程
注册支付宝账号——进行实名认证——提交审核资料——审核通过
备注:申请通过后会获得:合作者身份ID(PID),该ID在项目配置中需要用到
开发流程:
第一步:
下载API开发文档后,即可获取官方Demo,该Demo中需要将审核通过后获取的PID替换,并且输入支付宝收款账户即可。这里非常简单,就不过多叙述。
第二步:
官方Api开发文档中,存在一个openssl的文件夹,该文件夹主要是用于生成支付宝所需要用到的公钥以及私钥。打开该文件夹可以看到详细的生成方式,根据提示生成公钥及私钥,请注意,密钥需要经过pkcs8二次加密。
第三步:
将生成的公钥和私钥配置到Demo中。
第四步(可省略):
为了方便后期维护,建议将支付宝相关的方法及配置项抽取出来做为单独的一个类,后期需要使用直接调用即可。代码如下:
<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.alipay.pay; <span class="hljs-keyword">import</span> java.io.UnsupportedEncodingException; <span class="hljs-keyword">import</span> java.net.URLEncoder; <span class="hljs-keyword">import</span> java.text.DecimalFormat; <span class="hljs-keyword">import</span> java.text.SimpleDateFormat; <span class="hljs-keyword">import</span> java.util.Date; <span class="hljs-keyword">import</span> java.util.Locale; <span class="hljs-keyword">import</span> java.util.Random; <span class="hljs-keyword">import</span> android.app.Activity; <span class="hljs-keyword">import</span> android.os.Handler; <span class="hljs-keyword">import</span> android.os.Message; <span class="hljs-keyword">import</span> android.support.v4.app.FragmentActivity; <span class="hljs-keyword">import</span> android.view.View; <span class="hljs-keyword">import</span> android.widget.Toast; <span class="hljs-keyword">import</span> com.alipay.sdk.app.PayTask; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Alipay</span> {</span> <span class="hljs-comment">// 商户PID</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String PARTNER = <span class="hljs-string">"******"</span>; <span class="hljs-comment">// 商户收款账号</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String SELLER = <span class="hljs-string">"***@alipay.com"</span>; <span class="hljs-comment">// 商户私钥,pkcs8格式</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String RSA_PRIVATE = <span class="hljs-string">"*****"</span>; <span class="hljs-comment">// 支付宝公钥</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String RSA_PUBLIC = <span class="hljs-string">"******"</span>; <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> SDK_PAY_FLAG = <span class="hljs-number">1</span>; <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> SDK_CHECK_FLAG = <span class="hljs-number">2</span>; <span class="hljs-keyword">private</span> Handler mHandler; <span class="hljs-keyword">private</span> Activity activity; <span class="hljs-keyword">private</span> String orderNo; <span class="hljs-keyword">public</span> <span class="hljs-title">Alipay</span>(Handler handler, Activity activity) { mHandler = handler; <span class="hljs-keyword">this</span>.activity = activity; } <span class="hljs-javadoc">/** * call alipay sdk pay. 调用SDK支付 * */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span>(PayInfo payinfo) { <span class="hljs-comment">// 订单</span> DecimalFormat df = <span class="hljs-keyword">new</span> DecimalFormat(<span class="hljs-string">"0.00"</span>); String orderInfo = getOrderInfo(payinfo.getName(), payinfo.getDesc() + <span class="hljs-string">" "</span>, df.format(payinfo.getPrice() * payinfo.getRate())); <span class="hljs-comment">// 对订单做RSA 签名</span> String sign = sign(orderInfo); <span class="hljs-keyword">try</span> { <span class="hljs-comment">// 仅需对sign 做URL编码</span> sign = URLEncoder.encode(sign, <span class="hljs-string">"UTF-8"</span>); } <span class="hljs-keyword">catch</span> (UnsupportedEncodingException e) { e.printStackTrace(); } <span class="hljs-comment">// 完整的符合支付宝参数规范的订单信息</span> <span class="hljs-keyword">final</span> String payInfo = orderInfo + <span class="hljs-string">"&sign=\""</span> + sign + <span class="hljs-string">"\"&"</span> + getSignType(); Runnable payRunnable = <span class="hljs-keyword">new</span> Runnable() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span>() { <span class="hljs-comment">// 构造PayTask 对象</span> PayTask alipay = <span class="hljs-keyword">new</span> PayTask(activity); <span class="hljs-comment">// 调用支付接口,获取支付结果</span> String result = alipay.pay(payInfo); Message msg = <span class="hljs-keyword">new</span> Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; <span class="hljs-comment">// 必须异步调用</span> Thread payThread = <span class="hljs-keyword">new</span> Thread(payRunnable); payThread.start(); } <span class="hljs-javadoc">/** * check whether the device has authentication alipay account. * 查询终端设备是否存在支付宝认证账户 * */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">check</span>(View v) { Runnable checkRunnable = <span class="hljs-keyword">new</span> Runnable() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span>() { <span class="hljs-comment">// 构造PayTask 对象</span> PayTask payTask = <span class="hljs-keyword">new</span> PayTask(activity); <span class="hljs-comment">// 调用查询接口,获取查询结果</span> <span class="hljs-keyword">boolean</span> isExist = payTask.checkAccountIfExist(); Message msg = <span class="hljs-keyword">new</span> Message(); msg.what = SDK_CHECK_FLAG; msg.obj = isExist; mHandler.sendMessage(msg); } }; Thread checkThread = <span class="hljs-keyword">new</span> Thread(checkRunnable); checkThread.start(); } <span class="hljs-javadoc">/** * get the sdk version. 获取SDK版本号 * */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getSDKVersion</span>() { PayTask payTask = <span class="hljs-keyword">new</span> PayTask(activity); String version = payTask.getVersion(); Toast.makeText(activity, version, Toast.LENGTH_SHORT).show(); } <span class="hljs-javadoc">/** * create the order info. 创建订单信息 * */</span> <span class="hljs-keyword">public</span> String <span class="hljs-title">getOrderInfo</span>(String subject, String body, String price) { <span class="hljs-comment">// 签约合作者身份ID</span> String orderInfo = <span class="hljs-string">"partner="</span> + <span class="hljs-string">"\""</span> + PARTNER + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 签约卖家支付宝账号</span> orderInfo += <span class="hljs-string">"&seller_id="</span> + <span class="hljs-string">"\""</span> + SELLER + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 商户网站唯一订单号</span> orderInfo += <span class="hljs-string">"&out_trade_no="</span> + <span class="hljs-string">"\""</span> + getOutTradeNo() + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 商品名称</span> orderInfo += <span class="hljs-string">"&subject="</span> + <span class="hljs-string">"\""</span> + subject + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 商品详情</span> orderInfo += <span class="hljs-string">"&body="</span> + <span class="hljs-string">"\""</span> + body + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 商品金额</span> orderInfo += <span class="hljs-string">"&total_fee="</span> + <span class="hljs-string">"\""</span> + price + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 服务器异步通知页面路径</span> orderInfo += <span class="hljs-string">"¬ify_url="</span> + <span class="hljs-string">"\""</span> + <span class="hljs-string">"http://notify.msp.hk/notify.htm"</span> + <span class="hljs-string">"\""</span>; <span class="hljs-comment">// 服务接口名称, 固定值</span> orderInfo += <span class="hljs-string">"&service=\"mobile.securitypay.pay\""</span>; <span class="hljs-comment">// 支付类型, 固定值</span> orderInfo += <span class="hljs-string">"&payment_type=\"1\""</span>; <span class="hljs-comment">// 参数编码, 固定值</span> orderInfo += <span class="hljs-string">"&_input_charset=\"utf-8\""</span>; <span class="hljs-comment">// 设置未付款交易的超时时间</span> <span class="hljs-comment">// 默认30分钟,一旦超时,该笔交易就会自动被关闭。</span> <span class="hljs-comment">// 取值范围:1m~15d。</span> <span class="hljs-comment">// m-分钟,h-小时,d-天,1c-当天(无论交易何时创建,都在0点关闭)。</span> <span class="hljs-comment">// 该参数数值不接受小数点,如1.5h,可转换为90m。</span> orderInfo += <span class="hljs-string">"&it_b_pay=\"30m\""</span>; <span class="hljs-comment">// extern_token为经过快登授权获取到的alipay_open_id,带上此参数用户将使用授权的账户进行支付</span> <span class="hljs-comment">// orderInfo += "&extern_token=" + "\"" + extern_token + "\"";</span> <span class="hljs-comment">// 支付宝处理完请求后,当前页面跳转到商户指定页面的路径,可空</span> orderInfo += <span class="hljs-string">"&return_url=\"m.alipay.com\""</span>; <span class="hljs-comment">// 调用银行卡支付,需配置此参数,参与签名, 固定值 (需要签约《无线银行卡快捷支付》才能使用)</span> <span class="hljs-comment">// orderInfo += "&paymethod=\"expressGateway\"";</span> <span class="hljs-keyword">return</span> orderInfo; } <span class="hljs-javadoc">/** * get the out_trade_no for an order. 生成商户订单号,该值在商户端应保持唯一(可自定义格式规范) * */</span> <span class="hljs-keyword">public</span> String <span class="hljs-title">getOutTradeNo</span>() { SimpleDateFormat format = <span class="hljs-keyword">new</span> SimpleDateFormat(<span class="hljs-string">"MMddHHmmss"</span>, Locale.getDefault()); Date date = <span class="hljs-keyword">new</span> Date(); String key = format.format(date); Random r = <span class="hljs-keyword">new</span> Random(); key = key + r.nextInt(); key = key.substring(<span class="hljs-number">0</span>, <span class="hljs-number">15</span>); String md5 = Constants.MD5(key); <span class="hljs-keyword">this</span>.orderNo = md5; <span class="hljs-keyword">return</span> md5; } <span class="hljs-javadoc">/** * 获取已经生产的订单编号 * *<span class="hljs-javadoctag"> @return</span> */</span> <span class="hljs-keyword">public</span> String <span class="hljs-title">getOrderNo</span>() { <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.orderNo; } <span class="hljs-javadoc">/** * sign the order info. 对订单信息进行签名 * *<span class="hljs-javadoctag"> @param</span> content * 待签名订单信息 */</span> <span class="hljs-keyword">public</span> String <span class="hljs-title">sign</span>(String content) { <span class="hljs-keyword">return</span> SignUtils.sign(content, RSA_PRIVATE); } <span class="hljs-javadoc">/** * get the sign type we use. 获取签名方式 * */</span> <span class="hljs-keyword">public</span> String <span class="hljs-title">getSignType</span>() { <span class="hljs-keyword">return</span> <span class="hljs-string">"sign_type=\"RSA\""</span>; } } </code><ul class="pre-numbering" style="display: block;"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li><li>82</li><li>83</li><li>84</li><li>85</li><li>86</li><li>87</li><li>88</li><li>89</li><li>90</li><li>91</li><li>92</li><li>93</li><li>94</li><li>95</li><li>96</li><li>97</li><li>98</li><li>99</li><li>100</li><li>101</li><li>102</li><li>103</li><li>104</li><li>105</li><li>106</li><li>107</li><li>108</li><li>109</li><li>110</li><li>111</li><li>112</li><li>113</li><li>114</li><li>115</li><li>116</li><li>117</li><li>118</li><li>119</li><li>120</li><li>121</li><li>122</li><li>123</li><li>124</li><li>125</li><li>126</li><li>127</li><li>128</li><li>129</li><li>130</li><li>131</li><li>132</li><li>133</li><li>134</li><li>135</li><li>136</li><li>137</li><li>138</li><li>139</li><li>140</li><li>141</li><li>142</li><li>143</li><li>144</li><li>145</li><li>146</li><li>147</li><li>148</li><li>149</li><li>150</li><li>151</li><li>152</li><li>153</li><li>154</li><li>155</li><li>156</li><li>157</li><li>158</li><li>159</li><li>160</li><li>161</li><li>162</li><li>163</li><li>164</li><li>165</li><li>166</li><li>167</li><li>168</li><li>169</li><li>170</li><li>171</li><li>172</li><li>173</li><li>174</li><li>175</li><li>176</li><li>177</li><li>178</li><li>179</li><li>180</li><li>181</li><li>182</li><li>183</li><li>184</li><li>185</li><li>186</li><li>187</li><li>188</li><li>189</li><li>190</li><li>191</li><li>192</li><li>193</li><li>194</li><li>195</li><li>196</li><li>197</li><li>198</li><li>199</li><li>200</li><li>201</li><li>202</li><li>203</li><li>204</li><li>205</li><li>206</li><li>207</li><li>208</li><li>209</li><li>210</li><li>211</li><li>212</li><li>213</li><li>214</li><li>215</li><li>216</li><li>217</li><li>218</li><li>219</li><li>220</li><li>221</li><li>222</li><li>223</li><li>224</li><li>225</li><li>226</li><li>227</li></ul>
从上面代码可以看出,程序的主要运行流程是:通过开启一个子线程去调用支付宝的支付功能,获取到支付结果后,通过Handler通知UI线程,根据支付结果去显示不同的。
到这里基本上整个开发流程已经大致完成了,具体细节根据需求去修改即可。官方建议支付完成后,将获取到的支付结果上传到自己的服务器,通过官方提供的API进行验证,建议添加该流程。
如果有疑问,可以通过QQ381959281联系作者进行交流