php --对接支付宝支付,菜鸟教程(以官方sdk做教程,thinkphp框架为例)

1,目前市面上最为火爆的支付为,wechat以及alipay ,这两个支付想必大家都已经非常了解了。

下面我将对支付宝支付对接有比较详细的讲解

第一步,我们需要我支付权限包含的私钥,公钥,appid等等一系列权限,开发者工具里面查看





2,支付宝官方入口文件 Aopsdk


引入支付宝文件

引入后就可以直接在资金的paymentApi里面使用了,附上代码

vendor('Alipay.AopSdk');
$aop = new \AopClient ();


3,请求支付,获取支付宝请求参数,调起支付。

支付宝官方给的demo

$aop = new AopClient ();

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

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

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

$aop->apiVersion = '1.0';

$aop->signType = 'RSA2';

$aop->postCharset='GBK';

$aop->format='json';

$request = new AlipayTradeAppPayRequest ();


记得设置回调地址

$request->setNotifyUrl( C('ALIPAY_CONFIG.notify_url'));

//商品参数

$request->setBizContent("{" ."\"timeout_express\":\"90m\"," ."\"total_amount\":\"9.00\"," ."\"seller_id\":\"2088102147948060\"," ."\"product_code\":\"QUICK_MSECURITY_PAY\"," ."\"body\":\"Iphone6 16G\"," ."\"subject\":\"大乐透\"," ."\"out_trade_no\":\"70501111111S001111119\"," ."\"time_expire\":\"2016-12-31 10:05\"," ."\"goods_type\":\"0\"," ."\"promo_params\":\"{\\\"storeIdType\\\":\\\"1\\\"}\"," ."\"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," ."\"royalty_info\":{" ."\"royalty_type\":\"ROYALTY\"," ." \"royalty_detail_infos\":[{" ." \"serial_no\":1," ."\"trans_in_type\":\"userId\"," ."\"batch_no\":\"123\"," ."\"out_relation_id\":\"20131124001\"," ."\"trans_out_type\":\"userId\"," ."\"trans_out\":\"2088101126765726\"," ."\"trans_in\":\"2088101126708402\"," ."\"amount\":0.1," ."\"desc\":\"分账测试1\"," ."\"amount_percentage\":\"100\"" ." }]" ." }," ."\"extend_params\":{" ."\"sys_service_provider_id\":\"2088511833207846\"," ."\"hb_fq_num\":\"3\"," ."\"hb_fq_seller_percent\":\"100\"," ."\"industry_reflux_info\":\"{\\\\\\\"scene_code\\\\\\\":\\\\\\\"metro_tradeorder\\\\\\\",\\\\\\\"channel\\\\\\\":\\\\\\\"xxxx\\\\\\\",\\\\\\\"scene_data\\\\\\\":{\\\\\\\"asset_name\\\\\\\":\\\\\\\"ALIPAY\\\\\\\"}}\"," ."\"card_type\":\"S0JP0000\"" ." }," ."\"sub_merchant\":{" ."\"merchant_id\":\"19023454\"," ."\"merchant_type\":\"alipay: 支付宝分配的间连商户编号, merchant: 商户端的间连商户编号\"" ." }," ."\"enable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," ."\"store_id\":\"NJ_001\"," ."\"specified_channel\":\"pcredit\"," ."\"disable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," ."\"settle_info\":{" ." \"settle_detail_infos\":[{" ." \"trans_in_type\":\"cardSerialNo\"," ."\"trans_in\":\"A0001\"," ."\"summary_dimension\":\"A0001\"," ."\"amount\":0.1" ." }]" ." }," ."\"invoice_info\":{" ."\"key_info\":{" ."\"is_support_invoice\":true," ."\"invoice_merchant_name\":\"ABC|003\"," ."\"tax_num\":\"1464888883494\"" ." }," ."\"details\":\"[{\\\"code\\\":\\\"100294400\\\",\\\"name\\\":\\\"服饰\\\",\\\"num\\\":\\\"2\\\",\\\"sumPrice\\\":\\\"200.00\\\",\\\"taxRate\\\":\\\"6%\\\"}]\"" ." }," ."\"ext_user_info\":{" ."\"name\":\"李明\"," ."\"mobile\":\"16587658765\"," ."\"cert_type\":\"IDENTITY_CARD\"," ."\"cert_no\":\"362334768769238881\"," ."\"min_age\":\"18\"," ."\"fix_buyer\":\"F\"," ."\"need_check_info\":\"F\"" ." }," ."\"business_params\":\"{\\\"data\\\":\\\"123\\\"}\"" ." }");

//附上对商品参数的封装与解释

$payData = [
    'body' => $order['body'],
    'subject' => $order['detail'],
    'order_no' => $order['out_trade_no'],
    'timeout_express' => time() + 600,// 表示必须 600s 内付款
    'amount' => $order['total_fee'],// 单位为元 ,最小为0.01
    'return_param' => $order['out_trade_no'],// 一定不要传入汉字,只能是 字母 数字组合
    // 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',// 客户地址
    'goods_type' => $order['trade_type'],//订单类型',
    'store_id' => '',
];

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

返回结果:


这便是生成的支付宝请求参数,给到客户端那边,即可客户端会拿到商品请求数据请求,支付宝,调起支付。

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

$resultCode = $result->$responseNode->code;if(!empty($resultCode)&&$resultCode == 10000){echo "成功";} else {echo "失败";}


3,异步回调,获取结果

根据你给的回调地址,支付会一直请求,直到你返回Success

附上代码:


/**
 *支付宝通知
 * @author fancy
 */
public function alipayNotify()
{
    Log::writeJmqNoticeLog('notify_begin', ErrorCode::ALIPAY_NOTIFY_SUC, __FILE__, __LINE__);
    vendor('Alipay.AopSdk');
    $aop = new \AopClient();
    $aop->alipayrsaPublicKey = C('ALIPAY_CONFIG.alipayrsaPublicKey');
    $aop->rsaCheckV1($_POST, null, "RSA2");
    if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
        $order_sn = I('post.out_trade_no');
        Log::writeJmqNoticeLog('alipay_notify' . $order_sn, ErrorCode::ALIPAY_NOTIFY_SUC, __FILE__, __LINE__);
        $order = OrderLogic::getInstance()->findOrderBySn($order_sn);
        if (empty($order)) {
            $this->failReturn("Order not exist", __FILE__, __LINE__);
        }
        if ($order['order_status'] == OrderConstant::ORDER_UN_PAY) {
            M()->startTrans();
            // 改变订单状态
            OrderLogic::getInstance()->pay($order_sn, OrderConstant::PAY_TYPE_ALIPAY);

            // 获取更新后的订单,并回调处理
            $order = OrderLogic::getInstance()->findOrderBySn($order_sn);
            $resut = PaySuccessLogic::process($order);
            Log::writeJmqNoticeLog(ErrorCode::ALIPAY_NOTIFY_FAIL_MSG, ErrorCode::ALIPAY_NOTIFY_FAIL, __FILE__,
                __LINE__);
            if ($resut) {//成功 消息入队列
                M()->commit();
            } else {//钱已到账 数据库 入库操作回滚
                M()->rollback();
            }
            return true;
        }
        return false;
    } else {
        Log::writeJmqNoticeLog(ErrorCode::ALIPAY_NOTIFY_FAIL_MSG, ErrorCode::ALIPAY_NOTIFY_FAIL, __FILE__,
            __LINE__);
        $this->failReturn("支付失败!", __FILE__, __LINE__);
    }
}


猜你喜欢

转载自blog.csdn.net/Baron0071/article/details/80832526
今日推荐