From 25fa41d6c9c983421921d446b4f34c859c77dd1a Mon Sep 17 00:00:00 2001 From: ForeverGlory Date: Sun, 6 Mar 2016 22:22:03 +0800 Subject: [PATCH 1/7] add payment default notify_url. $options = [ // ... 'payment' => [ // ... 'notify_url' => 'http://xxx.com/order-notify'; ] ]; $app = new Application($options); $attributes = [ // ... 'notify_url' => 'http://xxx.com/order-notify';, ]; $order = new Order($attributes); if $order haven't notify_url attribute, use $options['payment']['notify_url']. Don't need to be set notify_url in every order. --- src/Payment/API.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Payment/API.php b/src/Payment/API.php index 92a768547..3093e1185 100644 --- a/src/Payment/API.php +++ b/src/Payment/API.php @@ -81,6 +81,8 @@ public function __construct(Merchant $merchant) */ public function pay(Order $order) { + $order['notify_url'] = $order['notify_url']? : $this->merchant->notify_url; + return $this->request(self::API_PAY_ORDER, $order->all()); } @@ -93,6 +95,8 @@ public function pay(Order $order) */ public function prepare(Order $order) { + $order['notify_url'] = $order['notify_url']? : $this->merchant->notify_url; + return $this->request(self::API_PREPARE_ORDER, $order->all()); } From b54599f1b5e0d8f5fe7d8779f13f0da1b43a9ee4 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sun, 6 Mar 2016 22:45:25 +0800 Subject: [PATCH 2/7] Add tests. #325 --- src/Payment/API.php | 4 ++-- tests/Payment/PaymentAPITest.php | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Payment/API.php b/src/Payment/API.php index 3093e1185..e22c009c8 100644 --- a/src/Payment/API.php +++ b/src/Payment/API.php @@ -81,7 +81,7 @@ public function __construct(Merchant $merchant) */ public function pay(Order $order) { - $order['notify_url'] = $order['notify_url']? : $this->merchant->notify_url; + $order->notify_url = $order->get('notify_url', $this->merchant->notify_url); return $this->request(self::API_PAY_ORDER, $order->all()); } @@ -95,7 +95,7 @@ public function pay(Order $order) */ public function prepare(Order $order) { - $order['notify_url'] = $order['notify_url']? : $this->merchant->notify_url; + $order->notify_url = $order->get('notify_url', $this->merchant->notify_url); return $this->request(self::API_PREPARE_ORDER, $order->all()); } diff --git a/tests/Payment/PaymentAPITest.php b/tests/Payment/PaymentAPITest.php index 5841aeca7..219516803 100755 --- a/tests/Payment/PaymentAPITest.php +++ b/tests/Payment/PaymentAPITest.php @@ -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]); @@ -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']); } From 2b5d1e05cc6ea76f0d6d7ea6d23030cb829eb3a9 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sun, 6 Mar 2016 09:50:53 -0500 Subject: [PATCH 3/7] Applied fixes from StyleCI --- src/Payment/API.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Payment/API.php b/src/Payment/API.php index e22c009c8..af42c453c 100644 --- a/src/Payment/API.php +++ b/src/Payment/API.php @@ -82,7 +82,7 @@ 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()); } @@ -96,7 +96,7 @@ 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()); } From 480d9cbe5458f33c5e658a9867f3537e408ca5a8 Mon Sep 17 00:00:00 2001 From: overtrue Date: Tue, 8 Mar 2016 11:24:38 +0800 Subject: [PATCH 4/7] Bugfix for Payment::refund(). #328 --- src/Payment/API.php | 5 ++++- tests/Payment/PaymentAPITest.php | 9 ++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Payment/API.php b/src/Payment/API.php index af42c453c..a0950cfe9 100644 --- a/src/Payment/API.php +++ b/src/Payment/API.php @@ -187,6 +187,7 @@ public function reverseByTranscationId($transcationId) */ public function refund( $orderNo, + $refundNo, $totalFee, $refundFee = null, $opUserId = null, @@ -194,6 +195,7 @@ public function refund( ) { $params = [ $type => $orderNo, + 'out_refund_no' => $refundNo, 'total_fee' => $totalFee, 'refund_fee' => $refundFee ?: $totalFee, 'refund_fee_type' => $this->merchant->fee_type, @@ -215,11 +217,12 @@ public function refund( */ public function refundByTranscationId( $orderNo, + $refundNo, $totalFee, $refundFee = null, $opUserId = null ) { - return $this->refund($orderNo, $totalFee, $refundFee, $opUserId, self::TRANSCATION_ID); + return $this->refund($orderNo, $refundNo, $totalFee, $refundFee, $opUserId, self::TRANSCATION_ID); } /** diff --git a/tests/Payment/PaymentAPITest.php b/tests/Payment/PaymentAPITest.php index 219516803..acff52449 100755 --- a/tests/Payment/PaymentAPITest.php +++ b/tests/Payment/PaymentAPITest.php @@ -143,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']); } From 36a91497ede1ea270000cdb5d4b3653d4c8b3a65 Mon Sep 17 00:00:00 2001 From: overtrue Date: Wed, 9 Mar 2016 11:01:00 +0800 Subject: [PATCH 5/7] SSL bugfix #328 --- src/Payment/API.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Payment/API.php b/src/Payment/API.php index a0950cfe9..de7499c06 100644 --- a/src/Payment/API.php +++ b/src/Payment/API.php @@ -386,7 +386,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)); } /** From f4de97e4d285fbb22e7c1f79251381980afab153 Mon Sep 17 00:00:00 2001 From: overtrue Date: Tue, 8 Mar 2016 22:24:15 -0500 Subject: [PATCH 6/7] Applied fixes from StyleCI --- tests/Payment/PaymentAPITest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Payment/PaymentAPITest.php b/tests/Payment/PaymentAPITest.php index 79cfdc269..4007da705 100755 --- a/tests/Payment/PaymentAPITest.php +++ b/tests/Payment/PaymentAPITest.php @@ -152,7 +152,7 @@ public function testRefund() $this->assertEquals('testMerchantId', $response['params']['op_user_id']); $this->assertEquals('testTradeNo', $response['params']['out_trade_no']); - $response = $api->refund('testTradeNo','testRefundNo', 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']); From a3ea6431fffd19b4fad82eebed139bb3310e96be Mon Sep 17 00:00:00 2001 From: overtrue Date: Wed, 9 Mar 2016 11:29:35 +0800 Subject: [PATCH 7/7] Tests fix. --- tests/Payment/PaymentAPITest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Payment/PaymentAPITest.php b/tests/Payment/PaymentAPITest.php index 79cfdc269..15c0dfc28 100755 --- a/tests/Payment/PaymentAPITest.php +++ b/tests/Payment/PaymentAPITest.php @@ -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')); });