Skip to content

Commit

Permalink
add NotifyRequest Response
Browse files Browse the repository at this point in the history
  • Loading branch information
thenbsp committed Oct 27, 2015
1 parent 9156f05 commit 02c50e1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
57 changes: 56 additions & 1 deletion src/Payment/NotifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
namespace Thenbsp\Wechat\Payment;

use Thenbsp\Wechat\Util\Request;
use Thenbsp\Wechat\Util\Response;
use Thenbsp\Wechat\Util\Serialize;
use Thenbsp\Wechat\Util\Option;
use Thenbsp\Wechat\Util\OptionValidator;

class NotifyRequest extends Option
{
/**
* 状态(成功)
*/
const SUCCESS = 'SUCCESS';

/**
* 状态(失败)
*/
const FAILURE = 'FAIL';

/**
* 请求对象
*/
Expand Down Expand Up @@ -88,4 +99,48 @@ public function isValid()

return true;
}
}

/**
* 成功响应
*/
public static function success($errorMessage = null)
{
$response = array('return_code' => self::SUCCESS);

if( !is_null($errorMessage) ) {
$response['return_msg'] = $errorMessage;
}

self::_finalResponse($response);
}

/**
* 失败响应
*/
public static function fail($errorMessage = null)
{
$response = array('return_code' => self::FAILURE);

if( !is_null($errorMessage) ) {
$response['return_msg'] = $errorMessage;
}

self::_finalResponse($response);
}

/**
* 最终输出
*/
private static function _finalResponse(array $arrayResponse)
{
$headers = array(
'Content-Type' => 'application/xml'
);

$response = new Response();
$response->setHeaders($headers);
$response->setContent(Serialize::encode($arrayResponse, 'xml'));
$response->send();
exit;
}
}
2 changes: 1 addition & 1 deletion src/Payment/QrcodeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ public function isValid()

return true;
}
}
}
21 changes: 14 additions & 7 deletions src/Payment/QrcodeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Thenbsp\Wechat\Wechat;
use Thenbsp\Wechat\Payment\Unifiedorder;
use Thenbsp\Wechat\Util\Response;
use Thenbsp\Wechat\Util\Serialize;

class QrcodeResponse extends Unifiedorder
Expand Down Expand Up @@ -50,30 +51,36 @@ public function send()

$options['sign'] = $signature;

self::_finalOutput($options);
self::_finalResponse($options);
}

/**
*
* 失败响应
*/
public static function fail($errorMessage)
public static function fail($errorMessage = null)
{
$response = array('return_code' => self::FAILURE);

if( !is_null($errorMessage) ) {
$response['return_msg'] = $errorMessage;
}

self::_finalOutput($response);
self::_finalResponse($response);
}

/**
* 最终输出
*/
private static function _finalOutput(array $arrayResponse)
private static function _finalResponse(array $arrayResponse)
{
header("Content-Type: application/xml; charset=utf-8");
echo Serialize::encode($arrayResponse, 'xml');
$headers = array(
'Content-Type' => 'application/xml'
);

$response = new Response();
$response->setHeaders($headers);
$response->setContent(Serialize::encode($arrayResponse, 'xml'));
$response->send();
exit;
}
}
2 changes: 1 addition & 1 deletion src/Payment/QrcodeTemporary.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public function getPayurl()

return urlencode($response['code_url']);
}
}
}

0 comments on commit 02c50e1

Please sign in to comment.