FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SSRPanel/app/Http/Controllers/PaymentController.php at master · programmejava/SSRPanel · GitHub
programmejava
SSRPanel
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SSRPanel
/
app
/
Http
/
Controllers
/
PaymentController.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
205 lines (163 loc) · 6.49 KB
Breadcrumbs
SSRPanel
/
app
/
Http
/
Controllers
/
PaymentController.php
Copy path
File metadata and controls
205 lines (163 loc) · 6.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
namespace
App
\
Http
\
Controllers
;
use
App
\
Http
\
Models
\
Goods
;
use
App
\
Http
\
Models
\
Paypal
;
use
Illuminate
\
Http
\
Request
;
use
Response
;
use
Redirect
;
use
Cache
;
use
Log
;
use
PayPal
\
Api
\
Amount
;
use
PayPal
\
Api
\
Details
;
use
PayPal
\
Api
\
Item
;
use
PayPal
\
Api
\
ItemList
;
use
PayPal
\
Api
\
Payer
;
use
PayPal
\
Api
\
Payment
;
use
PayPal
\
Api
\
PaymentExecution
;
use
PayPal
\
Api
\
RedirectUrls
;
use
PayPal
\
Api
\
Transaction
;
use
PayPal
\
Api
\
ShippingAddress
;
use
PayPal
\
Rest
\
ApiContext
;
use
PayPal
\
Auth
\
OAuthTokenCredential
;
class
PaymentController
extends
Controller
{
protected
static
$
config
;
private
$
apiContext
;
function
__construct
()
{
self
::
$
config
=
$
this
->
systemConfig
();
$
this
->
apiContext
=
new
ApiContext
(
new
OAuthTokenCredential
(
self
::
$
config
[
'
paypal_client_id
'
],
self
::
$
config
[
'
paypal_client_secret
'
])
);
$
this
->
apiContext
->
setConfig
([
'
mode
'
=>
'
sandbox
'
,
'
log.LogEnabled
'
=>
true
,
'
log.FileName
'
=>
storage_path
(
'
logs/paypal.log
'
),
'
log.LogLevel
'
=>
'
DEBUG
'
,
// 测试DEBUG,生产环境INFO
'
cache.enabled
'
=>
true
,
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
//'log.AdapterFactory' => '\PayPal\Log\DefaultLogFactory' // Factory class implementing \PayPal\Log\PayPalLogFactory
]);
}
// 创建支付
public
function
create
(
Request
$
request
)
{
$
oid
=
$
request
->
get
(
'
oid
'
);
$
goods_id
=
$
request
->
get
(
'
goods_id
'
);
$
user
=
$
request
->
session
()->
get
(
'
user
'
);
// 商品信息
$
goods
=
Goods
::
query
()->
where
(
'
id
'
,
$
goods_id
)->
first
();
if
(!
$
goods
) {
//TODO:购买商品页需要做判断,出现异常时挂掉
$
request
->
session
()->
flash
(
'
paypalErrorMsg
'
,
'
创建支付订单失败:所购服务不存在
'
);
return
Redirect
::
back
();
}
// 设置支付信息
$
payer
=
new
Payer
();
$
payer
->
setPaymentMethod
(
"
paypal
"
);
// 设置所购商品信息,包含名称、数量、SKU、价格
$
item1
=
new
Item
();
$
item1
->
setName
(
$
goods
->
name
)->
setCurrency
(
'
USD
'
)->
setQuantity
(
1
)->
setSku
(
$
goods
->
sku
)->
setPrice
(
$
goods
->
price
/
100
);
$
itemList
=
new
ItemList
();
$
itemList
->
setItems
([
$
item1
]);
/*
// 设定收货地址信息,防止用户自付款时可改
$address = new ShippingAddress();
$address->setRecipientName($user['username'])
->setLine1('余杭区')
->setLine2('文一西路969号西溪园区')
->setCity('杭州市')
->setState('浙江省')
->setPhone('+8613800000000')
->setPostalCode('311100')
->setCountryCode('CN');
// 商品列表写入设定好的地址信息
$itemList->setShippingAddress($address);
*/
// 设置单据运费、税费、小计算
$
details
=
new
Details
();
$
details
->
setShipping
(
0
)->
setTax
(
0
)->
setSubtotal
(
$
goods
->
price
/
100
);
// 设定单据金额
$
amount
=
new
Amount
();
$
amount
->
setCurrency
(
"
USD
"
)->
setTotal
(
$
goods
->
price
/
100
)->
setDetails
(
$
details
);
// 跳转页
$
redirectUrls
=
new
RedirectUrls
();
$
redirectUrls
->
setReturnUrl
(
url
(
"
payment/execute?subtotal=
"
.
$
goods
->
price
/
100
))->
setCancelUrl
(
url
(
"
payment/cancel
"
));
// 设定交易描述
$
transaction
=
new
Transaction
();
$
transaction
->
setAmount
(
$
amount
)->
setItemList
(
$
itemList
)->
setDescription
(
"
购买虚拟服务
"
)->
setInvoiceNumber
(
uniqid
());
// 创建支付
$
payment
=
new
Payment
();
$
payment
->
setIntent
(
"
sale
"
)->
setPayer
(
$
payer
)->
setRedirectUrls
(
$
redirectUrls
)->
setTransactions
([
$
transaction
]);
try
{
$
payment
->
create
(
$
this
->
apiContext
);
}
catch
(
\
Exception
$
ex
) {
Log
::
error
(
$
ex
->
getMessage
());
Log
::
error
(
'
PayPal授权失败,可能是接口配置错误
'
);
$
request
->
session
()->
flash
(
'
paypalErrorMsg
'
,
'
PayPal授权失败,可能是接口配置错误
'
);
return
Redirect
::
back
();
}
// 得到支付授权跳转页(给用户点确认付款用)
$
approvalUrl
=
$
payment
->
getApprovalLink
();
return
Redirect
::
to
(
$
approvalUrl
);
}
// 执行支付
public
function
execute
(
Request
$
request
)
{
\
Log
::
info
(
'
execute_params:
'
.
var_export
(
$
request
->
all
(),
true
));
$
subtotal
=
$
request
->
get
(
'
subtotal
'
);
$
paymentId
=
$
request
->
get
(
'
paymentId
'
);
$
token
=
$
request
->
get
(
'
token
'
);
$
PayerID
=
$
request
->
get
(
'
PayerID
'
);
if
(
empty
(
$
paymentId
) ||
empty
(
$
token
) ||
empty
(
$
PayerID
)) {
$
request
->
session
()->
flash
(
'
paypalErrorMsg
'
,
'
支付回调地址错误
'
);
return
Redirect
::
to
(
'
user/goodsList
'
);
}
else
{
// 根据支付单据获取支付信息
$
payment
=
Payment
::
get
(
$
paymentId
,
$
this
->
apiContext
);
$
details
=
new
Details
();
$
details
->
setShipping
(
0
)->
setTax
(
0
)->
setSubtotal
(
$
subtotal
);
$
amount
=
new
Amount
();
$
amount
->
setCurrency
(
'
USD
'
)->
setTotal
(
$
subtotal
)->
setDetails
(
$
details
);
$
transaction
=
new
Transaction
();
$
transaction
->
setAmount
(
$
amount
);
// 执行支付
$
execution
=
new
PaymentExecution
();
$
execution
->
setPayerId
(
$
PayerID
)->
addTransaction
(
$
transaction
);
try
{
$
result
=
$
payment
->
execute
(
$
execution
,
$
this
->
apiContext
);
\
Log
::
info
(
var_export
(
$
result
,
true
));
// 支付成功,写入支付单据信息
}
catch
(
\
Exception
$
ex
) {
var_dump
(
$
ex
);
echo
"
支付失败
"
;
exit
(
1
);
}
\
Log
::
info
(
var_export
(
$
payment
,
true
));
return
$
payment
;
}
}
// 取消支付
public
function
cancel
(
Request
$
request
)
{
var_dump
(
$
request
->
all
());
echo
'
取消支付
'
;
}
// 查询支付状态
public
function
query
()
{
}
// 写入日志
private
function
log
(
$
oid
,
$
invoice_number
=
''
,
$
items
=
''
,
$
response_data
=
''
,
$
error
=
''
)
{
$
paypal
=
new
Paypal
();
$
paypal
->
oid
=
$
oid
;
$
paypal
->
invoice_number
=
$
invoice_number
;
$
paypal
->
items
=
$
items
;
$
paypal
->
response_data
=
$
response_data
;
$
paypal
->
error
=
$
error
;
$
paypal
->
save
();
return
$
paypal
->
id
;
}
}
Back
|
FazBrowse Home
|
New Git URL