From 69cd6314d4246ee5a38bb85a4abf09ff145bb82c Mon Sep 17 00:00:00 2001 From: SimMan Date: Sat, 17 Sep 2016 19:21:30 +0800 Subject: [PATCH 1/7] Update index.js add wechat payment --- index.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/index.js b/index.js index 518b9ae3..9c3134d2 100644 --- a/index.js +++ b/index.js @@ -183,3 +183,28 @@ export function shareToTimeline(data) { export function shareToSession(data) { return nativeShareToSession(data); } + +/** + * wechat pay + * @param {Object} data + * @param {String} data.partnerId + * @param {String} data.prepayId + * @param {String} data.nonceStr + * @param {String} data.timeStamp + * @param {String} data.package + * @param {String} data.sign + * @returns {Promise} + */ +export function pay(data) { + return new Promise((resolve, reject) => { + WeChat.pay(data,() => {}); + emitter.on('PayReq.Resp', (resp) => { + const result = resp.errCode; + if (result === 0) { + resolve(resp.returnKey); + } else { + reject(result); + } + }); + }); +} From a78dbc268d4d13a5914429b0a89fe430c4ad089a Mon Sep 17 00:00:00 2001 From: SimMan Date: Sat, 17 Sep 2016 19:23:36 +0800 Subject: [PATCH 2/7] Update RCTWeChat.m add wechat payment --- ios/RCTWeChat.m | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ios/RCTWeChat.m b/ios/RCTWeChat.m index 76a4499d..794f55fa 100644 --- a/ios/RCTWeChat.m +++ b/ios/RCTWeChat.m @@ -151,6 +151,20 @@ - (dispatch_queue_t)methodQueue [self shareToWeixinWithData:data scene:WXSceneSession callback:callback]; } +RCT_EXPORT_METHOD(pay:(NSDictionary *)data + :(RCTResponseSenderBlock)callback) +{ + PayReq* req = [PayReq new]; + req.partnerId = data[@"partnerId"]; + req.prepayId = data[@"prepayId"]; + req.nonceStr = data[@"nonceStr"]; + req.timeStamp = [data[@"timeStamp"] unsignedIntValue]; + req.package = data[@"package"]; + req.sign = data[@"sign"]; + BOOL success = [WXApi sendReq:req]; + callback(@[success ? [NSNull null] : INVOKE_FAILED]); +} + - (void)shareToWeixinWithData:(NSDictionary *)aData thumbImage:(UIImage *)aThumbImage scene:(int)aScene @@ -360,7 +374,15 @@ -(void) onResp:(BaseResp*)resp else { [self.bridge.eventDispatcher sendDeviceEventWithName:RCTWXEventName body:body]; } - } + } else if ([resp isKindOfClass:[PayResp class]]) { + PayResp *r = (PayResp *)resp; + NSMutableDictionary *body = @{@"errCode":@(r.errCode)}.mutableCopy; + body[@"errStr"] = r.errStr; + body[@"type"] = @(r.type); + body[@"returnKey"] =r.returnKey; + body[@"type"] = @"PayReq.Resp"; + [self.bridge.eventDispatcher sendDeviceEventWithName:RCTWXEventName body:body]; + } } @end From 989223e3c672fb0e5b9a454d285c68f7bdb14e96 Mon Sep 17 00:00:00 2001 From: SimMan Date: Sat, 17 Sep 2016 19:26:30 +0800 Subject: [PATCH 3/7] Update WeChatModule.java add wechat payment --- .../com/theweflex/react/WeChatModule.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/android/src/main/java/com/theweflex/react/WeChatModule.java b/android/src/main/java/com/theweflex/react/WeChatModule.java index a1729824..3847b02e 100644 --- a/android/src/main/java/com/theweflex/react/WeChatModule.java +++ b/android/src/main/java/com/theweflex/react/WeChatModule.java @@ -37,6 +37,8 @@ import com.tencent.mm.sdk.modelmsg.WXTextObject; import com.tencent.mm.sdk.modelmsg.WXVideoObject; import com.tencent.mm.sdk.modelmsg.WXWebpageObject; +import com.tencent.mm.sdk.modelpay.PayReq; +import com.tencent.mm.sdk.modelpay.PayResp; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.IWXAPIEventHandler; import com.tencent.mm.sdk.openapi.WXAPIFactory; @@ -161,6 +163,34 @@ public void shareToSession(ReadableMap data, Callback callback) { _share(SendMessageToWX.Req.WXSceneSession, data, callback); } + @ReactMethod + public void pay(ReadableMap data, Callback callback){ + PayReq payReq = new PayReq(); + if (data.hasKey("partnerId")) { + payReq.partnerId = data.getString("partnerId"); + } + if (data.hasKey("prepayId")) { + payReq.prepayId = data.getString("prepayId"); + } + if (data.hasKey("nonceStr")) { + payReq.nonceStr = data.getString("nonceStr"); + } + if (data.hasKey("timeStamp")) { + payReq.timeStamp = data.getString("timeStamp"); + } + if (data.hasKey("sign")) { + payReq.sign = data.getString("sign"); + } + if (data.hasKey("package")) { + payReq.packageValue = data.getString("package"); + } + if (data.hasKey("extData")) { + payReq.extData = data.getString("extData"); + } + payReq.appId = appId; + callback.invoke(api.sendReq(payReq) ? null : INVOKE_FAILED); + } + private void _share(final int scene, final ReadableMap data, final Callback callback) { Uri uri = null; if (data.hasKey("thumbImage")) { @@ -441,6 +471,10 @@ public void onResp(BaseResp baseResp) { } else if (baseResp instanceof SendMessageToWX.Resp) { SendMessageToWX.Resp resp = (SendMessageToWX.Resp) (baseResp); map.putString("type", "SendMessageToWX.Resp"); + } else if (baseResp instanceof PayResp) { + PayResp resp = (PayResp) (baseResp); + map.putString("type", "PayReq.Resp"); + map.putString("returnkey", resp.returnKey); } this.getReactApplicationContext() From 10b1863ce5c536206d81fb1f26ec8915142d3a1e Mon Sep 17 00:00:00 2001 From: SimMan Date: Sat, 17 Sep 2016 19:32:53 +0800 Subject: [PATCH 4/7] Update README.md add wechat payment --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index 2a0c5662..af38a8e1 100644 --- a/README.md +++ b/README.md @@ -404,6 +404,28 @@ Receive result for `shareToTimeline` and `shareToSession` and arguments would be For more details, visit [WeChat SDK]. +#### `pay` + +``` +async useWeChat() { + try { + let result = await WeChat.pay( + { + partnerId: '', // 商家向财付通申请的商家id + prepayId: '', // 预支付订单 + nonceStr: '', // 随机串,防重发 + timeStamp: '', // 时间戳,防重发 + package: '', // 商家根据财付通文档填写的数据和签名 + sign: '' // 商家根据微信开放平台文档对数据做的签名 + } + ); + console.log('Pay for success!'); + } catch (error) { + console.log('Pay for failure!'); + } + } +``` + ## Installation ```sh From 213ba2010a2c12df944b04bc4a4f6dd16e2058bb Mon Sep 17 00:00:00 2001 From: SimMan Date: Sat, 17 Sep 2016 21:38:11 +0800 Subject: [PATCH 5/7] Update README.md Remove the wrapping function. indention to 2 spaces --- README.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index af38a8e1..8100eb25 100644 --- a/README.md +++ b/README.md @@ -407,23 +407,21 @@ For more details, visit [WeChat SDK]. #### `pay` ``` -async useWeChat() { - try { - let result = await WeChat.pay( - { - partnerId: '', // 商家向财付通申请的商家id - prepayId: '', // 预支付订单 - nonceStr: '', // 随机串,防重发 - timeStamp: '', // 时间戳,防重发 - package: '', // 商家根据财付通文档填写的数据和签名 - sign: '' // 商家根据微信开放平台文档对数据做的签名 - } - ); - console.log('Pay for success!'); - } catch (error) { - console.log('Pay for failure!'); +try { + let result = await WeChat.pay( + { + partnerId: '', // 商家向财付通申请的商家id + prepayId: '', // 预支付订单 + nonceStr: '', // 随机串,防重发 + timeStamp: '', // 时间戳,防重发 + package: '', // 商家根据财付通文档填写的数据和签名 + sign: '' // 商家根据微信开放平台文档对数据做的签名 } - } + ); + console.log('Pay for success!'); +} catch (error) { + console.log('Pay for failure!'); +} ``` ## Installation From 9137167c6b8be5c2b386d63a7d9fda2ac07dcff5 Mon Sep 17 00:00:00 2001 From: SimMan Date: Sun, 18 Sep 2016 04:55:32 +0800 Subject: [PATCH 6/7] Update index.js callback throws --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9c3134d2..fc73d048 100644 --- a/index.js +++ b/index.js @@ -197,7 +197,9 @@ export function shareToSession(data) { */ export function pay(data) { return new Promise((resolve, reject) => { - WeChat.pay(data,() => {}); + WeChat.pay(data, (result) => { + if (result) reject(result); + }); emitter.on('PayReq.Resp', (resp) => { const result = resp.errCode; if (result === 0) { From ec55c72ebbd6369471d0f9d909a69adee9ffa848 Mon Sep 17 00:00:00 2001 From: SimMan Date: Sun, 18 Sep 2016 04:57:51 +0800 Subject: [PATCH 7/7] Update WeChatModule.java returnkey to returnKey --- android/src/main/java/com/theweflex/react/WeChatModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/theweflex/react/WeChatModule.java b/android/src/main/java/com/theweflex/react/WeChatModule.java index 3847b02e..fb6a64d3 100644 --- a/android/src/main/java/com/theweflex/react/WeChatModule.java +++ b/android/src/main/java/com/theweflex/react/WeChatModule.java @@ -474,7 +474,7 @@ public void onResp(BaseResp baseResp) { } else if (baseResp instanceof PayResp) { PayResp resp = (PayResp) (baseResp); map.putString("type", "PayReq.Resp"); - map.putString("returnkey", resp.returnKey); + map.putString("returnKey", resp.returnKey); } this.getReactApplicationContext()