mirror of
https://github.com/zhigang1992/react-native-wechat.git
synced 2026-04-27 21:07:29 +08:00
wechat: add payment logic (#108)
This commit is contained in:
20
README.md
20
README.md
@@ -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!');
|
||||
}
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
|
||||
@@ -37,6 +37,8 @@ import com.tencent.mm.sdk.modelmsg.WXMusicObject;
|
||||
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 class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
|
||||
_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 class WeChatModule extends ReactContextBaseJavaModule implements IWXAPIEv
|
||||
} 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()
|
||||
|
||||
27
index.js
27
index.js
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -151,6 +151,20 @@ RCT_EXPORT_METHOD(shareToSession:(NSDictionary *)data
|
||||
[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 @@ RCT_EXPORT_METHOD(shareToSession:(NSDictionary *)data
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user