`
lzqustc
  • 浏览: 210314 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

微信内网页开发 - 公众号发红包

阅读更多

接口文档:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3

 

一、开通现金红包权限

二、下载API证书

三、充值

以上步骤请参考:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_3&index=2

 

服务端逻辑:

1、服务端先根据用户在APP中获得的红包金额,创建一条数据库记录(分配红包兑换码,用户信息,金额等等)

2、用户关注公众号,点击公众号菜单:红包兑换,菜单的链接可以是授权登录的链接,点击菜单经过服务器授权登录,获得用户的openid,然后重定向到H5兑换页面(openid作为页面参数)

 

3、用户输入兑换码,H5页面把openid和兑换码信息提交给服务器的Perl CGI脚本,例如https:/xxxx/cgi-bin/pay.pl?do=GetRedPack&redpack_code=xxxxxxx&openid=xxxxxx

 

////脚本处理

if ($cgi->param('do') eq "GetRedPack") {

}

4、CGI脚本根据兑换码,调用服务端接口获取数据库记录,然后组合相关参数调用微信普通红包接口,给用户发红包

 

代码片段:

use CGI;

use warnings;

use JSON;

use utf8;

use Digest::MD5 qw/md5_hex/;

use HTTP::Request;

use HTTP::Headers;

use LWP::UserAgent;

use Encode;

use XML::Simple;

use Data::Dumper;

 

if ($cgi->param('do') eq "GetRedPack") { # ?do=GetRedPack&openid=xxx&redpack_code=xxx

my $openid = $cgi->param('openid');

my $redpack_code=$cgi->param('redpack_code');

 

my $redpack_info = get_redpack_by_code($redpack_code);

 

my $now_t = time();

my $wx_order_info;

$wx_order_info->{mch_id}=$MCH_ID;

$wx_order_info->{nonce_str}=nonce_str();

my $mch_billno=$MCH_ID.formateTime($now_t).$now_t;

if (length($mch_billno) > 28) {

$mch_billno = substr($mch_billno, 0, 28);

}

my $total_rmb = $redpack_info->{money_rmb} + 0;

my $total_num = $redpack_info->{total_num} + 0;

$wx_order_info->{mch_billno} = $mch_billno;

$wx_order_info->{wxappid}=$APPID;

$wx_order_info->{send_name}="Tester";

$wx_order_info->{re_openid}=$openid;

$wx_order_info->{total_amount}=100*$total_rmb;

$wx_order_info->{total_num}=1;

$wx_order_info->{wishing}=$redpack_info->{wishing}; #"happy new year";

$wx_order_info->{client_ip}="1.1.1.1";  #需要填写服务器ip

$wx_order_info->{act_name}=$redpack_info->{act_name};

$wx_order_info->{remark}=$redpack_info->{remark}; #"throw more get more";

 

#红包金额大于200时,请求参数scene_id必传

$wx_order_info->{scene_id}="PRODUCT_3";

#write_log("req_json:".Dumper($wx_order_info));

 

$wx_order_info->{sign} = sign($wx_order_info, "false");

#write_log("req_sign:".Dumper($wx_order_info));

 

my $request_xml = create_xml_data($wx_order_info);

#write_log("req_xml:".$request_xml);

my $header = HTTP::Headers->new( Content_Type => 'text/xml; charset=utf8', );

my $http_request = HTTP::Request->new( POST => "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", $header, $request_xml);

my $ua = LWP::UserAgent->new(

ssl_opts => { 

verify_hostname => 0, 

#SSL_verify_mode => 0x00, 

#SSL_ca_file => '/var/redpack/rootca.pem'

SSL_use_cert => 1,

SSL_cert_file => '/var/redpack/apiclient_cert.pem',

SSL_key_file => '/var/redpack/apiclient_key.pem',

SSL_passwd_cb => sub { $MCH_ID },

},

);  #本接口需要上传证书

my $response = $ua->request($http_request);

my $response_json;

if ($response->message ne "OK" && $response->is_success ne "1") { #出错,或者timeout了

$response_json->{return_code} = "99999";

$response_json->{return_msg} = $response->message;

$response_json->{err_code} = $response->is_success;

} else {

my $decode_rsp = $response->decoded_content();

write_log("\nrsp_xml_utf8:".$decode_rsp);

$response_json = parse_xml_response( $decode_rsp);

}

 

my $ret = update_redpack_by_code($redpack_code);

print_html_notify_rsp("success");

if ($response_json->{return_code} eq "SUCCESS" && $response_json->{result_code} eq "SUCCESS") {

$redirect_url = "http://xxxx/redpack_success.html?redpack_code=$redpack_code";

print $cgi->redirect($redirect_url);  #发送成功,跳转到成功页面

} else {

$redirect_url = "http://xxxx/redpack_failed.html?redpack_code=$redpack_code";

print $cgi->redirect($redirect_url);  #发送失败,跳转到成功页面

}

exit;

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics