/** * 生成用于回复的数据 * * @return array */ public function buildForReply() { if (!method_exists($this, 'toReply')) { throw new Exception(__CLASS__ . '未实现此方法:toReply()'); } $base = array('ToUserName' => $this->to, 'FromUserName' => $this->from, 'CreateTime' => time(), 'MsgType' => $this->getDefaultMessageType()); return XML::build(array_merge($base, $this->toReply())); }
/** * Test isInvalid(). */ public function testIsValid() { $params = ['foo' => 'bar', 'hi' => 'here']; $params['sign'] = \EasyWeChat\Payment\generate_sign($params, 'sign_key'); $request = Request::create('/callback', 'POST', [], [], [], [], XML::build($params)); $notify = new Notify(new Merchant(['key' => 'sign_key']), $request); $this->assertTrue($notify->isValid()); $notify = new Notify(new Merchant(['key' => 'different_sign_key']), $request); $this->assertFalse($notify->isValid()); }
public function getServer($message = '', $queries = null) { $request = Mockery::mock(Request::class . '[get,getContent]'); $request->shouldReceive('get')->andReturnUsing(function ($key) use($queries) { $queries = $queries ?: ['signature' => '5fe39987c51aa87c0da1af7420d4649d77850391', 'timestamp' => '1437865042', 'nonce' => '335941714']; return isset($queries[$key]) ? $queries[$key] : null; }); $message = $message ?: ['ToUserName' => 'gh_9a1a7e312b32', 'FromUserName' => 'oNlnUjq_uJdd52zt3OxFsJHEr_NY', 'CreateTime' => '1437865042', 'MsgType' => 'text', 'Content' => 'foobar', 'MsgId' => '6175583331658476609']; $request->shouldReceive('getContent')->andReturn(XML::build($message)); $server = new Guard($request); return $server; }
/** * Build API instance. * * @return API */ public function getAPI() { $http = Mockery::mock(Http::class); $http->shouldReceive('request')->andReturnUsing(function ($api, $method, $options) { $options['body'] = XML::parse($options['body']); return XML::build(compact('api', 'options')); }); $merchant = new Merchant(['merchant_id' => 'testMerchantId', 'app_id' => 'wxTestAppId', 'key' => 'testKey', 'cert_path' => 'testCertPath', 'key_path' => 'testKeyPath']); $api = Mockery::mock('EasyWeChat\\Payment\\LuckyMoney\\API[getHttp]', [$merchant]); $api->shouldReceive('getHttp')->andReturn($http); return $api; }
/** * Build API instance. * * @return API */ public function getAPI() { $http = Mockery::mock(Http::class); $http->shouldReceive('post')->andReturnUsing(function ($api, $params) { $params = XML::parse($params); return XML::build(compact('api', 'params')); }); $merchant = new Merchant(['fee_type' => 'CNY', 'merchant_id' => 'testMerchantId', 'app_id' => 'wxTestAppId', 'device_info' => 'testDeviceInfo', 'key' => 'testKey']); $api = Mockery::mock('EasyWeChat\\Payment\\API[getHttp]', [$merchant]); $api->shouldReceive('getHttp')->andReturn($http); return $api; }
/** * Test handleNotify(). */ public function testHandleNotify() { $merchant = new Merchant(['key' => 'different_sign_key']); $payment = Mockery::mock(Payment::class . '[getNotify]', [$merchant]); $request = Request::create('/callback', 'POST', [], [], [], [], '<xml><foo>bar</foo></xml>'); $notify = Mockery::mock(Notify::class . '[isValid]', [$merchant, $request]); $notify->shouldReceive('isValid')->andReturn(true); $payment->shouldReceive('getNotify')->andReturn($notify); $response = $payment->handleNotify(function () { return true; }); $this->assertInstanceOf(Response::class, $response); $this->assertEquals(XML::build(['return_code' => 'SUCCESS', 'return_msg' => 'OK']), $response->getContent()); $response = $payment->handleNotify(function () { return 'error_message'; }); $this->assertEquals(XML::build(['return_code' => 'FAIL', 'return_msg' => 'error_message']), $response->getContent()); $response = $payment->handleNotify(function () { return false; }); $this->assertEquals(XML::build(['return_code' => 'FAIL', 'return_msg' => '']), $response->getContent()); }
/** * Encrypt the message and return XML. * * @param string $xml * @param string $nonce * @param int $timestamp * * @return string */ public function encryptMsg($xml, $nonce = null, $timestamp = null) { $encrypt = $this->encrypt($xml, $this->appId); !is_null($nonce) || ($nonce = substr($this->appId, 0, 10)); !is_null($timestamp) || ($timestamp = time()); //生成安全签名 $signature = $this->getSHA1($this->token, $timestamp, $nonce, $encrypt); $response = array('Encrypt' => $encrypt, 'MsgSignature' => $signature, 'TimeStamp' => $timestamp, 'Nonce' => $nonce); //生成响应xml return XML::build($response); }
/** * Build reply XML. * * @param string $to * @param string $from * @param AbstractMessage $message * * @return string */ protected function buildReply($to, $from, $message) { $base = ['ToUserName' => $to, 'FromUserName' => $from, 'CreateTime' => time(), 'MsgType' => is_array($message) ? current($message)->getType() : $message->getType()]; $transformer = new Transformer(); return XML::build(array_merge($base, $transformer->transform($message))); }
/** * Make a API request. * * @param string $api * @param array $params * @param string $method * * @return Collection */ protected function request($api, array $params, $method = 'post') { $params['appid'] = $this->merchant->app_id; $params['mch_id'] = $this->merchant->merchant_id; $params['device_info'] = $this->merchant->device_info; $params['time_stamp'] = time(); $params['nonce_str'] = uniqid(); $params['sign'] = generate_sign($params, $this->merchant->key, 'md5'); return $this->parseResponse($this->getHttp()->{$method}($api, XML::build($params))); }
/** * Handle payment notify. * * @param callable $callback * * @return Response */ public function handleNotify(callable $callback) { $notify = $this->getNotify(); if (!$notify->isValid()) { throw new FaultException('Invalid request XML.', 400); } $notify = $notify->getNotify(); $successful = $notify->get('result_code') === 'SUCCESS'; $handleResult = call_user_func_array($callback, [$notify, $successful]); if (is_bool($handleResult) && $handleResult) { $response = ['return_code' => 'SUCCESS', 'return_msg' => 'OK']; } else { $response = ['return_code' => 'FAIL', 'return_msg' => $handleResult]; } return new Response(XML::build($response)); }
/** * Make a API request. * * @param string $api * @param array $params * @param string $method * @param array $options * @param bool $returnResponse * * @return \EasyWeChat\Support\Collection|\Psr\Http\Message\ResponseInterface */ protected function request($api, array $params, $method = 'post', array $options = [], $returnResponse = false) { $params = array_merge($params, $this->merchant->only(['sub_appid', 'sub_mch_id'])); $params['appid'] = $this->merchant->app_id; $params['mch_id'] = $this->merchant->merchant_id; $params['device_info'] = $this->merchant->device_info; $params['nonce_str'] = uniqid(); $params = array_filter($params); $params['sign'] = generate_sign($params, $this->merchant->key, 'md5'); $options = array_merge(['body' => XML::build($params)], $options); $response = $this->getHttp()->request($api, $method, $options); return $returnResponse ? $response : $this->parseResponse($response); }
/** * Make a API request. * * @param string $api * @param array $params * @param string $method * * @return \EasyWeChat\Support\Collection */ protected function request($api, array $params, $method = 'post') { $params = array_filter($params); $params['mch_id'] = $this->merchant->merchant_id; $params['nonce_str'] = uniqid(); $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5'); $options = ['body' => XML::build($params), 'cert' => $this->merchant->get('cert_path'), 'ssl_key' => $this->merchant->get('key_path')]; return $this->parseResponse($this->getHttp()->request($api, $method, $options)); }
/** * Build reply XML. * * @param string $to * @param string $from * @param MessageInterface $message * * @return string */ protected function buildReply($to, $from, $message) { $base = ['ToUserName' => $to, 'FromUserName' => $from, 'CreateTime' => time(), 'MsgType' => $message->getType()]; return XML::build(array_merge($base, $this->transformer->transform($message))); }
/** * Build message for reply. * * @return array */ public function buildForReply() { $base = array('ToUserName' => $this->to, 'FromUserName' => $this->from, 'CreateTime' => time(), 'MsgType' => $this->getDefaultMessageType()); return XML::build(array_merge($base, $this->toReply())); }