| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yansongda\Pay\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\Psr7\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GuzzleHttp\Psr7\ServerRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Http\Message\MessageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yansongda\Artful\Artful; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yansongda\Artful\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yansongda\Artful\Exception\ContainerException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yansongda\Artful\Exception\InvalidParamsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yansongda\Artful\Exception\ServiceNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Yansongda\Artful\Plugin\ParserPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Yansongda\Artful\Rocket; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Yansongda\Pay\Contract\ProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Yansongda\Pay\Event\CallbackReceived; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Yansongda\Pay\Event\MethodCalled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Yansongda\Pay\Pay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\AddPayloadSignaturePlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\AddRadarPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\AppCallbackPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\CallbackPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\FormatPayloadBizContentPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\ResponsePlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\StartPlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Yansongda\Pay\Plugin\Alipay\V2\VerifySignaturePlugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Yansongda\Supports\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Yansongda\Supports\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @method ResponseInterface|Rocket app(array $order)      APP 支付 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @method Collection|Rocket        pos(array $order)      刷卡支付(付款码,被扫码) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @method Collection|Rocket        scan(array $order)     扫码支付(摄像头,主动扫) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @method Collection|Rocket        transfer(array $order) 帐户转账 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @method ResponseInterface|Rocket h5(array $order)       手机网站支付 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @method ResponseInterface|Rocket web(array $order)      电脑支付 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @method Collection|Rocket        mini(array $order)     小程序支付 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | class Alipay implements ProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public const URL = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         Pay::MODE_SANDBOX => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @throws ServiceNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $plugin = '\Yansongda\Pay\Shortcut\Alipay\\'.Str::studly($shortcut).'Shortcut'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return Artful::shortcut($plugin, ...$params); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return Artful::artful($plugins, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @throws ServiceNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function query(array $order): Collection|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         Event::dispatch(new MethodCalled('alipay', __METHOD__, $order, null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $this->__call('query', [$order]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @throws ServiceNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function cancel(array $order): Collection|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         Event::dispatch(new MethodCalled('alipay', __METHOD__, $order, null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $this->__call('cancel', [$order]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @throws ServiceNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function close(array $order): Collection|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         Event::dispatch(new MethodCalled('alipay', __METHOD__, $order, null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $this->__call('close', [$order]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws ServiceNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function refund(array $order): Collection|Rocket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         Event::dispatch(new MethodCalled('alipay', __METHOD__, $order, null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $this->__call('refund', [$order]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $request = $this->getCallbackParams($contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         Event::dispatch(new CallbackReceived('alipay', $request->all(), $params, null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $this->pay([CallbackPlugin::class], $request->merge($params)->all()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @throws ContainerException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @throws InvalidParamsException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |     public function appCallback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         $request = $this->getCallbackParams($contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         return $this->pay([AppCallbackPlugin::class], $request->merge($params)->all()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function success(): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return new Response(200, [], 'success'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function mergeCommonPlugins(array $plugins): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             [StartPlugin::class], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $plugins, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             [FormatPayloadBizContentPlugin::class, AddPayloadSignaturePlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if (is_array($contents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             return Collection::wrap($contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         if ($contents instanceof ServerRequestInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 $contents->getParsedBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $request = ServerRequest::fromGlobals(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return Collection::wrap( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             array_merge($request->getQueryParams(), $request->getParsedBody()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  |