让Discuz 7.2支持支付宝双功能接口的解决方法

刚刚测试成功,所以要立马分享出来,由于比较喜欢DZ7.2的纯论坛,轻量一点,简单一点,就图一份简单。由于网站涉及到积分充值,需要集成支付宝,遗憾的是discuz 7.2不支持双功能,只支持纯即时到账和纯担保交易。网上找了很久没找到,最后受美秀lily的博文启发,于是动手改了下,居然成功了。之前比对了下discuz 7.2和dz x2.5的alipay.api.php文件区别,因为在X2.5是成功的,所以也参考了下。

一、下载discuz 7.2补丁

补丁是官方出的,由于discuz 7.2官方提供的下载没有更改,所以还需要自己打个补丁:

discuz 7.2官方下载地址:http://download.comsenz.com/Discuz/7.2/

补丁帖子地址:http://www.discuz.net/thread-1623928-1-1.html

二、修改alipay.api.php

根据官方的说明,二进制补丁后,然后你需要修改下alipay.api.php文件,有两处修改,将create_partner_trade_by_buyer修改成trade_create_by_buyer。

我贴一下,我修改后的文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

/*
    [Discuz!] (C)2001-2009 Comsenz Inc.
    This is NOT a freeware, use is subject to license terms

    $Id: alipay.api.php 18853 2009-07-29 03:21:09Z monkey $
*/


if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

require DISCUZ_ROOT.'./forumdata/cache/cache_alipaycontract.php';
define('STATUS_SELLER_SEND', 4);
define('STATUS_WAIT_BUYER', 5);
define('STATUS_TRADE_SUCCESS', 7);
define('STATUS_REFUND_CLOSE', 17);

function credit_payurl($price, &$orderid) {
    global $bbname, $discuz_userss, $language, $boardurl, $extcredits, $creditstrans, $onlineip, $ec_ratio, $ec_account, $timestamp, $authkey, $charset, $_DCACHE;

    $orderid = gmdate('YmdHis', $timestamp + $_DCACHE['settings']['timeoffset'] * 3600).random(18);

    $args = array(
        'subject'       => $bbname.' - '.$discuz_userss.' - '.$language['credit_payment'],
        'body'          => $language['credit_forum_payment'].' '.$extcredits[$creditstrans]['title'].' '.intval($price * $ec_ratio).' '.$extcredits[$creditstrans]['unit'].' ('.$onlineip.')',
        'service'       => 'trade_create_by_buyer',
        'partner'       => DISCUZ_PARTNER,
        'notify_url'        => $boardurl.'api/trade/credit.notify.php',
        'return_url'        => $boardurl.'api/trade/credit.notify.php',
        'show_url'      => $boardurl,
        '_input_charset'    => $charset,
        'out_trade_no'      => $orderid,
        'price'         => $price,
        'quantity'      => 1,
        'seller_email'      => $ec_account,
    );
    if(DISCUZ_DIRECTPAY) {
        $args['service'] = 'create_direct_pay_by_user';
        $args['payment_type'] = '1';
    } else {
        $args['logistics_type'] = 'EXPRESS';
        $args['logistics_fee'] = 0;
        $args['logistics_payment'] = 'SELLER_PAY';
        $args['payment_type'] = 1;
    }
    return trade_returnurl($args);
}

function trade_payurl($pay, $trade, $tradelog) {
    global $boardurl, $authkey, $charset;

    $args = array(
        'service'       => 'trade_create_by_buyer',
        'partner'       => DISCUZ_PARTNER,
        'notify_url'        => $boardurl.'api/trade/trade.notify.php',
        'return_url'        => $boardurl.'api/trade/trade.notify.php',
        'show_url'      => $tradelog['tid'] ? $boardurl.'viewthread.php?do=tradeinfo&tid='.$tradelog['tid'].'&pid='.$tradelog['pid'] : '',
        '_input_charset'    => $charset,
        'subject'       => $trade['subject'],
        'body'          => $trade['subject'],
        'out_trade_no'      => $tradelog['orderid'],
        'price'         => $tradelog['baseprice'],
        'quantity'      => $tradelog['number'],
        'logistics_type'    => $pay['logistics_type'],
        'logistics_fee'     => $tradelog['transportfee'],
        'logistics_payment'     => $pay['transport'],
        'payment_type'      => $trade['itemtype'],
        'seller_email'      => $trade['account'],
    );

    if($pay['logistics_type'] == 'VIRTUAL') {
        if(DISCUZ_DIRECTPAY) {
            $args['service'] = 'create_direct_pay_by_user';
            $args['payment_type'] = '1';
            unset($args['logistics_type'], $args['logistics_fee'], $args['logistics_payment']);
        } else {
            $args['logistics_type'] = 'EXPRESS';
            $args['logistics_payment'] = 'SELLER_PAY';
        }
    }
    return trade_returnurl($args);
}

function trade_returnurl($args) {
    ksort($args);
    $urlstr = $sign = '';
    foreach($args as $key => $val) {
        if($val !== '') {
            $sign .= '&'.$key.'='.$val;
        }
        $urlstr .= $key.'='.rawurlencode($val).'&';
    }
    $sign = substr($sign, 1);
    $sign = md5($sign.DISCUZ_SECURITYCODE);
    return 'https://www.alipay.com/cooperate/gateway.do?'.$urlstr.'sign='.$sign.'&sign_type=MD5';
}

function trade_notifycheck($type) {
    if(!empty($_POST)) {
        $notify = $_POST;
        $location = FALSE;
    } elseif(!empty($_GET)) {
        $notify = $_GET;
        $location = TRUE;
    } else {
        exit('Access Denied');
    }

    if(dfopen("http://notify.alipay.com/trade/notify_query.do?partner=".DISCUZ_PARTNER."&notify_id=".$notify['notify_id'], 60) !== 'true') {
        exit('Access Denied');
    }

    if($type == 'trade') {
        $urlstr = '';
        foreach($notify as $key => $val) {
            MAGIC_QUOTES_GPC && $val = stripslashes($val);
            $urlstr .= $key.'='.rawurlencode(stripslashes($val)).'&';
        }
    } else {
        ksort($notify);
        $sign = '';
        foreach($notify as $key => $val) {
            if($key != 'sign' and $key != 'sign_type') $sign .= "&$key=$val";
        }

        if($notify['sign'] != md5(substr($sign,1).DISCUZ_SECURITYCODE)) {
            exit('Access Denied');
        }
    }

    if($type == 'credit' && (!DISCUZ_DIRECTPAY && $notify['notify_type'] == 'trade_status_sync' && $notify['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || DISCUZ_DIRECTPAY && $notify['trade_status'] == 'TRADE_FINISHED')
        || $type == 'trade' && $notify['notify_type'] == 'trade_status_sync') {
        return array(
            'validator' => TRUE,
            'status'    => trade_getstatus(!empty($notify['refund_status']) ? $notify['refund_status'] : $notify['trade_status'], 1),
            'order_no'  => $notify['out_trade_no'],
            'price'     => !DISCUZ_DIRECTPAY ? $notify['price'] : $notify['total_fee'],
            'trade_no'  => $notify['trade_no'],
            'notify'    => 'success',
            'location'  => $location
            );
    } else {
        return array(
            'validator' => FALSE,
            'notify'    => 'fail',
            'location'  => $location
            );
    }
}

function trade_getorderurl($orderid) {
    return 'https://www.alipay.com/trade/query_trade_detail.htm?trade_no='.$orderid;
}

function trade_typestatus($method, $status = -1) {
    switch($method) {
        case 'buytrades'    : $methodvalue = array(1, 5, 11, 12);break;
        case 'selltrades'   : $methodvalue = array(2, 4, 10, 13);break;
        case 'successtrades'    : $methodvalue = array(7);break;
        case 'tradingtrades'    : $methodvalue = array(1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 16);break;
        case 'closedtrades' : $methodvalue = array(8, 17);break;
        case 'refundsuccess'    : $methodvalue = array(17);break;
        case 'refundtrades' : $methodvalue = array(14, 15, 16, 17, 18);break;
        case 'unstarttrades'    : $methodvalue = array(0);break;
        case 'eccredittrades'   : $methodvalue = array(7, 17);break;
    }
    return $status != -1 ? in_array($status, $methodvalue) : implode('\',\'', $methodvalue);
}

function trade_getstatus($key, $method = 2) {
    global $language;
    $status[1] = array(
        'WAIT_BUYER_PAY' => 1,
        'WAIT_SELLER_CONFIRM_TRADE' => 2,
        'WAIT_SYS_CONFIRM_PAY' => 3,
        'WAIT_SELLER_SEND_GOODS' => 4,
        'WAIT_BUYER_CONFIRM_GOODS' => 5,
        'WAIT_SYS_PAY_SELLER' => 6,
        'TRADE_FINISHED' => 7,
        'TRADE_CLOSED' => 8,
        'WAIT_SELLER_AGREE' => 10,
        'SELLER_REFUSE_BUYER' => 11,
        'WAIT_BUYER_RETURN_GOODS' => 12,
        'WAIT_SELLER_CONFIRM_GOODS' => 13,
        'WAIT_ALIPAY_REFUND' => 14,
        'ALIPAY_CHECK' => 15,
        'OVERED_REFUND' => 16,
        'REFUND_SUCCESS' => 17,
        'REFUND_CLOSED' => 18
    );
    $status[2] = array(
        0  => $language['trade_unstart'],
        1  => $language['trade_waitbuyerpay'],
        2  => $language['trade_waitsellerconfirm'],
        3  => $language['trade_sysconfirmpay'],
        4  => $language['trade_waitsellersend'],
        5  => $language['trade_waitbuyerconfirm'],
        6  => $language['trade_syspayseller'],
        7  => $language['trade_finished'],
        8  => $language['trade_closed'],
        10 => $language['trade_waitselleragree'],
        11 => $language['trade_sellerrefusebuyer'],
        12 => $language['trade_waitbuyerreturn'],
        13 => $language['trade_waitsellerconfirmgoods'],
        14 => $language['trade_waitalipayrefund'],
        15 => $language['trade_alipaycheck'],
        16 => $language['trade_overedrefund'],
        17 => $language['trade_refundsuccess'],
        18 => $language['trade_refundclosed']
    );
    return $method == -1 ? $status[2] : $status[$method][$key];
}

function trade_setprice($data, &$price, &$pay, &$transportfee) {
    if($data['transport'] == 1) {
        $pay['transport'] = 'SELLER_PAY';
    } elseif($data['transport'] == 2) {
        $pay['transport'] = 'BUYER_PAY';
    } elseif($data['transport'] == 3) {
        $pay['logistics_type'] = 'VIRTUAL';
    } else {
        $pay['transport'] = 'BUYER_PAY_AFTER_RECEIVE';
    }

    if($data['transport'] != 3) {
        if($data['fee'] == 1) {
            $pay['logistics_type'] = 'POST';
            $pay['logistics_fee'] = $data['trade']['ordinaryfee'];
            if($data['transport'] == 2) {
                $price = $price + $data['trade']['ordinaryfee'];
                $transportfee = $data['trade']['ordinaryfee'];
            }
        } elseif($data['fee'] == 2) {
            $pay['logistics_type'] = 'EMS';
            $pay['logistics_fee'] = $data['trade']['emsfee'];
            if($data['transport'] == 2) {
                $price = $price + $data['trade']['emsfee'];
                $transportfee = $data['trade']['emsfee'];
            }
        } else {
            $pay['logistics_type'] = 'EXPRESS';
            $pay['logistics_fee'] = $data['trade']['expressfee'];
            if($data['transport'] == 2) {
                $price = $price + $data['trade']['expressfee'];
                $transportfee = $data['trade']['expressfee'];
            }
        }
    }
}

?>

三、设置dz后台

位置:后台>> 扩展 >> 电子商务 >>支付宝

使用即时到帐接口这个地方请选择否,这个地方是关键字,如果你选择“是”的话,照样会出错“ILLEGAL_PARTNER_EXTERFACE”。

顺便贴一下:
Discuz X论坛系统接入支付宝担保交易接口的方法 http://luck7me.i.sohu.com/blog/view/239755812.htm

发表评论

:?: :razz: :sad: :!: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: :smile: :evil:
贴图 表情 ( ps. 若要贴代码, 请將 "<" 改成 "&lt;" 即可. )

3 + 9 = ?

这篇文章上的评论的RSS feed TrackBack URL