Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add payment scan notify handler #593

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Payment/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,48 @@ public function handleNotify(callable $callback)
return new Response(XML::build($response));
}

/**
* Handle native scan notify.
* https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4
* The callback shall return string of prepay_id or throw an exception.
*
* @param callable $callback
*
* @return Response
*/
public function handleScanNotify(callable $callback)
{
$notify = $this->getNotify();

if (!$notify->isValid()) {
throw new FaultException('Invalid request payloads.', 400);
}

$notify = $notify->getNotify();

try {
$prepayId = call_user_func_array($callback, [$notify->get('product_id'), $notify->get('openid'), $notify]);
$response = [
'return_code' => 'SUCCESS',
'appid' => $this->merchant->app_id,
'mch_id' => $this->merchant->merchant_id,
'nonce_str' => uniqid(),
'prepay_id' => strval($prepayId),
'result_code' => 'SUCCESS',
];
$response['sign'] = generate_sign($response, $this->merchant->key);
} catch (\Exception $e) {
$response = [
'return_code' => 'SUCCESS',
'return_msg' => $e->getCode(),
'result_code' => 'FAIL',
'err_code_des' => $e->getMessage(),
];
}

return new Response(XML::build($response));
}

/**
* [WeixinJSBridge] Generate js config for payment.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Payment/PaymentPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ public function testHandleNotify()
]), $response->getContent());
}

/**
* Test handleNotify().
*/
public function testHandleScanNotify()
{
$merchant = new Merchant(['key' => 'different_sign_key']);
$payment = Mockery::mock(Payment::class.'[getNotify]', [$merchant]);
$request = Request::create('/callback', 'POST', [], [], [], [], '<xml><product_id>88888</product_id><openid>o8GeHuLAsgefS_80exEr1cTqekUs</openid></xml>');
$notify = Mockery::mock(Notify::class.'[isValid]', [$merchant, $request]);
$notify->shouldReceive('isValid')->andReturn(true);
$payment->shouldReceive('getNotify')->andReturn($notify);

$response = $payment->handleScanNotify(function () {
return 'wx201410272009395522657a690389285100';
});

$this->assertInstanceOf(Response::class, $response);
$this->assertRegExp('#<prepay_id><!\[CDATA\[wx201410272009395522657a690389285100\]\]></prepay_id>#', $response->getContent());

$response = $payment->handleScanNotify(function () {
throw new \Exception('Operation failed', 1048);
});

$this->assertEquals(XML::build([
'return_code' => 'SUCCESS',
'return_msg' => 1048,
'result_code' => 'FAIL',
'err_code_des' => 'Operation failed',
]), $response->getContent());
}

/**
* test configForPayment.
*/
Expand Down