php小程序支付获取open_id,thinkcmf,tp5.1接入支付宝sdk,获取user_id,小程序支付

因为版本的问题,不支import()这种引入类方法,就只能用require。。。

支付宝给的sdk不支持composer,只能手动导入

我是在vendor下创建alipay,然后再aop,接着放下载到的sdk

在需要的地方引入

require CMF_ROOT.'/vendor/alipay/Aop/AopClient.php';

require CMF_ROOT.'/vendor/alipay/Aop/request/AlipaySystemOauthTokenRequest.php';

然后使用支付宝给出的示例,但是在5.1版本引入时候需要在new后面加上\

公钥跟私钥完全按照官方给出的软件下载生成就好

$c = new \AopClient;

$c->gatewayUrl = "https://openapi.alipay.com/gateway.do";

$c->appId = "app_id";

$c->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串' ;

$c->format = "json";

$c->charset= "utf-8";

$c->signType= "RSA2";

$c->alipayrsaPublicKey = '请填写支付宝公钥,一行字符串';

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify

$request = new \AlipayOpenPublicTemplateMessageIndustryModifyRequest();

//SDK已经封装掉了公共参数,这里只需要传入业务参数

//此次只是参数展示,未进行字符串转义,实际情况下请转义

$request->setBizContent = "{" .

" \"primary_industry_name\":\"IT科技/IT软件与服务\"," .

" \"primary_industry_code\":\"10001/20102\"," .

" \"secondary_industry_code\":\"10001/20102\"," .

" \"secondary_industry_name\":\"IT科技/IT软件与服务\"" .

" }";

$response= $c->execute($request);

然后是小程序的支付,首先生成订单发起统一下单

//进行支付宝统一下单操作

public function alipay_do_order(){

$param= $this->request->param();

$order_sn='T02002082158045248850232';//自己生成的唯一订单号

$user_id='2088***********';//上一步获取的user_id

$order_money='0.01';

$aop = new \AopClient ();

$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';

$aop->appId = '******';//这个是应用id

$aop->rsaPrivateKey = '*********=';//私钥

$aop->alipayrsaPublicKey='###';//公钥

$aop->apiVersion = '1.0';

$aop->signType = 'RSA2';

$aop->postCharset='utf-8';

$aop->format='json';

$request = new \AlipayTradeCreateRequest ();

$arr_info=array(

'out_trade_no'=>$order_sn,

'total_amount'=>$order_money,

'subject'=>'华为P30',//产品的标题

'buyer_id'=>$user_id,

// 'timeout_express'=>'30m',

);

$arr_info_json= json_encode($arr_info);

$request->setBizContent($arr_info_json);

$result = $aop->execute ( $request);

$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

$resultCode = $result->$responseNode;

$the_result= json_decode( json_encode( $resultCode),true);

if ($the_result['code'] == 1000) {

# code...

session('the_result',$the_result);

}

$this->success('请求成功!', $the_result );

}

成功返回后会获得交易单号跟订单号,在小程序端把交易号发送然后拉起支付界面

my.tradePay({

// 调用统一收单交易创建接口(alipay.trade.create),获得返回字段支付宝交易号trade_no

tradeNO: '201711152100110410533667792',//填在这里

success: (res) => {

my.alert({

content: JSON.stringify(res),

});

},

fail: (res) => {

my.alert({

content: JSON.stringify(res),

});

}

});

然后根据返回的resultCode 进行回调操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值