1
+ <?php
2
+
3
+ /**
4
+ * Public Alipay Base Pay Ment Restful API
5
+ * Main File
6
+ * @access private
7
+ * @link http://cenegd.com/
8
+ * @author cenegd <cenegd@live.com>
9
+ * @copyright cenegd
10
+ **/
11
+
12
+ // 全局通信密码
13
+ $ key = 'input_you_secert_key ' ;
14
+
15
+ ini_set ('display_errors ' , false );
16
+ error_reporting (0 );
17
+
18
+ header ("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 " );
19
+ header ("Pragma: no-cache " );
20
+ header ('Content-Type: application/json ' );
21
+
22
+ require_once '../library/medoo.php ' ;
23
+
24
+ $ database = new medoo ();
25
+
26
+ function message ($ message_id , $ data ='' ) {
27
+ $ message_list = array (
28
+ 0 => 'Order is exists ' , // 订单存在
29
+ 1 => 'Order is not exists ' , // 订单不存在
30
+ 2 => 'Order is exists, but is used for other appid ' , // 订单存在并且被用于其他 appid
31
+ 3 => 'Order is exists, but is used for this appid ' , // 订单存在并且被当前 appid 使用
32
+ 4 => 'Order did not use ' , // 订单没有被使用
33
+ 5 => 'Order used with this request ' , // 成功使用订单给这个应用
34
+ );
35
+ $ json = array (
36
+ 'error ' => false ,
37
+ 'message_id ' => $ message_id ,
38
+ 'message ' => $ message_list [$ message_id ]
39
+ );
40
+
41
+ if ($ data ) {
42
+ $ json ['data ' ] = $ data ;
43
+ }
44
+
45
+ echo json_encode ($ json );
46
+ }
47
+
48
+ /* 统一输出函数 */
49
+ function message_error ($ message ) {
50
+ echo json_encode (array (
51
+ 'error ' => true ,
52
+ 'message ' => $ message
53
+ ));
54
+ }
55
+
56
+ if (isset ($ _GET ['application ' ], $ _GET ['method ' ], $ _GET ['apikey ' ])) {
57
+ $ appid = (int )$ _GET ['application ' ];
58
+ $ method = $ _GET ['method ' ];
59
+ $ apikey = $ _GET ['apikey ' ];
60
+
61
+ $ application = $ database ->get ('application ' , array ('id ' ,'apikey ' ), array ('id ' => $ appid ));
62
+
63
+ if ($ application ) {
64
+ if ($ apikey === $ application ['apikey ' ]) {
65
+ $ apikey = $ application ['apikey ' ];
66
+
67
+ if ($ method == 'new_order ' ) {
68
+
69
+ /* 方法 new_order 开始 */
70
+
71
+ if (isset ($ _POST ['sig ' ], $ _POST ['tradeNo ' ], $ _POST ['desc ' ], $ _POST ['time ' ], $ _POST ['username ' ], $ _POST ['userid ' ], $ _POST ['amount ' ], $ _POST ['status ' ])) {
72
+ /**
73
+ * 给软件的接口,将订单添加到数据库
74
+ * 这段代码采用 echo 而不是标准输出函数输出,因为是给别的程序看的
75
+ **/
76
+
77
+ $ sig = $ _POST ['sig ' ]; //签名
78
+ $ tradeNo = $ _POST ['tradeNo ' ]; //交易号
79
+ $ payname = $ _POST ['desc ' ]; //交易名称(付款说明)
80
+ $ time = $ _POST ['time ' ]; //付款时间
81
+ $ username = $ _POST ['username ' ]; //客户名称
82
+ $ userid = $ _POST ['userid ' ]; //客户id
83
+ $ amount = $ _POST ['amount ' ]; //交易额
84
+ $ status = $ _POST ['status ' ]; //交易状态
85
+
86
+ if (strtoupper (md5 ("$ tradeNo| $ payname| $ time| $ username| $ userid| $ amount| $ status| $ key " )) == $ sig ) {
87
+ // 参数正确,将行添加到数据库
88
+ $ database ->insert ('order ' , array (
89
+ 'tradeNo ' => $ tradeNo ,
90
+ 'payname ' => $ payname ,
91
+ 'time ' => $ time ,
92
+ 'username ' => $ username ,
93
+ 'userid ' => $ userid ,
94
+ 'amount ' => $ amount ,
95
+ 'status ' => $ status ,
96
+ ));
97
+
98
+ // 成功
99
+ echo 'success ' ;
100
+ } else {
101
+ // 校对参数失败,参数可能是伪造的
102
+ echo 'param sig has error ' ;
103
+ }
104
+ } else {
105
+ // 参数提交的不够
106
+ echo 'Params is not exists ' ;
107
+ }
108
+
109
+ /* 方法 new_order 结束 */
110
+ } else if ($ method == 'save_status ' ) {
111
+ if (isset ($ _POST ['tradeNo ' ])) {
112
+
113
+ }
114
+ } else if ($ method == 'check_order ' ) {
115
+ /* 方法 check_order 开始 */
116
+
117
+ if (isset ($ _GET ['tradeNo ' ])) {
118
+ /**
119
+ * $tradeNo // 订单编号
120
+ * $use // 如果这个订单没有使用,是否使用这个订单给 appid
121
+ **/
122
+
123
+ $ tradeNo = $ _GET ['tradeNo ' ];
124
+
125
+ $ order = $ database ->get ('order ' , array ('tradeNo ' , 'payname ' , 'time ' , 'username ' , 'userid ' , 'amount ' , 'status ' , 'use_timestamp ' , 'use_appid ' ), array ('tradeNo ' => $ tradeNo ));
126
+
127
+ if ($ order ) {
128
+ if ($ order ['use_appid ' ]) {
129
+ // 该订单被使用过
130
+ if ($ order ['use_appid ' ] != $ appid ) {
131
+ // 被其他 appid 使用了
132
+ message (2 );
133
+ } else {
134
+ // 被当前 appid 使用了
135
+ message (3 , $ order );
136
+ }
137
+ } else {
138
+ // 该订单没有被使用过
139
+ if (isset ($ _GET ['update ' ])) {
140
+ // 如果没有使用过,那就标记为使用
141
+ $ updated = $ database ->update ('order ' , array ('use_timestamp ' =>time (), 'use_appid ' =>$ appid ), array ('tradeNo ' =>$ tradeNo ));
142
+ if ($ updated >= 1 ) {
143
+ message (5 , $ order );
144
+ } else {
145
+ message_error ('Failed to update order ' );
146
+ }
147
+ } else {
148
+ // 该订单没有被使用过
149
+ message (4 , $ order );
150
+ }
151
+ }
152
+ } else {
153
+ // 订单不存在
154
+ message (1 );
155
+ }
156
+ } else {
157
+ // tradeNo 参数没有提交
158
+ message_error ('Param tradeNo is not exists ' );
159
+ }
160
+
161
+ /* 方法 check_order 结束 */
162
+ } else {
163
+ message_error ('Method is not exists ' );
164
+ }
165
+ } else {
166
+ // 参数 apikey 不正确
167
+ message_error ('Param apikey has error ' );
168
+ }
169
+ } else {
170
+ // appid 不存在
171
+ message_error ('Application is not exists ' );
172
+ }
173
+ } else {
174
+ // 提交的参数不够
175
+ message_error ('Params is not exists ' );
176
+ }
0 commit comments