您的位置 首页 php

PHP实现支付宝支付的方法

支付宝付款,开发上比起微信支付要简单很多,今天就以支付宝手机网站支付为例,简单讲一下实现方法:

fb8554458808c52f213988fbaf4263a.png

前期准备,当然就不多说了,当你想开发使用支付宝支付,必然需要在支付宝开放品台注册认证并且创建好应用并且具备手机网站支付功能!不明白可以查看支付宝官方文档(https://docs.open.alipay.com/203/107084/)

一.开发准备

开发之前,需要准备以下信息

1.支付宝应用appid

2.明确接口加密方式(RSA或者RSA2)

3.支付宝公钥

4.应用私钥

二.支付实现

话不多说,直接上代码

/** * 将要参与签名的参数按要求拼接 * @param $data * author 江南极客 * @return string */function signQueryString($data){    // 去空    $data = array_filter($data);    //签名步骤一:按字典序排序参数    ksort($data);    $string_a = http_build_query($data);    $string_a = urldecode($string_a);    return $string_a;} /** * 支付宝RSA签名加密 * @param $data  要参与加密的参数 * @param $private_key  应用私钥 * author 江南极客 * @return array|string */function RSASign($data,$private_key){    //要签名的参数字符串    $query_string = signQueryString($data);    //应用私钥    $private_key = chunk_split($private_key, 64, "\n");    $private_key = "-----BEGIN RSA PRIVATE KEY-----\n$private_key-----END RSA PRIVATE KEY-----\n";    $private_key_id = openssl_pkey_get_private($private_key);    if ($private_key_id === false){        return array(-1,'提供的私钥格式不对');    }    $rsa_sign = false;    if($data['sign_type'] == 'RSA'){        $rsa_sign = openssl_sign($query_string, $sign, $private_key_id,OPENSSL_ALGO_SHA1);    }else if($data['sign_type'] == 'RSA2'){        $rsa_sign = openssl_sign($query_string, $sign, $private_key_id,OPENSSL_ALGO_SHA256);    }    //释放资源    openssl_free_key($private_key_id);    if ($rsa_sign === false){        return array(-1,'签名失败');    }    $signature = base64_encode($sign);    return $signature;} /** * 支付宝支付 * @param array $params  构造好的支付参数 * author 江南极客 * @return array|string */function aliPay(array $params){    $public = [        'app_id' => $params['app_id'],        'method' => $params['method'],        'sign_type' => $params['sign_type'],        'format' => 'JSON',        'charset' => 'utf-8',        'version' => '1.0',        'timestamp' => date('Y-m-d H:i:s'),        'biz_content' => $params['biz_content'],    ];    if(!empty($params['notify_url'])){        $public['notify_url'] = $params['notify_url'];    }    if(!empty($params['return_url'])){        $public['return_url'] = $params['return_url'];    }    $sign = RSASign($public,$params['private_key']);    if(is_array($sign)){        return $sign;    }    $public['sign'] = $sign;    $url = 'https://mapi.alipay.com/gateway.do?'. http_build_query($public,'', '&');    return $url;}

注:这里的支付网关,如果是新接口是(https://openapi.alipay.com/gateway.do)

调用实例:

$biz_content = [    'body' => '测试商品x1',    'subject' => '测试商品',    'out_trade_no' => date('YmdHis').rand(1000,9999),    'product_code' => 'QUICK_WAP_WAY',    'total_amount' => 0.01,];$notify_url = "https://xxxxxxxx/notify.php";//通知回调地址(必须是可以无障碍访问没有登录验证的地址)$params = [    'app_id'  => '2017xxxxxxxxx6554',//appid    'method'  => 'alipay.trade.wap.pay',//接口名称    'sign_type'  => 'RSA2',//签名加密方式    'notify_url'  => $notify_url,    'biz_content'  => json_encode($biz_content),//请求参数];$params['private_key'] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//应用私钥$data = aliPay($params);print_r($data);

三.回调验签

对于手机网站支付产生的交易,支付宝会根据原始支付API中传入的异步通知地址notify_url,通过POST请求的形式将支付结果作为参数通知到商户系统。支付宝异步回调通知POST过来的数据如下

dc314a09cfb344262d6a9bd504408b1.png

在拿到这个数据之后,为了安全防止数据被篡改,需要签证签名,方法如下:

/** * 支付宝验证签名 * @param $return_data  支付宝服务器推送给notify_url的数据 * @param $public_key 支付宝公钥 * author 江南极客 * @return bool|int */function RSAVerify($return_data, $public_key){    if(empty($return_data) || !is_array($return_data)){        return false;    }    //支付宝公钥    $public_key = wordwrap($public_key, 64, "\n", true);    $public_key = "-----BEGIN PUBLIC KEY-----\n$public_key\n-----END PUBLIC KEY-----\n";    $public_key_id = openssl_pkey_get_public($public_key);    if($public_key_id === false){        return false;    }    //除去sign、sign_type两个参数外,凡是通知返回回来的参数皆是待验签的参数。    $sign = $return_data['sign'];    $sign_type = trim($return_data['sign_type'],'"');    unset($return_data['sign'], $return_data['sign_type']);     $query_string = signQueryString($return_data);    $sign = base64_decode($sign);    $rsa_verify = 0;    if($sign_type == 'RSA'){        $rsa_verify = openssl_verify($query_string, $sign, $public_key_id,OPENSSL_ALGO_SHA1);    }else if($sign_type == 'RSA2'){        $rsa_verify = openssl_verify($query_string, $sign, $public_key_id,OPENSSL_ALGO_SHA256);    }    openssl_free_key($public_key_id);    if($rsa_verify == 0 || $rsa_verify == -1){        //Returns 1 if the signature is correct, 0 if it is incorrect, and -1 on error.        return false;    }    return $rsa_verify;}

其余支付宝其他支付方式(扫码支付,PC支付,APP支付等),实现方式大同小异,修改几个参数就OK了!

更多PHP相关知识,请访问PHP教程!

以上就是PHP实现支付宝支付的方法的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:PHP实现支付宝支付的方法

文章地址:https://www.zhihuclub.com/2434.shtml

关于作者: 智云科技

热门文章

评论已关闭

30条评论

  1. She was hired as a post doctoral Fellow because of her recent MD degree but it was clear she had talent, grasping complex scientific problems in estrogen induced apoptosis

  2. Age related changes in the 25 hydroxyvitamin D versus parathyroid hormone relationship suggest a different reason why older adults require more vitamin D

  3. But the weaker spending report was a disappointing sign Patients were grouped as concurrent tamoxifen given during RT followed by continued tamoxifen; 174 patients and sequential RT followed by tamoxifen; 104 patients

  4. Other ABCG genes include ABCG2, a drug resistance gene; ABCG5 and ABCG8, transporters of sterols in the intestine and liver; ABCG3, to date found exclusively in rodents; and the ABCG4 gene that is expressed predominantly in the liver furazolidone bijwerkingen ivermectine tabletten Watsa said Fairfax has no current intention to sell itsBlackBerry shares some 10 percent of the company

  5. 3270 with investors wary of getting too carried away aheadof policy meetings at the European Central Bank and Bank ofEngland later on Thursday

  6. Rökstopp, sjukgymnastbedömning, nutritionsbedömning, utbildning KOL skola, osteoporosprofylax och rehabilitering inklusive arbetsterapi är viktiga åtgärder

  7. To examine whether T47D A18 PKCО± cells are similarly resistant to RAL in vivo, we bilaterally injected T47D A18 PKCО± cells into the mammary fat pads of ovariectomized athymic mice and began treatment with TAM 1 Different spontaneous mutants as well as engineered versions of MMLV displayed the capability to induce other type of hematopoietic malignancies

  8. Hence, the prevailing model is that adult DG lineage amplification occurs through expansion of type 2a cells and limited cell division by stem cells and neuroblasts 2, 20 Overall, costs may rather have been underestimated than overestimated

  9. The proteasome recognizes and binds the tagged protein and subsequently hydrolyzes it into short polypeptides in the 20S core That Men Are Likely To Shed Pounds More Quickly Than Ladies Their Larger Oxygen Intake And Testosterone Levels Enable For Easier Muscle Acquire And Fat Loss

  10. A tumor flare with increased bone pain, swelling, or erythema of superficial lesions or hypercalcemia during the first week or two of therapy should not be confused with disease progression

  11. s Marshall Faulk took a different approach Sunday zyvox cleocin t fiyat Any royalists with 50, 000 burning a hole in their pockets better be quick if they want to splash out on a limited edition gold kilo coin, which has been struck especially for the occasion

  12. topiramate will decrease the level or effect of ondansetron by affecting hepatic intestinal enzyme CYP3A4 metabolism

  13. Monitor Closely 1 phenobarbital decreases levels of propranolol by increasing metabolism When you look at this town it s a hockey town for sure

  14. A recent report found that vitamin C treatment mimics Tet2 restoration to block leukemia progression and vitamin C treatment in leukemia cells enhances their sensitivity to PARP inhibition 34

  15. Uterine sarcomas were reported in 4 women randomized to Tamoxifen Citrate Actavis tamoxifen citrate 1 was FIGO IA, 1 was FIGO IB, 1 was FIGO IIA, and 1 was FIGO IIIC and one patient randomized to placebo FIGO 1A; incidence per 1, 000 women years of 0

  16. In the present study, we investigated combination of tamoxifen TAM with resveratrol RES and observed that the combination is effective on MCF 7 breast cancer cells Monitor Closely 1 celecoxib will increase the level or effect of atomoxetine by affecting hepatic enzyme CYP2D6 metabolism

  17. Venous stases develop increased hydrostatic pressure and this determines the appearance of fluid in the interstitial spaces of subcutaneous tissue com 20 E2 AD 90 20Viagra 20Generico 20Funziona 20Forum 20 20Viagra 20Teva 205343 viagra generico funziona forum The experience and knowledge of this elite team was truly evident in their concise presentation of findings, Coronato said

  18. But that has not relieved pressure on the local fishing industry, which has had to scrap plans to resume test fishing next month because of the recent leaks at the plant

网站地图