Alipay app pays-php backstage signature and verification

Source: Internet
Author: User
signature and verification on the app side can also be completed, considering the security issues, signature and verification is best done in the service side, Alipay official proposal is the case, so the PHP side needs to be signed a good parameter to the app end. PHP server-side SDK Generation app Payment order information sample
$AOP = new Aopclient;
$AOP->gatewayurl = "Https://openapi.alipay.com/gateway.do";
$AOP->appid = "app_id";
$AOP->rsaprivatekey = ' Please fill in the developer's private key to go to the end of the head to return, a line of string ';
$AOP->format = "json";
$AOP->charset = "UTF-8";
$AOP->signtype = "RSA2";
$AOP->alipayrsapublickey = ' Please fill out Alipay public key, one line string ';
Instantiate the specific API corresponding to the request class, class name and interface name corresponding to the current call Interface name: Alipay.trade.app.pay $request = new Alipaytradeapppayrequest (); The SDK has already encapsulated the public parameters, where only incoming business parameters $bizcontent = "{\ body\": \ "I am the test data \". "\ subject\": \ "app payment test \", "." \ "out_trade_no\": \ "20170125test01\", "." \ "Timeout_express\": \ "30m\", "." \ "total_amount\": \ "0.01\", "." \ "Product_code\": \ "Quick_msecurity_pay\".
"}";
$request->setnotifyurl ("asynchronous address accessible to merchant Extranet");
$request->setbizcontent ($bizcontent);
This is different from the ordinary interface call, using the Sdkexecute $response = $aop->sdkexecute ($request); Htmlspecialchars is to prevent the browser from escaping key parameter HTML in order to output to the page, actually printing to the log and the HTTP transmission will not have this problem echo htmlspecialchars ($response);//is orderstring can be directly to the client request, no more processing. 
Here is the demo we use for our project
<?php/*creatorder.php *1. Insert order information in the database *2. Generate order number, return product info/include __dir__. '
    /config/apiconfig.php '; $m = include __dir__. '
    /config/mysqlconfig.php '; Include __dir__. '
    /function/time.php '; Include __dir__. '
    /function/checkuid.php '; Include __dir__. '
    /alipay/aop/aopclient.php '; Include __dir__. '
    /alipay/aop/request/alipaytradeapppayrequest.php ';
 /**///Error_reporting (E_all);
 Sets the category of the error message.
    Ini_set (' Display_error ', ' 1 ');
    foreach ($_post as $key => $value) {$ $key = $value;
    } errorLog (Function_exists (' openssl_sign '));
    if (!checkuid ($uid)) {Opjson ([' Code ' => ' 0011 ', ' msg ' => ' user does not exist ']);
    };
    Create order Number $time = $goodsId. Time ();
    $str = Bin2Hex ($uid); Eval (' $time = '. "
    Dechex ($time); ");

    $orderId = $time. $str;
    $order = $m->order; $insert = [' uid ' => $uid, ' orderId ' => $orderId, ' status ' =>0, ' time ' =>time ()

    ,
    ]; $order-&GT;insert ($insert, M::insert);
    $goods = $m->goods;
    $where [' = '] = [' Goodsid ' => $goodsId,];
    $result = $goods->select ([' Price '])->where ($where); $price = $result->fetch () [' Price '];?>
<?php
    /* Please see creatorder.php/include __dir__ for the OrderID and price used here
    . /creatorder.php ';
    Link commodity table
    $AOP = new Aopclient ();
    $AOP->signtype = "RSA2";
    $request = new Alipaytradeapppayrequest ();
    $bizcontent = ' {' body ': ' 1 ', '
                . ' Subject ":" 1 ", '
                . '" Out_trade_no ":" '. $orderId. ' ", '
                . '" Timeout_express ": 30m", ' 
                . ' " Total_amount ":" '. $price. ' ", '
                . '" Product_code ":" Quick_msecurity_pay "}";
    A merchant extranet can access the asynchronous address
    $request->setnotifyurl ("http://www.***.com/api/alipayurl.php");
    $request->setbizcontent ($bizcontent);
    In this I understand that a signature is generated inside the aopclient, and that the signature is validated at the callback
    $response = $aop->sdkexecute ($request);
    ErrorLog ($response);
    echo $response;
    Opjson ([' Code ' => ' 0000 ', ' info ' => $response]);
? >
PHP Server-side validation asynchronous Notification information parameter sample
$AOP = new Aopclient;
$AOP->alipayrsapublickey = ' Please fill out Alipay public key, one line string ';
$flag = $aop->rsacheckv1 ($_post, NULL, "RSA2");
here is the demo used by our project
<?php
* * Here is a pit, to use is Alipay's public key, do not make a mistake oh. It is not generated by itself, it is paid by the
* *
include __dir__. /alipay/aop/aopclient.php ';
$AOP = new Aopclient;
$AOP->alipayrsapublickey = ' Please fill out Alipay public key, one line string ';
$flag = $aop->rsacheckv1 ($_post, NULL, "RSA2");
ErrorLog ($_post);
echo "Success";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.