#!/bin/bash
MONEY=金额,单位是分
OPENID='谁要付款就用他的openid'
ORDER_ID='订单号,你自定即可'
MCHID='你的商户号'
CERT_SN='你的api接口证书序列号'
APPID='你的appid'
GOODS_DESC='商品描述信息'
NOTIFY_URL='你自己做一个用来接收微信支付平台通知你的url'
PRIV_KEY='你的api接口证书私钥路径,名字叫apiclient_key.pem'
METHOD='POST'
URL_PATH='/v3/pay/transactions/jsapi'
TIMESTAMP=`date +%s`
NONCE_STR=`hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random`
POST_DATA="{\"mchid\":\"$MCHID\",\"out_trade_no\":\"$ORDER_ID\",\"appid\":\"$APPID\",\"description\":\"$GOODS_DESC\",\"notify_url\":\"$NOTIFY_URL\",\"amount\":{\"total\":$MONEY,\"currency\":\"CNY\"},\"payer\":{\"openid\":\"$OPENID\"}}"
oneline="$METHOD\n$URL_PATH\n$TIMESTAMP\n$NONCE_STR\n$POST_DATA\n"
#echo $oneline
signstr=`echo -n -e "$oneline" | openssl dgst -sha256 -sign "$PRIV_KEY" | openssl base64 -A`
#echo $signstr
prepay_id=`curl -s 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi' \
-H 'Content-Type: application/json' \
-H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"$MCHID\",serial_no=\"$CERT_SN\",nonce_str=\"$NONCE_STR\",timestamp=\"$TIMESTAMP\",signature=\"$signstr\"" \
--data-raw "$POST_DATA" |jq .prepay_id |tr -d '"'`
#echo $prepay_id
oneline2="$APPID\n$TIMESTAMP\n$NONCE_STR\nprepay_id=$prepay_id\n"
paySign=`echo -n -e "$oneline2" | openssl dgst -sha256 -sign "$PRIV_KEY" | openssl base64 -A`
#echo $paySign
echo "{\"timeStamp\":\"$TIMESTAMP\",\"nonceStr\":\"$NONCE_STR\",\"package\":\"prepay_id=$prepay_id\",\"paySign\":\"$paySign\"}"
结果JSON中的4个字段,发给小程序,让小程序发起支付就可以了。
完整的支付流程,还是少不了要用到拿手的后台语言,比如支付通知回调、退款,比如上传图片等接口。
不过感觉有用,mark一下。