接口模式:
第一步
//获取 token – 成功
a
p
i
e
n
d
p
o
i
n
t
=
′
h
t
t
p
s
:
/
/
a
p
i
.
s
a
n
d
b
o
x
.
p
a
y
p
a
l
.
c
o
m
′
;
/
/
S
a
n
d
b
o
x
e
n
d
p
o
i
n
t
/
/
api_endpoint = 'https://api.sandbox.paypal.com'; // Sandbox endpoint //
apiendpoint=′https://api.sandbox.paypal.com′;//Sandboxendpoint//api_endpoint = ‘https://api.paypal.com’; // Live endpoint
// 在开发平台获取 https://developer.paypal.com/
$client_id = ‘*******’;
$client_secret = ‘*****’;
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); curl_setopt(
ch=curlinit();curlsetopt(ch, CURLOPT_URL,
a
p
i
e
n
d
p
o
i
n
t
.
"
/
v
1
/
o
a
u
t
h
2
/
t
o
k
e
n
"
)
;
c
u
r
l
s
e
t
o
p
t
(
api_endpoint."/v1/oauth2/token"); curl_setopt(
apiendpoint."/v1/oauth2/token");curlsetopt(ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
c
h
,
C
U
R
L
O
P
T
P
O
S
T
,
1
)
;
c
u
r
l
s
e
t
o
p
t
(
ch, CURLOPT_POST, 1); curl_setopt(
ch,CURLOPTPOST,1);curlsetopt(ch, CURLOPT_USERPWD,
c
l
i
e
n
t
i
d
.
"
:
"
.
client_id.":".
clientid.":".client_secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, “grant_type=client_credentials”);
r
e
s
u
l
t
=
c
u
r
l
e
x
e
c
(
result = curl_exec(
result=curlexec(ch);
r
e
s
p
o
n
s
e
=
j
s
o
n
d
e
c
o
d
e
(
response = json_decode(
response=jsondecode(result);
e
r
r
o
r
=
c
u
r
l
e
r
r
o
r
(
error = curl_error(
error=curlerror(ch);
$access_token =
r
e
s
p
o
n
s
e
−
>
a
c
c
e
s
s
t
o
k
e
n
;
v
a
r
d
u
m
p
(
response->access_token; var_dump(
response−>accesstoken;vardump(access_token);
第二步
$post_data = ‘{
“intent”: “sale”,
“payer”: {
“payment_method”: “paypal”
},
“transactions”: [{
“amount”: {
“total”: “20.00”,//修改这里的金额
“currency”: “USD”
}
}],
“redirect_urls”: {
“return_url”: “回调地址”,
“cancel_url”: “回调地址”
}
}’;
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); curl_setopt(
ch=curlinit();curlsetopt(ch, CURLOPT_URL,
a
p
i
e
n
d
p
o
i
n
t
.
"
/
v
1
/
p
a
y
m
e
n
t
s
/
p
a
y
m
e
n
t
"
)
;
c
u
r
l
s
e
t
o
p
t
(
api_endpoint."/v1/payments/payment"); curl_setopt(
apiendpoint."/v1/payments/payment");curlsetopt(ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
c
h
,
C
U
R
L
O
P
T
P
O
S
T
,
1
)
;
c
u
r
l
s
e
t
o
p
t
(
ch, CURLOPT_POST, 1); curl_setopt(
ch,CURLOPTPOST,1);curlsetopt(ch, CURLOPT_POSTFIELDS, $post_data);
$access_token = ‘第一步获取的’;
h
e
a
d
e
r
s
=
a
r
r
a
y
(
"
C
o
n
t
e
n
t
−
T
y
p
e
:
a
p
p
l
i
c
a
t
i
o
n
/
j
s
o
n
"
,
"
A
u
t
h
o
r
i
z
a
t
i
o
n
:
B
e
a
r
e
r
"
.
headers = array( "Content-Type: application/json", "Authorization: Bearer ".
headers=array("Content−Type:application/json","Authorization:Bearer".access_token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
r
e
s
u
l
t
=
c
u
r
l
e
x
e
c
(
result = curl_exec(
result=curlexec(ch);
r
e
s
p
o
n
s
e
=
j
s
o
n
d
e
c
o
d
e
(
response = json_decode(
response=jsondecode(result);
获取 支付的链接 links 第二个
第三步
根据第二步 返回的俩个参数 GET 请求
$payment_id = ‘PAYID-MVQFE2Q14187575CY193100J’;
$payer_id = ‘WUPZR2EKB6EKA’; // Payer ID from the URL parameter
KaTeX parse error: Expected '}', got 'EOF' at end of input: …"payer_id": "'.payer_id.‘"
}’;
c
h
=
c
u
r
l
i
n
i
t
(
)
;
c
u
r
l
s
e
t
o
p
t
(
ch = curl_init(); curl_setopt(
ch=curlinit();curlsetopt(ch, CURLOPT_URL,
a
p
i
e
n
d
p
o
i
n
t
.
"
/
v
1
/
p
a
y
m
e
n
t
s
/
p
a
y
m
e
n
t
/
"
.
api_endpoint."/v1/payments/payment/".
apiendpoint."/v1/payments/payment/".payment_id.“/execute”);
curl_setopt(
c
h
,
C
U
R
L
O
P
T
R
E
T
U
R
N
T
R
A
N
S
F
E
R
,
1
)
;
c
u
r
l
s
e
t
o
p
t
(
ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt(
ch,CURLOPTRETURNTRANSFER,1);curlsetopt(ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $execute_payment);
h
e
a
d
e
r
s
=
a
r
r
a
y
(
"
C
o
n
t
e
n
t
−
T
y
p
e
:
a
p
p
l
i
c
a
t
i
o
n
/
j
s
o
n
"
,
"
A
u
t
h
o
r
i
z
a
t
i
o
n
:
B
e
a
r
e
r
"
.
headers = array( "Content-Type: application/json", "Authorization: Bearer ".
headers=array("Content−Type:application/json","Authorization:Bearer".access_token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
r
e
s
u
l
t
=
c
u
r
l
e
x
e
c
(
result = curl_exec(
result=curlexec(ch);
r
e
s
p
o
n
s
e
=
j
s
o
n
d
e
c
o
d
e
(
response = json_decode(
response=jsondecode(result,true);
判断是否支付成功 – 结束
form 表单提交
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paypalsubmit" name="paypalsubmit">
<input type="hidden" name="item_name_1" value="Analog Devices LTC1966MPMS8#PBF"/>
<input type="hidden" name="amount_1" value="15"/>
<input type="hidden" name="quantity_1" value="1"/>
<input type="hidden" name="cmd" value="_cart"/>
<input type="hidden" name="upload" value="1"/>
<input type="hidden" name="business" value="修改成自己的邮箱"/>
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="invoice" value="2023112206496861"/>
<input type="hidden" name="lc" value="zh-cn"/>
<input type="hidden" name="rm" value="2"/>
<input type="hidden" name="no_note" value="1"/>
<input type="hidden" name="no_shipping" value="1"/>
<input type="hidden" name="charset" value="utf-8"/>
<input type="hidden" name="return" value="回调地址 返回"/>
<input type="hidden" name="notify_url" value="回调地址 通知"/>
<input type="hidden" name="cancel_return" value="回调地址 取消"/>
<input type="hidden" name="paymentaction" value="sale"/>
<input type="hidden" name="custom" value="2023112206496861"/>
<input type="hidden" name="bn" value="OpenCart_2.0_WPS"/>
</form>
<script>
document.forms['paypalsubmit'].submit();
</script>
</body>
</html>