|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\WechatPay\Message; |
| 4 | + |
| 5 | +use Guzzle\Http\Client; |
| 6 | +use Omnipay\Common\Exception\InvalidRequestException; |
| 7 | +use Omnipay\Common\Message\ResponseInterface; |
| 8 | +use Omnipay\WechatPay\Helper; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class PromotionTransferRequest |
| 12 | + * |
| 13 | + * @package Omnipay\WechatPay\Message |
| 14 | + * @link https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2 |
| 15 | + * @method PromotionTransferResponse send() |
| 16 | + */ |
| 17 | +class PromotionTransferRequest extends BaseAbstractRequest |
| 18 | +{ |
| 19 | + protected $endpoint = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * Get the raw data array for this message. The format of this varies from gateway to |
| 24 | + * gateway, but will usually be either an associative array, or a SimpleXMLElement. |
| 25 | + * @return mixed |
| 26 | + * @throws InvalidRequestException |
| 27 | + */ |
| 28 | + public function getData() |
| 29 | + { |
| 30 | + $this->validate('app_id', 'mch_id', 'partner_trade_no', 'cert_path', 'key_path'); |
| 31 | + |
| 32 | + $data = array( |
| 33 | + 'mch_appid' => $this->getAppId(), |
| 34 | + 'mchid' => $this->getMchId(), |
| 35 | + 'device_info' => $this->getDeviceInfo(), // <optional> |
| 36 | + 'partner_trade_no' => $this->getPartnerTradeNo(), |
| 37 | + 'openid' => $this->getOpenId(), |
| 38 | + 'check_name' => $this->getCheckName(), // <NO_CHECK or FORCE_CHECK> |
| 39 | + 're_user_name' => $this->getReUserName(), |
| 40 | + 'amount' => $this->getAmount(), |
| 41 | + 'desc' => $this->getDesc(), |
| 42 | + 'spbill_create_ip' => $this->getSpbillCreateIp(), |
| 43 | + 'nonce_str' => md5(uniqid()), |
| 44 | + ); |
| 45 | + |
| 46 | + $data = array_filter($data); |
| 47 | + |
| 48 | + $data['sign'] = Helper::sign($data, $this->getApiKey()); |
| 49 | + |
| 50 | + return $data; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * @return mixed |
| 56 | + */ |
| 57 | + public function getDeviceInfo() |
| 58 | + { |
| 59 | + return $this->getParameter('device_Info'); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * @param mixed $deviceInfo |
| 65 | + */ |
| 66 | + public function setDeviceInfo($deviceInfo) |
| 67 | + { |
| 68 | + $this->setParameter('device_Info', $deviceInfo); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * @return mixed |
| 74 | + */ |
| 75 | + public function getPartnerTradeNo() |
| 76 | + { |
| 77 | + return $this->getParameter('partner_trade_no'); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + /** |
| 82 | + * @param mixed $partnerTradeNo |
| 83 | + */ |
| 84 | + public function setPartnerTradeNo($partnerTradeNo) |
| 85 | + { |
| 86 | + $this->setParameter('partner_trade_no', $partnerTradeNo); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * @return mixed |
| 92 | + */ |
| 93 | + public function getOpenId() |
| 94 | + { |
| 95 | + return $this->getParameter('open_id'); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * @param mixed $openId |
| 101 | + */ |
| 102 | + public function setOpenId($openId) |
| 103 | + { |
| 104 | + $this->setParameter('open_id', $openId); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * @return mixed |
| 110 | + */ |
| 111 | + public function getCheckName() |
| 112 | + { |
| 113 | + return $this->getParameter('check_name'); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * @param mixed $checkName |
| 119 | + */ |
| 120 | + public function setCheckName($checkName) |
| 121 | + { |
| 122 | + $this->setParameter('check_name', $checkName); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * @return mixed |
| 128 | + */ |
| 129 | + public function getReUserName() |
| 130 | + { |
| 131 | + return $this->getParameter('re_user_name'); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + /** |
| 136 | + * @param mixed $reUserNamme |
| 137 | + */ |
| 138 | + public function setReUserName($reUserName) |
| 139 | + { |
| 140 | + $this->setParameter('re_user_name', $reUserName); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + /** |
| 145 | + * @return mixed |
| 146 | + */ |
| 147 | + public function getAmount() |
| 148 | + { |
| 149 | + return $this->getParameter('amount'); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + /** |
| 154 | + * @param mixed $amount |
| 155 | + */ |
| 156 | + public function setAmount($amount) |
| 157 | + { |
| 158 | + $this->setParameter('amount', $amount); |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + /** |
| 163 | + * @return mixed |
| 164 | + */ |
| 165 | + public function getDesc() |
| 166 | + { |
| 167 | + return $this->getParameter('desc'); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + /** |
| 172 | + * @param mixed $desc |
| 173 | + */ |
| 174 | + public function setDesc($desc) |
| 175 | + { |
| 176 | + $this->setParameter('desc', $desc); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + /** |
| 181 | + * @return mixed |
| 182 | + */ |
| 183 | + public function getSpbillCreateIp() |
| 184 | + { |
| 185 | + return $this->getParameter('spbill_create_ip'); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + /** |
| 190 | + * @param mixed $spbill_create_ip |
| 191 | + */ |
| 192 | + public function setSpbillCreateIp($spbillCreateIp) |
| 193 | + { |
| 194 | + $this->setParameter('spbill_create_ip', $spbillCreateIp); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + /** |
| 199 | + * @return mixed |
| 200 | + */ |
| 201 | + public function getCertPath() |
| 202 | + { |
| 203 | + return $this->getParameter('cert_path'); |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + /** |
| 208 | + * @param mixed $certPath |
| 209 | + */ |
| 210 | + public function setCertPath($certPath) |
| 211 | + { |
| 212 | + $this->setParameter('cert_path', $certPath); |
| 213 | + } |
| 214 | + |
| 215 | + |
| 216 | + /** |
| 217 | + * @return mixed |
| 218 | + */ |
| 219 | + public function getKeyPath() |
| 220 | + { |
| 221 | + return $this->getParameter('key_path'); |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + /** |
| 226 | + * @param mixed $keyPath |
| 227 | + */ |
| 228 | + public function setKeyPath($keyPath) |
| 229 | + { |
| 230 | + $this->setParameter('key_path', $keyPath); |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | + /** |
| 235 | + * Send the request with specified data |
| 236 | + * |
| 237 | + * @param mixed $data The data to send |
| 238 | + * |
| 239 | + * @return ResponseInterface |
| 240 | + */ |
| 241 | + public function sendData($data) |
| 242 | + { |
| 243 | + $options = array( |
| 244 | + CURLOPT_SSL_VERIFYPEER => true, |
| 245 | + CURLOPT_SSL_VERIFYHOST => 2, |
| 246 | + CURLOPT_SSLCERTTYPE => 'PEM', |
| 247 | + CURLOPT_SSLKEYTYPE => 'PEM', |
| 248 | + CURLOPT_SSLCERT => $this->getCertPath(), |
| 249 | + CURLOPT_SSLKEY => $this->getKeyPath(), |
| 250 | + ); |
| 251 | + |
| 252 | + $body = Helper::array2xml($data); |
| 253 | + $request = $this->httpClient->post($this->endpoint, null, $data)->setBody($body); |
| 254 | + $request->getCurlOptions()->overwriteWith($options); |
| 255 | + $response = $request->send()->getBody(); |
| 256 | + $responseData = Helper::xml2array($response); |
| 257 | + |
| 258 | + return $this->response = new PromotionTransferResponse($this, $responseData); |
| 259 | + } |
| 260 | +} |
0 commit comments