diff --git a/src/BaseAbstractGateway.php b/src/BaseAbstractGateway.php index 96a0be3..ab5addc 100644 --- a/src/BaseAbstractGateway.php +++ b/src/BaseAbstractGateway.php @@ -6,6 +6,23 @@ abstract class BaseAbstractGateway extends AbstractGateway { + public function getDefaultParameters() + { + return array( + 'environment' => 'production' + ); + } + + public function getEnvironment() + { + return $this->getParameter('environment'); + } + + public function setEnvironment($environment) + { + $this->setParameter('environment', $environment); + } + public function setTradeType($tradeType) { $this->setParameter('trade_type', $tradeType); diff --git a/src/Message/BaseAbstractRequest.php b/src/Message/BaseAbstractRequest.php index 30959db..eec29c1 100644 --- a/src/Message/BaseAbstractRequest.php +++ b/src/Message/BaseAbstractRequest.php @@ -10,6 +10,15 @@ */ abstract class BaseAbstractRequest extends AbstractRequest { + public function getEnvironment() + { + return $this->getParameter('environment'); + } + + public function setEnvironment($envinronment) + { + return $this->setParameter('environment', $envinronment); + } /** * @return mixed diff --git a/src/Message/BaseAbstractResponse.php b/src/Message/BaseAbstractResponse.php index 4b25f45..2df897e 100644 --- a/src/Message/BaseAbstractResponse.php +++ b/src/Message/BaseAbstractResponse.php @@ -6,11 +6,11 @@ /** * Class BaseAbstractResponse + * * @package Omnipay\WechatPay\Message */ abstract class BaseAbstractResponse extends AbstractResponse { - /** * Is the response successful? * @@ -20,6 +20,26 @@ public function isSuccessful() { $data = $this->getData(); - return isset($data['result_code']) && $data['result_code'] == 'SUCCESS'; + return isset($data['result_code']) && $data['result_code'] === 'SUCCESS'; + } + + /** + * Gateway Reference + * + * @return null|string A reference provided by the gateway to represent this transaction + */ + public function getTransactionReference() + { + return $this->getData()['transaction_id'] ?? null; + } + + /** + * Get the transaction ID as generated by the merchant website. + * + * @return string + */ + public function getTransactionId() + { + return $this->getData()['out_trade_no'] ?? null; } } diff --git a/src/Message/CloseOrderRequest.php b/src/Message/CloseOrderRequest.php index 3a6f46f..8c5795c 100644 --- a/src/Message/CloseOrderRequest.php +++ b/src/Message/CloseOrderRequest.php @@ -17,6 +17,19 @@ class CloseOrderRequest extends BaseAbstractRequest { protected $endpoint = 'https://api.mch.weixin.qq.com/pay/closeorder'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/closeorder'; + + /** + * @return string + */ + public function getEndpoint() + { + if ($this->getEnvironment() == 'production') { + return $this->endpoint; + } + + return $this->sandboxEndpoint; + } /** * Get the raw data array for this message. The format of this varies from gateway to @@ -73,7 +86,7 @@ public function setOutTradeNo($outTradeNo) public function sendData($data) { $body = Helper::array2xml($data); - $response = $this->httpClient->request('POST', $this->endpoint, [], $body)->getBody(); + $response = $this->httpClient->request('POST', $this->getEndpoint(), [], $body)->getBody(); $payload = Helper::xml2array($response); return $this->response = new CloseOrderResponse($this, $payload); diff --git a/src/Message/CreateMicroOrderRequest.php b/src/Message/CreateMicroOrderRequest.php index 752a563..301367b 100644 --- a/src/Message/CreateMicroOrderRequest.php +++ b/src/Message/CreateMicroOrderRequest.php @@ -14,8 +14,9 @@ */ class CreateMicroOrderRequest extends CreateOrderRequest { - protected $endpoint = 'https://api.mch.weixin.qq.com/pay/micropay'; + protected $endpoint = 'https://api.mch.weixin.qq.com/pay/micropay'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay'; /** * Get the raw data array for this message. The format of this varies from gateway to @@ -77,7 +78,7 @@ public function setAuthCode($authCode) */ public function sendData($data) { - $request = $this->httpClient->request('POST', $this->endpoint, [], Helper::array2xml($data)); + $request = $this->httpClient->request('POST', $this->getEndpoint(), [], Helper::array2xml($data)); $response = $request->getBody(); $responseData = Helper::xml2array($response); diff --git a/src/Message/CreateOrderRequest.php b/src/Message/CreateOrderRequest.php index c9e79af..c1282d1 100644 --- a/src/Message/CreateOrderRequest.php +++ b/src/Message/CreateOrderRequest.php @@ -14,8 +14,21 @@ */ class CreateOrderRequest extends BaseAbstractRequest { - protected $endpoint = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; + protected $endpoint = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder'; + + /** + * @return string + */ + public function getEndpoint() + { + if ($this->getEnvironment() == 'production') { + return $this->endpoint; + } + + return $this->sandboxEndpoint; + } /** * Get the raw data array for this message. The format of this varies from gateway to @@ -42,7 +55,7 @@ public function getData() $this->validate('open_id'); } - $data = array( + $data = [ 'appid' => $this->getAppId(),//* 'mch_id' => $this->getMchId(), 'sub_appid' => $this->getSubAppId(), @@ -63,7 +76,7 @@ public function getData() 'limit_pay' => $this->getLimitPay(), 'openid' => $this->getOpenId(),//*(trade_type=JSAPI) 'nonce_str' => md5(uniqid()),//* - ); + ]; $data = array_filter($data); @@ -72,7 +85,6 @@ public function getData() return $data; } - /** * @return mixed */ @@ -81,7 +93,6 @@ public function getTradeType() return $this->getParameter('trade_type'); } - /** * @return mixed */ @@ -90,7 +101,6 @@ public function getDeviceInfo() return $this->getParameter('device_Info'); } - /** * @return mixed */ @@ -99,7 +109,6 @@ public function getBody() return $this->getParameter('body'); } - /** * @return mixed */ @@ -108,7 +117,6 @@ public function getDetail() return $this->getParameter('detail'); } - /** * @return mixed */ @@ -117,7 +125,6 @@ public function getAttach() return $this->getParameter('attach'); } - /** * @return mixed */ @@ -126,7 +133,6 @@ public function getOutTradeNo() return $this->getParameter('out_trade_no'); } - /** * @return mixed */ @@ -135,7 +141,6 @@ public function getFeeType() return $this->getParameter('fee_type'); } - /** * @return mixed */ @@ -144,7 +149,6 @@ public function getTotalFee() return $this->getParameter('total_fee'); } - /** * @return mixed */ @@ -153,7 +157,6 @@ public function getSpbillCreateIp() return $this->getParameter('spbill_create_ip'); } - /** * @return mixed */ @@ -162,7 +165,6 @@ public function getTimeStart() return $this->getParameter('time_start'); } - /** * @return mixed */ @@ -171,7 +173,6 @@ public function getTimeExpire() return $this->getParameter('time_expire'); } - /** * @return mixed */ @@ -180,7 +181,6 @@ public function getGoodsTag() return $this->getParameter('goods_tag'); } - /** * @return mixed */ @@ -189,7 +189,6 @@ public function getNotifyUrl() return $this->getParameter('notify_url'); } - /** * @return mixed */ @@ -198,7 +197,6 @@ public function getLimitPay() return $this->getParameter('limit_pay'); } - /** * @return mixed */ @@ -207,7 +205,6 @@ public function getOpenId() return $this->getParameter('open_id'); } - /** * @param mixed $deviceInfo */ @@ -216,7 +213,6 @@ public function setDeviceInfo($deviceInfo) $this->setParameter('device_Info', $deviceInfo); } - /** * @param mixed $body */ @@ -225,7 +221,6 @@ public function setBody($body) $this->setParameter('body', $body); } - /** * @param mixed $detail */ @@ -234,7 +229,6 @@ public function setDetail($detail) $this->setParameter('detail', $detail); } - /** * @param mixed $attach */ @@ -243,7 +237,6 @@ public function setAttach($attach) $this->setParameter('attach', $attach); } - /** * @param mixed $outTradeNo */ @@ -252,7 +245,6 @@ public function setOutTradeNo($outTradeNo) $this->setParameter('out_trade_no', $outTradeNo); } - /** * @param mixed $feeType */ @@ -261,7 +253,6 @@ public function setFeeType($feeType) $this->setParameter('fee_type', $feeType); } - /** * @param mixed $totalFee */ @@ -270,7 +261,6 @@ public function setTotalFee($totalFee) $this->setParameter('total_fee', $totalFee); } - /** * @param mixed $spbillCreateIp */ @@ -279,7 +269,6 @@ public function setSpbillCreateIp($spbillCreateIp) $this->setParameter('spbill_create_ip', $spbillCreateIp); } - /** * @param mixed $timeStart */ @@ -288,7 +277,6 @@ public function setTimeStart($timeStart) $this->setParameter('time_start', $timeStart); } - /** * @param mixed $timeExpire */ @@ -297,7 +285,6 @@ public function setTimeExpire($timeExpire) $this->setParameter('time_expire', $timeExpire); } - /** * @param mixed $goodsTag */ @@ -306,13 +293,11 @@ public function setGoodsTag($goodsTag) $this->setParameter('goods_tag', $goodsTag); } - public function setNotifyUrl($notifyUrl) { $this->setParameter('notify_url', $notifyUrl); } - /** * @param mixed $tradeType */ @@ -321,7 +306,6 @@ public function setTradeType($tradeType) $this->setParameter('trade_type', $tradeType); } - /** * @param mixed $limitPay */ @@ -330,7 +314,6 @@ public function setLimitPay($limitPay) $this->setParameter('limit_pay', $limitPay); } - /** * @param mixed $openId */ @@ -343,7 +326,7 @@ public function setOpenId($openId) /** * Send the request with specified data * - * @param mixed $data The data to send + * @param mixed $data The data to send * * @return ResponseInterface * @throws \Psr\Http\Client\Exception\NetworkException @@ -352,7 +335,7 @@ public function setOpenId($openId) public function sendData($data) { $body = Helper::array2xml($data); - $response = $this->httpClient->request('POST', $this->endpoint, [], $body)->getBody(); + $response = $this->httpClient->request('POST', $this->getEndpoint(), [], $body)->getBody(); $payload = Helper::xml2array($response); return $this->response = new CreateOrderResponse($this, $payload); diff --git a/src/Message/QueryOrderRequest.php b/src/Message/QueryOrderRequest.php index c8ccabb..5dbeeb4 100644 --- a/src/Message/QueryOrderRequest.php +++ b/src/Message/QueryOrderRequest.php @@ -17,6 +17,20 @@ class QueryOrderRequest extends BaseAbstractRequest { protected $endpoint = 'https://api.mch.weixin.qq.com/pay/orderquery'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery'; + + + /** + * @return string + */ + public function getEndpoint() + { + if ($this->getEnvironment() == 'production') { + return $this->endpoint; + } + + return $this->sandboxEndpoint; + } /** * Get the raw data array for this message. The format of this varies from gateway to @@ -95,7 +109,7 @@ public function setTransactionId($transactionId) public function sendData($data) { $body = Helper::array2xml($data); - $response = $this->httpClient->request('POST', $this->endpoint, [], $body)->getBody(); + $response = $this->httpClient->request('POST', $this->getEndpoint(), [], $body)->getBody(); $payload = Helper::xml2array($response); return $this->response = new QueryOrderResponse($this, $payload); diff --git a/src/Message/QueryRefundRequest.php b/src/Message/QueryRefundRequest.php index 0935f46..767a6c0 100644 --- a/src/Message/QueryRefundRequest.php +++ b/src/Message/QueryRefundRequest.php @@ -17,6 +17,19 @@ class QueryRefundRequest extends BaseAbstractRequest { protected $endpoint = 'https://api.mch.weixin.qq.com/pay/refundquery'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery'; + + /** + * @return string + */ + public function getEndpoint() + { + if ($this->getEnvironment() == 'production') { + return $this->endpoint; + } + + return $this->sandboxEndpoint; + } /** * Get the raw data array for this message. The format of this varies from gateway to @@ -153,7 +166,7 @@ public function setRefundId($refundId) */ public function sendData($data) { - $request = $this->httpClient->request('POST', $this->endpoint, [], Helper::array2xml($data)); + $request = $this->httpClient->request('POST', $this->getEndpoint(), [], Helper::array2xml($data)); $response = $request->getBody(); $responseData = Helper::xml2array($response); diff --git a/src/Message/RefundOrderRequest.php b/src/Message/RefundOrderRequest.php index 651ef2b..93fbc28 100644 --- a/src/Message/RefundOrderRequest.php +++ b/src/Message/RefundOrderRequest.php @@ -18,6 +18,21 @@ class RefundOrderRequest extends BaseAbstractRequest { protected $endpoint = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refund'; + + + /** + * @return string + */ + public function getEndpoint() + { + if ($this->getEnvironment() == 'production') { + return $this->endpoint; + } + + return $this->sandboxEndpoint; + } + /** * Get the raw data array for this message. The format of this varies from gateway to @@ -268,7 +283,7 @@ public function sendData($data) 'ssl_key' => $this->getKeyPath(), ]; - $result = $client->request('POST', $this->endpoint, $options)->getBody()->getContents(); + $result = $client->request('POST', $this->getEndpoint(), $options)->getBody()->getContents(); $responseData = Helper::xml2array($result); diff --git a/src/Message/SandboxGetSignKeyRequest.php b/src/Message/SandboxGetSignKeyRequest.php new file mode 100644 index 0000000..1de48e5 --- /dev/null +++ b/src/Message/SandboxGetSignKeyRequest.php @@ -0,0 +1,63 @@ + + */ +class SandboxGetSignKeyRequest extends BaseAbstractRequest +{ + protected $sandboxEndpoint = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'; + + /** + * Get the raw data array for this message. The format of this varies from gateway to + * gateway, but will usually be either an associative array, or a SimpleXMLElement. + * + * @return mixed + * @throws InvalidRequestException + */ + public function getData() + { + $this->validate( + 'mch_id' + ); + + $data = array( + 'mch_id' => $this->getMchId(), + 'nonce_str' => md5(uniqid('', true)),//* + ); + + $data = array_filter($data); + + $data['sign'] = Helper::sign($data, $this->getApiKey()); + + return $data; + } + + /** + * Send the request with specified data + * + * @param mixed $data The data to send + * + * @return ResponseInterface + * @throws NetworkException + * @throws RequestException + */ + public function sendData($data) + { + $request = $this->httpClient->request('POST', $this->sandboxEndpoint, [], Helper::array2xml($data)); + $response = $request->getBody(); + $responseData = Helper::xml2array($response); + + return $this->response = new SandboxGetSignKeyResponse($this, $responseData); + } +} diff --git a/src/Message/SandboxGetSignKeyResponse.php b/src/Message/SandboxGetSignKeyResponse.php new file mode 100644 index 0000000..f29dc20 --- /dev/null +++ b/src/Message/SandboxGetSignKeyResponse.php @@ -0,0 +1,46 @@ + + */ +class SandboxGetSignKeyResponse extends BaseAbstractResponse +{ + /** + * @var SandboxGetSignKeyRequest + */ + protected $request; + + /** + * Is the response successful? + * + * @return boolean + */ + public function isSuccessful() + { + return isset($this->getData()['return_code']) && $this->getData()['return_code'] === 'SUCCESS'; + } + + public function getSandboxKey() + { + return isset($this->getData()['sandbox_signkey']) ? $this->getData()['sandbox_signkey'] : null; + } + + public function getReturnMessage() + { + if (isset($this->getData()['return_msg'])) { + return $this->getData()['return_msg']; + } + + + if (isset($this->getData()['retmsg'])) { + return $this->getData()['retmsg']; + } + + return null; + } +} diff --git a/src/NativeGateway.php b/src/NativeGateway.php index d834492..523b3f2 100644 --- a/src/NativeGateway.php +++ b/src/NativeGateway.php @@ -29,4 +29,14 @@ public function shortenUrl($parameters = array()) { return $this->createRequest('\Omnipay\WechatPay\Message\ShortenUrlRequest', $parameters); } + + /** + * @param array $parameters + * + * @return \Omnipay\WechatPay\Message\SandboxGetSignKeyRequest + */ + public function getSandboxSignKey($parameters = array()) + { + return $this->createRequest(\Omnipay\WechatPay\Message\SandboxGetSignKeyRequest::class, $parameters); + } }