13
13
use Mantoufan \model \ProductCodeType ;
14
14
use Mantoufan \model \SettlementStrategy ;
15
15
use Mantoufan \model \TerminalType ;
16
+ use Mantoufan \request \notify \AlipayAcNotify ;
16
17
use Mantoufan \request \pay \AlipayPayRequest ;
17
- use Mantoufan \response \NotifyResponse ;
18
18
use Mantoufan \SignatureTool ;
19
+ use \Exception ;
19
20
20
21
class AliPayGlobal
21
22
{
@@ -26,6 +27,7 @@ class AliPayGlobal
26
27
private $ client_id ;
27
28
private $ is_sandbox ;
28
29
private $ alipayPublicKey ;
30
+ private $ merchantPrivateKey ;
29
31
30
32
function __construct ($ params )
31
33
{
@@ -37,11 +39,12 @@ function __construct($params)
37
39
'is_sandbox ' => false ,
38
40
), $ params );
39
41
$ this ->alipayPublicKey = $ params ['alipayPublicKey ' ];
42
+ $ this ->merchantPrivateKey = $ params ['merchantPrivateKey ' ];
40
43
$ this ->client_id = $ params ['client_id ' ];
41
44
$ this ->is_sandbox = $ params ['is_sandbox ' ];
42
45
$ this ->alipayClient = new DefaultAlipayClient (
43
46
constant (Endpoint::class . ':: ' . $ params ['endpoint_area ' ]),
44
- $ params [ ' merchantPrivateKey ' ] ,
47
+ $ this -> merchantPrivateKey ,
45
48
$ this ->alipayPublicKey
46
49
);
47
50
}
@@ -63,7 +66,7 @@ function getPath($key)
63
66
return str_replace ('{sandbox} ' , $ this ->is_sandbox ? 'sandbox/ ' : '' , self ::PATHS [$ key ]);
64
67
}
65
68
66
- function checkout ($ params )
69
+ function cashier ($ params )
67
70
{
68
71
$ params = array_merge (array (
69
72
'notify_url ' => '' ,
@@ -132,20 +135,105 @@ function checkout($params)
132
135
}
133
136
}
134
137
135
- function notify ()
138
+ function getNotify ()
136
139
{
137
- $ notifyResponse = new NotifyResponse ();
138
- $ response = $ notifyResponse ->getResponse ();
139
- var_dump ($ response );
140
+ $ alipayAcNotify = new AlipayAcNotify ();
141
+ $ notifyPaymentRequest = $ alipayAcNotify ->getNotifyPaymentRequest ();
140
142
$ result = SignatureTool::verify (
141
- $ response ->getHttpMethod (),
142
- $ this -> getPath ( ' payments/pay ' ) ,
143
- $ response ->getClientId (),
144
- $ response -> getRspTime (),
145
- $ response -> getRspBody (),
146
- $ response ->getSignature (),
143
+ $ notifyPaymentRequest ->getHttpMethod (),
144
+ $ _SERVER [ ' PHP_SELF ' ] ,
145
+ $ notifyPaymentRequest ->getClientId (),
146
+ $ notifyPaymentRequest -> getRsqTime (),
147
+ $ notifyPaymentRequest -> getRsqBody (),
148
+ $ notifyPaymentRequest ->getSignature (),
147
149
$ this ->alipayPublicKey
148
150
);
149
- var_dump ($ result );
151
+ if ($ result === 0 ) {
152
+ throw Exception ('Invalid Signature ' );
153
+ }
154
+ return $ notifyPaymentRequest ;
155
+ }
156
+
157
+ function sendNotifyResponse ()
158
+ {
159
+ $ alipayAcNotify = new AlipayAcNotify ();
160
+ $ alipayAcNotify ->sendNotifyResponse ();
161
+ }
162
+
163
+ function sendNotifyResponseWithRSA ()
164
+ {
165
+ $ alipayAcNotify = new AlipayAcNotify ();
166
+ $ alipayAcNotify ->sendNotifyResponseWithRSA (array (
167
+ 'merchantPrivateKey ' => $ this ->merchantPrivateKey ,
168
+ ));
169
+ }
170
+
171
+ function agreement ($ params )
172
+ {
173
+ $ params = array_merge (array (
174
+ 'notify_url ' => '' ,
175
+ 'return_url ' => '' ,
176
+ 'amount ' => array (
177
+ 'currency ' => 'USD ' ,
178
+ 'value ' => '' ,
179
+ ),
180
+ 'order ' => array (
181
+ 'id ' => null ,
182
+ 'desc ' => '' ,
183
+ 'extend_info ' => array (
184
+ 'china_extra_trans_info ' => array (
185
+ 'business_type ' => '' ,
186
+ ),
187
+ ),
188
+ ),
189
+ 'payment_request_id ' => null ,
190
+ ), $ params );
191
+
192
+ $ alipayPayRequest = new AlipayPayRequest ();
193
+ $ alipayPayRequest ->setPath ($ this ->getPath ('payments/pay ' ));
194
+ $ alipayPayRequest ->setClientId ($ this ->client_id );
195
+
196
+ $ alipayPayRequest ->setProductCode (ProductCodeType::AGREEMENT_PAYMENT );
197
+ $ alipayPayRequest ->setPaymentNotifyUrl ($ params ['notify_url ' ]);
198
+ $ alipayPayRequest ->setPaymentRedirectUrl ($ params ['return_url ' ]);
199
+ $ alipayPayRequest ->setPaymentRequestId ($ params ['payment_request_id ' ] ?? self ::CreatePaymentRequestId ());
200
+
201
+ $ paymentMethod = new PaymentMethod ();
202
+ $ paymentMethod ->setPaymentMethodType (CustomerBelongsTo::ALIPAY_CN );
203
+ $ alipayPayRequest ->setPaymentMethod ($ paymentMethod );
204
+
205
+ $ amount = new Amount ();
206
+ $ amount ->setCurrency ($ params ['amount ' ]['currency ' ]);
207
+ $ amount ->setValue ($ params ['amount ' ]['value ' ]);
208
+
209
+ $ order = new Order ();
210
+ $ order ->setOrderDescription ($ params ['order ' ]['desc ' ]);
211
+ $ order ->setReferenceOrderId ($ params ['order ' ]['id ' ] ?? self ::CreateOredrId ());
212
+ $ order ->setOrderAmount ($ amount );
213
+
214
+ $ chinaExtraTransInfo = new ChinaExtraTransInfo ();
215
+ $ chinaExtraTransInfo ->setBusinessType ($ params ['order ' ]['extend_info ' ]['china_extra_trans_info ' ]['business_type ' ]);
216
+ $ extendInfo = $ chinaExtraTransInfo ;
217
+
218
+ $ extendInfo = new ExtendInfo ();
219
+ $ extendInfo ->setChinaExtraTransInfo ($ chinaExtraTransInfo );
220
+ $ order ->setExtendInfo ($ extendInfo . '' );
221
+
222
+ $ env = new Env ();
223
+ $ env ->setTerminalType (TerminalType::WEB );
224
+ $ order ->setEnv ($ env );
225
+
226
+ $ alipayPayRequest ->setPaymentAmount ($ amount );
227
+ $ alipayPayRequest ->setOrder ($ order );
228
+
229
+ $ settlementStrategy = new SettlementStrategy ();
230
+ $ settlementStrategy ->setSettlementCurrency ($ params ['amount ' ]['currency ' ]);
231
+ $ alipayPayRequest ->setSettlementStrategy ($ settlementStrategy );
232
+
233
+ try {
234
+ return $ this ->alipayClient ->execute ($ alipayPayRequest );
235
+ } catch (Exception $ e ) {
236
+ throw $ e ;
237
+ }
150
238
}
151
239
}
0 commit comments