参考:
cordova-plugin-wechat
cordova-plugin-wechat-example的后台PHP文件参考javascript
cordova plugin add cordova-plugin-wechat --variable wechatappid=YOUR_WECHAT_APPID
。declare var Wechat: any; // 此处声明plugin.xml中clobbers对应的值
。Wechat.isInstalled(function (installed) {
alert("Wechat installed: " + (installed ? "Yes" : "No"));
}, function (reason) {
alert("Failed: " + reason);
});
5. 使用Wechat实现ionic2上的微信支付:php
Wechat.share({
message: {
title: "Hi, there",
description: "This is description.",
thumb: "www/img/thumbnail.png",
mediaTagName: "TEST-TAG-001",
messageExt: "这是第三方带的测试字段",
messageAction: "<action>dotalist</action>",
media: "YOUR_MEDIA_OBJECT_HERE"
},
scene: Wechat.Scene.TIMELINE // share to Timeline
}, function () {
alert("Success");
}, function (reason) {
alert("Failed: " + reason);
});
declare var Wechat: any; // 此处声明plugin.xml中clobbers对应的值
export interface WechatPayParam {
partnerid: string;
prepayid: string;
noncestr: string;
timestamp: string;
sign: string;
}
export class WechatPlugin {
public static isInstalled() {
return new Promise((resolve, reject) => {
Wechat.isInstalled(result => {
resolve(result);
}, error => {
reject(error);
});
});
}
public static sendPaymentRequest(params: WechatPayParam) {
return new Promise((resolve, reject) => {
Wechat.sendPaymentRequest(params, result => {
resolve(result);
}, error => {
reject(error);
});
});
}
}
import {WechatPlugin} from 'wechat-plugin';
注:要根据本身的项目路径引入哦。html
var params = {
partnerid: this.payResult.partnerid, // merchant id
prepayid: this.payResult.prepayid, // prepay id
noncestr: this.payResult.noncestr, // nonce
timestamp: this.payResult.timestamp, // timestamp
sign: this.payResult.sign // signed string
};
WechatPlugin.sendPaymentRequest(params).then((result) => { // xxx }), (error) => {
// xxx
});
注:听parnter说他在github上找了个。因为当前木有copy到他的,这里就先不贴。
demo.php
参考:微信支付PHP实现签名java
<?php
// APPID (开户邮件中可查看)
define("APP_ID", "YOUR_APP_ID");
// 商户号 (开户邮件中可查看)
define("MCH_ID", "YOUR_MCH_ID");
// 商户支付密钥 (https://pay.weixin.qq.com/index.php/account/api_cert)
define("APP_KEY", "YOUR_APP_KEY");
// get prepay id
$prepay_id = generatePrepayId(APP_ID, MCH_ID);
// re-sign it
$response = array(
'appid' => APP_ID,
'partnerid' => MCH_ID,
'prepayid' => $prepay_id,
'package' => 'Sign=WXPay',
'noncestr' => generateNonce(),
'timestamp' => time(),
);
$response['sign'] = calculateSign($response, APP_KEY);
// send it to APP
echo json_encode($response);
/** * Generate a nonce string * * @link https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=4_3 */
function generateNonce() {
return md5(uniqid('', true));
}
/** * Get a sign string from array using app key * * @link https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=4_3 */
function calculateSign($arr, $key) {
ksort($arr);
$buff = "";
foreach ($arr as $k => $v) {
if ($k != "sign" && $k != "key" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return strtoupper(md5($buff . "&key=" . $key));
}
/** * Get xml from array */
function getXMLFromArray($arr) {
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= sprintf("<%s>%s</%s>", $key, $val, $key);
} else {
$xml .= sprintf("<%s><![CDATA[%s]]></%s>", $key, $val, $key);
}
}
$xml .= "</xml>";
return $xml;
}
/** * Generate a prepay id * * @link https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_1 */
function generatePrepayId($app_id, $mch_id) {
$params = array(
'appid' => $app_id,
'mch_id' => $mch_id,
'nonce_str' => generateNonce(),
'body' => 'Test product name',
'out_trade_no' => time(),
'total_fee' => 1,
'spbill_create_ip' => '8.8.8.8',
'notify_url' => 'http://localhost',
'trade_type' => 'APP',
);
// add sign
$params['sign'] = calculateSign($params, APP_KEY);
// create xml
$xml = getXMLFromArray($params);
// send request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://api.mch.weixin.qq.com/pay/unifiedorder",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/xml'),
CURLOPT_POSTFIELDS => $xml,
));
$result = curl_exec($ch);
curl_close($ch);
// get the prepay id from response
$xml = simplexml_load_string($result);
return (string)$xml->prepay_id;
}
最最直观的BUG就属:明明服务端返回的params中的参数都正确了可是却一直不能调起微信支付,而且提示“Failed:普通错误”。
注:以致于我瞎搞了好久,试了不少解决方案。最后莫名地“试”成功了。下面说说几种可能致使错误的事咯。
实际上应该是对应微信支付中错误码“-1”。参考:APP端调起支付的参数列表
返回结果的含义:python
名称 | 描述 | 解决方案 |
---|---|---|
0 | 成功 | 展现成功页面 |
-1 | 错误 | 可能的缘由:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其余异常等。 |
-2 | 用户取消 | 无需处理。发生场景:用户不支付了,点击取消,返回APP。 |