banner
阿珏酱

阿珏酱

乘上与平常相反的电车,去看看那未曾见过的风景
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

Alipay Face-to-Face Payment Integration

Tips: When you see this prompt, it indicates that the current article has been migrated from the original emlog blog system. The publication date of the article is quite old, and the formatting and content may not be complete. Please understand.

Alipay Face-to-Face Payment Integration

Date: 2020-4-4 Aju Code Tweaking Views: 2186 Comments: 10

Face-to-face payment, as the name suggests, is payment made in person, helping merchants achieve quick collection in offline consumption scenarios. The face-to-face payment product supports two payment methods: barcode payment and QR code payment.


What we are integrating here is QR code payment.


QR code payment refers to the mode where users open the "Scan" function in the Alipay wallet, scan the QR code displayed by the merchant in a certain cash register scenario, and make the payment. This mode is suitable for offline physical store payments, face-to-face payments, and other scenarios. The business process is shown in the figure below:


image


Due to the simplicity of signing up for face-to-face payment, it allows individual businesses/personal merchants to sign up. Therefore, this method is also widely used for online QR code payments. However, since this method violates Alipay's relevant terms, there is a certain risk. As a technical exchange, let's set this issue aside for now.




As for technical integration, even if you haven't signed up for the face-to-face payment product, you can still develop.


Payment capabilities are directly related to transactions and funds. To facilitate developers in debugging payment capabilities, the open platform has prepared a sandbox environment, including sandbox environment accounts and a sandbox version of the Alipay wallet, so developers can debug in the sandbox environment. Click to learn about

How to Access the Sandbox

and

Access the Sandbox Environment

.


image


So I am developing using the sandbox environment, after all, there is a lot of money in there, feel free to use it.


image


First, download the corresponding SDK for your development language: https://docs.open.alipay.com/194/105201/


QR code payment documentation: https://docs.open.alipay.com/194/106078/






Configure the Key




To ensure the identity and data security of both parties (merchant and Alipay) in the transaction, developers need to configure the keys for both parties before calling the interface to verify the transaction data.



Download



Alipay Open Platform Development Assistant

to generate the keys.




After generating the keys, developers need to configure the keys in the Open Platform Developer Center. Once configured, they can obtain the Alipay public key.



image




Design Integration




Since my design does not require polling (which will be discussed later), it has not been added.


The following is the relevant code from my business.

    The above is the pre-order code for face-to-face payment.
    <br/>
    Regarding this SDK, I really need to complain a bit about it. Which person wrote the demo, and still included a lotusphp framework in the PHP example, a bunch of useless stuff, completely disregarding whether we developers can accept it.
    <br/>
    I also spent some time simplifying the SDK, only taking out the parts I needed, putting them into my own framework, and adding namespaces and autoloading.
    <br/>
    <img src="https://img2023.cnblogs.com/blog/3441070/202405/3441070-20240526174145251-551273636.jpg"/>
    <br/>
    QR code payment has a unique feature ---- asynchronous notification.
    <br/>
    This is exactly the feature most needed for online payments.
    <br>
    When the cash register calls the pre-order request API to generate the QR code for the user, the user scans the QR code with their phone to make the payment. Alipay will send the change information of this order to the merchant system via the asynchronous notification address notify_url that was passed in when the merchant called the pre-order request, using a POST request to notify the payment result as parameters.
    <br/>
    <img src="https://img2023.cnblogs.com/blog/3441070/202405/3441070-20240526174146216-407474224.jpg"/>
    <br/>
    Remember that this asynchronous notification address needs to be set in the application.
    <br/>
    <pre class="prettyprint lang-php linenums">    // Asynchronous callback
public function notify() {
    if (request()->isPost()) {
        require ROOT_PATH.'extend/f2fpay/config/config.php';
        $aop                     = new AopClient;
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
        $flag                    = $aop->rsaCheckV1($_POST, null, "RSA2");
        if ($flag) {
            // Asynchronous SIGN verification successful, can proceed to the next action. For example, verify the order amount and then complete the order, etc.
            // The verification needed is whether the order number and order amount are consistent. If verified successfully, you can operate on the order in the database.
            // TRADE_SUCCESS means that the payment has been received for face-to-face payment. For details, see here https://www.cnblogs.com/tdalcn/p/5956690.html
            if ($_POST['trade_status'] === "TRADE_SUCCESS") {
                // Order processing template
                
                $res = Db::name('order')
                    ->where('pay_id', $_POST['out_trade_no'])
                    ->where('money', $_POST['total_amount'])
                    ->where('status', 0)
                    ->find();
                if($res){
                    Db::name('order')
                        ->where('id',$res['id'])
                        ->update([
                            'status' => 1,
                            'buyer_logon_id' => $_POST['buyer_logon_id'],
                            'pay_time' => $_POST['gmt_payment'],
                            'pay_no' => $_POST['trade_no']
                        ]);
                    Db::name('user')
                        ->where('uid',$res['uid'])
                        ->setInc('integral', floatval($_POST['total_amount']) * 1000);
                }

            }
        } 
        echo 'success'; // The interface must return success, otherwise Alipay will keep sending verification.
    } 
}</pre>
     <strong>
      <span style="font-size:18px;">
       Polling
      </span>
     </strong>
     <br>
      If you need to synchronously redirect after the payment is successful on the page, polling needs to be added. Since face-to-face payment does not have this synchronous notification feature, polling is required, and this method is also mentioned in the face-to-face payment documentation.
      <br/>
      The following code is excerpted from the Bty paid version.
      <br/>
      <pre class="prettyprint lang-php linenums">public function query()
{
    if (input('post.no')) {
        $out_trade_no = input('post.no');

        $queryContentBuilder = new AlipayTradeQueryContentBuilder();
        $queryContentBuilder->setOutTradeNo($out_trade_no);

        $queryResponse = new AlipayTradeService($this->alipay_config);
        $queryResult   = $queryResponse->queryTradeResult($queryContentBuilder);
        $res['status'] = $queryResult->getTradeStatus();
        $res['buyer']  = isset($queryResult->getResponse()->buyer_logon_id) ? $queryResult->getResponse()->buyer_logon_id : '';
        $res['amount'] = $queryResult->getResponse()->buyer_pay_amount;

        if ($res['status'] == 'SUCCESS') {
            $this->paySuccess('', $out_trade_no);
        }

        exit(json_encode($res));
    } else {
        $this->error('Illegal request');
    }
}</pre>
      Since we are not integrating a system similar to a mall, we do not temporarily need complex operations like refunds.
      <br/>
      <img src="https://img2023.cnblogs.com/blog/3441070/202405/3441070-20240526174147080-1313678268.jpg"/>
      <br/>
      The basic integration of face-to-face payment ends here.
      <br/>
      The end, let’s celebrate!
      
      
     </br>
    </br>
   </br>
  </img>
 </br>
</br>


User Comments:

image Self-study College 5 months ago (2020-11-14)
Okay, okay

image Shenzhen Self-study College 6 months ago (2020-10-06)
The blogger is really amazing, secretly taking advantage of the situation...

image Xiao Ai 10 months ago (2020-06-23)
...... Individuals can use it for dozens of days.

image Adult Beauty 11 months ago (2020-06-01)
Face-to-face payment has too small a limit.

image Bei Wan_ 1 year ago (2020-04-08)
How to automatically fill in the QQ input, so cool [#aru_17]

image Aju 1 year ago (2020-04-09)
@Bei Wan_: Isn't this quite common now? Just write it like that.

image Bei Wan_ 1 year ago (2020-04-09)
@Aju: I also got it done, feels really fun, thanks blogger! [#aru_17]

image Aju 1 year ago (2020-04-14)
@Bei Wan_: [#aru_53]

image -- 1 year ago (2020-04-07)
Front row watching [#aru_8]

image Yu Shen Zhuang - Do Not Forget 1 year ago (2020-04-04)
The end, let’s celebrate [#aru_8]

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.