|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\WechatPay\Message; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 7 | +use Omnipay\Common\Message\ResponseInterface; |
| 8 | +use Omnipay\WechatPay\Helper; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class PayBankRequest |
| 12 | + * |
| 13 | + * @package Omnipay\WechatPay\Message |
| 14 | + * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_2 |
| 15 | + * @method PayBankResponse send() |
| 16 | + */ |
| 17 | +class PayBankRequest extends BaseAbstractRequest |
| 18 | +{ |
| 19 | + protected $endpoint = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the raw data array for this message. The format of this varies from gateway to |
| 23 | + * gateway, but will usually be either an associative array, or a SimpleXMLElement. |
| 24 | + * @return mixed |
| 25 | + * @throws InvalidRequestException |
| 26 | + */ |
| 27 | + public function getData() |
| 28 | + { |
| 29 | + $this->validate('mch_id', 'partner_trade_no', 'enc_bank_no', 'enc_true_name', 'bank_code', 'amount', 'desc', 'cert_path', 'key_path'); |
| 30 | + |
| 31 | + $data = array( |
| 32 | + 'mch_id' => $this->getMchId(), |
| 33 | + 'partner_trade_no' => $this->getPartnerTradeNo(), |
| 34 | + 'enc_bank_no' => $this->getEncBankNo(), |
| 35 | + 'enc_true_name' => $this->getEncTrueName(), |
| 36 | + 'bank_code' => $this->getBankCode(), |
| 37 | + 'amount' => $this->getAmount(), |
| 38 | + 'desc' => $this->getDesc(), |
| 39 | + 'nonce_str' => md5(uniqid()), |
| 40 | + ); |
| 41 | + |
| 42 | + $data = array_filter($data); |
| 43 | + |
| 44 | + $data['sign'] = Helper::sign($data, $this->getApiKey()); |
| 45 | + |
| 46 | + return $data; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * @return mixed |
| 52 | + */ |
| 53 | + public function getPartnerTradeNo() |
| 54 | + { |
| 55 | + return $this->getParameter('partner_trade_no'); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * @param mixed $partnerTradeNo |
| 61 | + */ |
| 62 | + public function setPartnerTradeNo($partnerTradeNo) |
| 63 | + { |
| 64 | + $this->setParameter('partner_trade_no', $partnerTradeNo); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * @return mixed |
| 70 | + */ |
| 71 | + public function getEncBankNo() |
| 72 | + { |
| 73 | + return $this->getParameter('enc_bank_no'); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * @param mixed $encBankNo |
| 79 | + */ |
| 80 | + public function setEncBankNo($encBankNo) |
| 81 | + { |
| 82 | + $this->setParameter('enc_bank_no', $encBankNo); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * @return mixed |
| 88 | + */ |
| 89 | + public function getEncTrueName() |
| 90 | + { |
| 91 | + return $this->getParameter('enc_true_name'); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * @param mixed $encTrueName |
| 97 | + */ |
| 98 | + public function setEncTrueName($encTrueName) |
| 99 | + { |
| 100 | + $this->setParameter('enc_true_name', $encTrueName); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * @return mixed |
| 106 | + */ |
| 107 | + public function getBankCode() |
| 108 | + { |
| 109 | + return $this->getParameter('bank_code'); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * @param mixed $bankCode |
| 115 | + */ |
| 116 | + public function setBankCode($bankCode) |
| 117 | + { |
| 118 | + $this->setParameter('bank_code', $bankCode); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /** |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + public function getAmount() |
| 126 | + { |
| 127 | + return $this->getParameter('amount'); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + /** |
| 132 | + * @param mixed $amount |
| 133 | + */ |
| 134 | + public function setAmount($amount) |
| 135 | + { |
| 136 | + $this->setParameter('amount', $amount); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + /** |
| 141 | + * @return mixed |
| 142 | + */ |
| 143 | + public function getDesc() |
| 144 | + { |
| 145 | + return $this->getParameter('desc'); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + /** |
| 150 | + * @param mixed $desc |
| 151 | + */ |
| 152 | + public function setDesc($desc) |
| 153 | + { |
| 154 | + $this->setParameter('desc', $desc); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + /** |
| 159 | + * @return mixed |
| 160 | + */ |
| 161 | + public function getCertPath() |
| 162 | + { |
| 163 | + return $this->getParameter('cert_path'); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + /** |
| 168 | + * @param mixed $certPath |
| 169 | + */ |
| 170 | + public function setCertPath($certPath) |
| 171 | + { |
| 172 | + $this->setParameter('cert_path', $certPath); |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + /** |
| 177 | + * @return mixed |
| 178 | + */ |
| 179 | + public function getKeyPath() |
| 180 | + { |
| 181 | + return $this->getParameter('key_path'); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + /** |
| 186 | + * @param mixed $keyPath |
| 187 | + */ |
| 188 | + public function setKeyPath($keyPath) |
| 189 | + { |
| 190 | + $this->setParameter('key_path', $keyPath); |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + /** |
| 195 | + * Send the request with specified data |
| 196 | + * |
| 197 | + * @param mixed $data The data to send |
| 198 | + * |
| 199 | + * @return ResponseInterface |
| 200 | + */ |
| 201 | + public function sendData($data) |
| 202 | + { |
| 203 | + $body = Helper::array2xml($data); |
| 204 | + $client = new Client(); |
| 205 | + |
| 206 | + $options = [ |
| 207 | + 'body' => $body, |
| 208 | + 'verify' => true, |
| 209 | + 'cert' => $this->getCertPath(), |
| 210 | + 'ssl_key' => $this->getKeyPath(), |
| 211 | + ]; |
| 212 | + $response = $client->request('POST', $this->endpoint, $options)->getBody(); |
| 213 | + $responseData = Helper::xml2array($response); |
| 214 | + |
| 215 | + return $this->response = new PayBankResponse($this, $responseData); |
| 216 | + } |
| 217 | +} |
0 commit comments