|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\Alipay\Requests; |
| 4 | + |
| 5 | +use Omnipay\Alipay\Responses\LegacyRefundNoPwdResponse; |
| 6 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 7 | +use Omnipay\Common\Message\ResponseInterface; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class LegacyRefundRequest |
| 11 | + * @package Omnipay\Alipay\Requests |
| 12 | + * @link https://doc.open.alipay.com/docs/doc.htm?treeId=66&articleId=103600&docType=1 |
| 13 | + */ |
| 14 | +class LegacyRefundNoPwdRequest extends AbstractLegacyRequest |
| 15 | +{ |
| 16 | + protected $service = 'refund_fastpay_by_platform_nopwd'; |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Get the raw data array for this message. The format of this varies from gateway to |
| 21 | + * gateway, but will usually be either an associative array, or a SimpleXMLElement. |
| 22 | + * |
| 23 | + * @return mixed |
| 24 | + */ |
| 25 | + public function getData() |
| 26 | + { |
| 27 | + $this->setDefaults(); |
| 28 | + |
| 29 | + $this->validate( |
| 30 | + 'partner', |
| 31 | + '_input_charset', |
| 32 | + 'refund_date', |
| 33 | + 'batch_no', |
| 34 | + 'refund_items' |
| 35 | + ); |
| 36 | + |
| 37 | + $this->validateOne( |
| 38 | + 'seller_user_id', |
| 39 | + 'seller_email' |
| 40 | + ); |
| 41 | + |
| 42 | + $this->setBatchNum(count($this->getRefundItems())); |
| 43 | + $this->setRefundDetail($this->getDetailData()); |
| 44 | + |
| 45 | + $data = [ |
| 46 | + 'service' => $this->service, |
| 47 | + 'partner' => $this->getPartner(), |
| 48 | + 'notify_url' => $this->getNotifyUrl(), |
| 49 | + 'seller_user_id' => $this->getPartner(), |
| 50 | + 'refund_date' => $this->getRefundDate(), |
| 51 | + 'batch_no' => $this->getBatchNo(), |
| 52 | + 'batch_num' => $this->getBatchNum(), |
| 53 | + 'detail_data' => $this->getDetailData(), |
| 54 | + '_input_charset' => $this->getInputCharset(), |
| 55 | + ]; |
| 56 | + |
| 57 | + $data['sign'] = $this->sign($data, $this->getSignType()); |
| 58 | + $data['sign_type'] = $this->getSignType(); |
| 59 | + |
| 60 | + return $data; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + protected function setDefaults() |
| 65 | + { |
| 66 | + if (!$this->getRefundDate()) { |
| 67 | + $this->setRefundDate(date('Y-m-d H:i:s')); |
| 68 | + } |
| 69 | + |
| 70 | + if (!$this->getBatchNo()) { |
| 71 | + $this->setBatchNo(date('Ymd') . mt_rand(1000, 9999)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + /** |
| 77 | + * @return mixed |
| 78 | + */ |
| 79 | + public function getRefundDate() |
| 80 | + { |
| 81 | + return $this->getParameter('refund_date'); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * @param $value |
| 87 | + * |
| 88 | + * @return $this |
| 89 | + */ |
| 90 | + public function setRefundDate($value) |
| 91 | + { |
| 92 | + return $this->setParameter('refund_date', $value); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * @return mixed |
| 98 | + */ |
| 99 | + public function getBatchNo() |
| 100 | + { |
| 101 | + return $this->getParameter('batch_no'); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + /** |
| 106 | + * @param $value |
| 107 | + * |
| 108 | + * @return $this |
| 109 | + */ |
| 110 | + public function setBatchNo($value) |
| 111 | + { |
| 112 | + return $this->setParameter('batch_no', $value); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * @param $value |
| 118 | + * |
| 119 | + * @return $this |
| 120 | + */ |
| 121 | + public function setBatchNum($value) |
| 122 | + { |
| 123 | + return $this->setParameter('batch_num', $value); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * @return mixed |
| 129 | + */ |
| 130 | + public function getRefundItems() |
| 131 | + { |
| 132 | + return $this->getParameter('refund_items'); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + /** |
| 137 | + * @param $value |
| 138 | + * |
| 139 | + * @return $this |
| 140 | + */ |
| 141 | + protected function setRefundDetail($value) |
| 142 | + { |
| 143 | + return $this->setParameter('refund_detail', $value); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + protected function getDetailData() |
| 148 | + { |
| 149 | + $strings = []; |
| 150 | + |
| 151 | + foreach ($this->getRefundItems() as $item) { |
| 152 | + $item = (array)$item; |
| 153 | + |
| 154 | + if (!isset($item['trade_no'])) { |
| 155 | + throw new InvalidRequestException('The field `trade_no` is not exist in item'); |
| 156 | + } |
| 157 | + |
| 158 | + if (!isset($item['amount'])) { |
| 159 | + throw new InvalidRequestException('The field `amount` is not exist in item'); |
| 160 | + } |
| 161 | + |
| 162 | + if (!isset($item['reason'])) { |
| 163 | + throw new InvalidRequestException('The field `reason` is not exist in item'); |
| 164 | + } |
| 165 | + |
| 166 | + $strings[] = implode('^', [ $item['trade_no'], $item['amount'], $item['reason'] ]); |
| 167 | + } |
| 168 | + |
| 169 | + return implode('#', $strings); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + /** |
| 174 | + * @return mixed |
| 175 | + */ |
| 176 | + public function getPartner() |
| 177 | + { |
| 178 | + return $this->getParameter('partner'); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + /** |
| 183 | + * @return mixed |
| 184 | + */ |
| 185 | + public function getNotifyUrl() |
| 186 | + { |
| 187 | + return $this->getParameter('notify_url'); |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + /** |
| 192 | + * @return mixed |
| 193 | + */ |
| 194 | + public function getBatchNum() |
| 195 | + { |
| 196 | + return $this->getParameter('batch_num'); |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + /** |
| 201 | + * @return mixed |
| 202 | + */ |
| 203 | + public function getInputCharset() |
| 204 | + { |
| 205 | + return $this->getParameter('_input_charset'); |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + /** |
| 210 | + * @return mixed |
| 211 | + */ |
| 212 | + public function getPaymentType() |
| 213 | + { |
| 214 | + return $this->getParameter('payment_type'); |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | + /** |
| 219 | + * @param $value |
| 220 | + * |
| 221 | + * @return $this |
| 222 | + */ |
| 223 | + public function setPaymentType($value) |
| 224 | + { |
| 225 | + return $this->setParameter('payment_type', $value); |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + /** |
| 230 | + * Send the request with specified data |
| 231 | + * |
| 232 | + * @param mixed $data The data to send |
| 233 | + * |
| 234 | + * @return ResponseInterface |
| 235 | + */ |
| 236 | + public function sendData($data) |
| 237 | + { |
| 238 | + $url = sprintf('%s?%s', $this->getEndpoint(), http_build_query($this->getData())); |
| 239 | + |
| 240 | + $result = $this->httpClient->get($url)->send()->getBody(); |
| 241 | + |
| 242 | + $xml = simplexml_load_string($result); |
| 243 | + $json = json_encode($xml); |
| 244 | + $data = json_decode($json, true); |
| 245 | + |
| 246 | + return $this->response = new LegacyRefundNoPwdResponse($this, $data); |
| 247 | + } |
| 248 | + |
| 249 | + |
| 250 | + /** |
| 251 | + * @param $value |
| 252 | + * |
| 253 | + * @return $this |
| 254 | + */ |
| 255 | + public function setPartner($value) |
| 256 | + { |
| 257 | + return $this->setParameter('partner', $value); |
| 258 | + } |
| 259 | + |
| 260 | + |
| 261 | + /** |
| 262 | + * @param $value |
| 263 | + * |
| 264 | + * @return $this |
| 265 | + */ |
| 266 | + public function setInputCharset($value) |
| 267 | + { |
| 268 | + return $this->setParameter('_input_charset', $value); |
| 269 | + } |
| 270 | + |
| 271 | + |
| 272 | + /** |
| 273 | + * @param $value |
| 274 | + * |
| 275 | + * @return $this |
| 276 | + */ |
| 277 | + public function setNotifyUrl($value) |
| 278 | + { |
| 279 | + return $this->setParameter('notify_url', $value); |
| 280 | + } |
| 281 | + |
| 282 | + |
| 283 | + /** |
| 284 | + * @return mixed |
| 285 | + */ |
| 286 | + public function getSellerEmail() |
| 287 | + { |
| 288 | + return $this->getParameter('seller_email'); |
| 289 | + } |
| 290 | + |
| 291 | + |
| 292 | + /** |
| 293 | + * @param $value |
| 294 | + * |
| 295 | + * @return $this |
| 296 | + */ |
| 297 | + public function setSellerEmail($value) |
| 298 | + { |
| 299 | + return $this->setParameter('seller_email', $value); |
| 300 | + } |
| 301 | + |
| 302 | + |
| 303 | + /** |
| 304 | + * @return mixed |
| 305 | + */ |
| 306 | + public function getSellerId() |
| 307 | + { |
| 308 | + return $this->getSellerUserId(); |
| 309 | + } |
| 310 | + |
| 311 | + |
| 312 | + /** |
| 313 | + * @return mixed |
| 314 | + */ |
| 315 | + public function getSellerUserId() |
| 316 | + { |
| 317 | + return $this->getParameter('seller_user_id'); |
| 318 | + } |
| 319 | + |
| 320 | + |
| 321 | + /** |
| 322 | + * @param $value |
| 323 | + * |
| 324 | + * @return $this |
| 325 | + */ |
| 326 | + public function setSellerId($value) |
| 327 | + { |
| 328 | + return $this->setSellerUserId($value); |
| 329 | + } |
| 330 | + |
| 331 | + |
| 332 | + /** |
| 333 | + * @param $value |
| 334 | + * |
| 335 | + * @return $this |
| 336 | + */ |
| 337 | + public function setSellerUserId($value) |
| 338 | + { |
| 339 | + return $this->setParameter('seller_user_id', $value); |
| 340 | + } |
| 341 | + |
| 342 | + |
| 343 | + /** |
| 344 | + * @param $value |
| 345 | + * |
| 346 | + * @return $this |
| 347 | + */ |
| 348 | + public function setRefundItems($value) |
| 349 | + { |
| 350 | + return $this->setParameter('refund_items', $value); |
| 351 | + } |
| 352 | + |
| 353 | + |
| 354 | + /** |
| 355 | + * @return mixed |
| 356 | + */ |
| 357 | + protected function getRefundDetail() |
| 358 | + { |
| 359 | + return $this->getParameter('refund_detail'); |
| 360 | + } |
| 361 | +} |
0 commit comments