Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

add wechat pay #108

Merged
merged 7 commits into from
Sep 21, 2016
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ Receive result for `shareToTimeline` and `shareToSession` and arguments would be

For more details, visit [WeChat SDK].

#### `pay`

```
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!');
}
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the indention to 2 spaces please, thank you


## Installation

```sh
Expand Down
34 changes: 34 additions & 0 deletions android/src/main/java/com/theweflex/react/WeChatModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,30 @@ 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, (result) => {
if (result) reject(result);
});
emitter.on('PayReq.Resp', (resp) => {
const result = resp.errCode;
if (result === 0) {
resolve(resp.returnKey);
} else {
reject(result);
}
});
});
}
24 changes: 23 additions & 1 deletion ios/RCTWeChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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