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

Merge Develop #329

Merged
merged 11 commits into from
Mar 9, 2016
17 changes: 15 additions & 2 deletions src/Payment/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function __construct(Merchant $merchant)
*/
public function pay(Order $order)
{
$order->notify_url = $order->get('notify_url', $this->merchant->notify_url);

return $this->request(self::API_PAY_ORDER, $order->all());
}

Expand All @@ -93,6 +95,8 @@ public function pay(Order $order)
*/
public function prepare(Order $order)
{
$order->notify_url = $order->get('notify_url', $this->merchant->notify_url);

return $this->request(self::API_PREPARE_ORDER, $order->all());
}

Expand Down Expand Up @@ -183,13 +187,15 @@ public function reverseByTransactionId($transactionId)
*/
public function refund(
$orderNo,
$refundNo,
$totalFee,
$refundFee = null,
$opUserId = null,
$type = self::OUT_TRADE_NO
) {
$params = [
$type => $orderNo,
'out_refund_no' => $refundNo,
'total_fee' => $totalFee,
'refund_fee' => $refundFee ?: $totalFee,
'refund_fee_type' => $this->merchant->fee_type,
Expand All @@ -211,11 +217,12 @@ public function refund(
*/
public function refundByTransactionId(
$orderNo,
$refundNo,
$totalFee,
$refundFee = null,
$opUserId = null
) {
return $this->refund($orderNo, $totalFee, $refundFee, $opUserId, self::TRANSACTION_ID);
return $this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, self::TRANSCATION_ID);
}

/**
Expand Down Expand Up @@ -378,7 +385,13 @@ protected function request($api, array $params, $method = 'post')
$params['nonce_str'] = uniqid();
$params['sign'] = generate_sign($params, $this->merchant->key, 'md5');

return $this->parseResponse($this->getHttp()->{$method}($api, XML::build($params)));
$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));
}

/**
Expand Down
39 changes: 33 additions & 6 deletions tests/Payment/PaymentAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function getAPI()
{
$http = Mockery::mock(Http::class);

$http->shouldReceive('post')->andReturnUsing(function ($api, $params) {
$params = XML::parse($params);
$http->shouldReceive('request')->andReturnUsing(function ($api, $method, $options) {
$params = XML::parse($options['body']);

return XML::build(compact('api', 'params'));
});
Expand All @@ -38,6 +38,7 @@ public function getAPI()
'app_id' => 'wxTestAppId',
'device_info' => 'testDeviceInfo',
'key' => 'testKey',
'notify_url' => 'merchant_default_notify_url',
]);

$api = Mockery::mock('EasyWeChat\Payment\API[getHttp]', [$merchant]);
Expand All @@ -52,13 +53,36 @@ public function getAPI()
public function testPrepare()
{
$api = $this->getAPI();
$order = Mockery::mock(Order::class);
$_SERVER['SERVER_ADDR'] = '127.0.0.1';

$order = new Order(['foo' => 'bar']);
$order->shouldReceive('all')->andReturn(['foo' => 'bar']);

$response = $api->prepare($order);

$this->assertEquals(API::API_PREPARE_ORDER, $response['api']);
$this->assertEquals('wxTestAppId', $response['params']['appid']);
$this->assertEquals('merchant_default_notify_url', $response['params']['notify_url']);
$this->assertEquals('testMerchantId', $response['params']['mch_id']);
$this->assertEquals('bar', $response['params']['foo']);
}

/**
* Test pay().
*/
public function testPay()
{
$api = $this->getAPI();
$_SERVER['SERVER_ADDR'] = '127.0.0.1';

$order = new Order(['foo' => 'bar']);
$order->shouldReceive('all')->andReturn(['foo' => 'bar']);

$response = $api->pay($order);

$this->assertEquals(API::API_PAY_ORDER, $response['api']);
$this->assertEquals('wxTestAppId', $response['params']['appid']);
$this->assertEquals('merchant_default_notify_url', $response['params']['notify_url']);
$this->assertEquals('testMerchantId', $response['params']['mch_id']);
$this->assertEquals('bar', $response['params']['foo']);
}
Expand Down Expand Up @@ -119,19 +143,22 @@ public function testRefund()
{
$api = $this->getAPI();

$response = $api->refund('testTradeNo', 100);
$response = $api->refund('testTradeNo', 'testRefundNo', 100);
$this->assertEquals(API::API_REFUND, $response['api']);
$this->assertEquals('testRefundNo', $response['params']['out_refund_no']);
$this->assertEquals(100, $response['params']['total_fee']);
$this->assertEquals(100, $response['params']['refund_fee']);
$this->assertEquals('CNY', $response['params']['refund_fee_type']);
$this->assertEquals('testMerchantId', $response['params']['op_user_id']);
$this->assertEquals('testTradeNo', $response['params']['out_trade_no']);

$response = $api->refund('testTradeNo', 100, 50);
$response = $api->refund('testTradeNo', 'testRefundNo', 100, 50);
$this->assertEquals('testRefundNo', $response['params']['out_refund_no']);
$this->assertEquals(100, $response['params']['total_fee']);
$this->assertEquals(50, $response['params']['refund_fee']);

$response = $api->refund('testTradeNo', 100, 50, 'testRefundNo');
$response = $api->refund('testTradeNo', 'testRefundNo', 100, 50);
$this->assertEquals('testRefundNo', $response['params']['out_refund_no']);
$this->assertEquals(100, $response['params']['total_fee']);
$this->assertEquals(50, $response['params']['refund_fee']);
}
Expand Down