Alipay looks at the underlying process and encryption

Source: Internet
Author: User
Tags decrypt openssl openssl rsa
how to integrate Alipay Alipay features are now integrated in many apps using Alipay for a complete payment function, there are roughly the following steps:Apply to Alipay, sign up with Alipay, get merchant ID (partner) and account ID (seller) Download Alipay SDK to generate order information, make a digital signature call Alipay client, pay treasure client to deal with Alipay Secure server after payment is finished, Alipay client will automatically jump back to the original application in the original application showing the payment results to the user to see Alipay Data Flow chart

official Data Flow chart


Note: In fact, only 1,3,7 step is our client needs to deal with, the rest only need to understand the process. Construct order data and sign

The Merchant client pays the interface rules of the development package according to Alipay Wallet, generates the signature result through the program and sets the data to be transmitted to the Alipay wallet to pay the development package. Send the request data to the Alipay wallet to pay the development package by passing the constructed data collection.

Alipay Wallet Payment Development Kit to process request data

Alipay Wallet Payment Development package will request the data according to the business rules wrapped after the delivery to the Alipay service side, the service side to obtain these collections, will be first security verification, such as verification, a series of verification after the pass will be processed to complete the data sent over the request.

Returns the processed result data

For transactions that have been completed, Alipay feeds the data back to the merchant application and merchant server in two different ways. On the mobile client, the development package client directly feeds the processed data results to the merchant client; The Alipay server initiates the notification, calling the merchant to set the page path when requested (parameter Notify_url, if the merchant is not set, the operation will not be done).

Processing of retrieved returned result data

After the client synchronously notifies the receiving module or the service-side asynchronous notification receiving module to obtain the result data returned by Alipay, it can be processed by the merchant's own business logic (such as: Order update, automatic recharge to the member account medium). Synchronization notification results are only used for results display, and inbound data is subject to asynchronous notification. Alipay Integration Steps How to contract how to integrate

We mainly explain the integration here, signing the steps to follow step by step. Alipay integrated download Sdk&demo file directory using the rules document mainly describes how to inherit the interface Development Package 2.0 Standard Edition document mainly describes the related interface to create the project, the SDK Import project to build a basic presentation interface into the demo libcrypto. A,LIBSSL.A Static Library, Openssl,util folder and Order,apauthv2info class

Error correction

If you encounter a post-run error, similar to the following prompt message:

Cannot find interface declaration for ' NSObject ', superclass of ' Base64 '

Then need to open the error file, add the header file.

#import <Foundation/Foundation.h>

If the merchant wants to use Alipay's development Package class library in a file, the reference header file needs to be added.

     #import <AlipaySDK/AlipaySDK.h>
' Openssl/asn1.h ' File not found error

Click on the project name, click on the Build Settings tab, in the Search box, search by keyword search, add the header file path to the header search paths: $ (srcroot)/project name. If the header file information has been increased, you do not need to increase it. Click on the project name, click on the "Build Settings" tab, in the Search box, with the keyword Bitcode, set to no AlipaySDK.h import UIKit frame Click on the project name, click "Build Phases" tab, in "Link Binary With librarles "option," Systemconfiguration.framework "System library file.

Generate a signed order string

    Generate order string
    nsstring *orderspec = [order description];
    Create an order signature based on the private key
    id<datasigner> signer = Creatersadatasigner (Privatekey);
    Generate signature string
    nsstring *signedstring = [signer signstring:orderspec];

Set scheme


Call the payment interface and listen for the payment result callback block callback

[[Alipaysdk Defaultservice] payorder:orderstring fromscheme:appscheme callback:^ (nsdictionary *resultDic) {
 NSLog (@ "Reslut =%@", resultdic);
}];

Alipay Wallet when opened callback in Appdelegate

  -(BOOL) Application: (UIApplication *) application
          OpenURL: (nsurl *) URL
    sourceapplication: (NSString *) Sourceapplication
           annotation: (id) annotation {

       //Jump Alipay Wallet for payment, process payment results
        [[ALIPAYSDK Defaultservice] Processorderwithpaymentresult:url standbycallback:^ (nsdictionary *resultdic) {
                      NSLog (@ "result =%@", resultdic);
          } ];

      return YES;
  }

10. Processing Alipay Wallet callback, in Appdelegate

  -(BOOL) Application: (UIApplication *) application
              OpenURL: (nsurl *) URL
    sourceapplication: (NSString *) Sourceapplication
           annotation: (id) annotation {
       //Jump Alipay Wallet for payment, process payment results
        [[ALIPAYSDK Defaultservice] Processorderwithpaymentresult:url standbycallback:^ (nsdictionary *resultdic) {
                      NSLog (@ "result =%@", resultdic) ;
          }];

      return YES;
  }

Generate Merchant Public key (RSA key generation and use) open the terminal into a suitable directory (I put it in the RSA folder of the desktop) to generate the RSA private key, enter the OpenSSL genrsa-out Rsa_private_key.pem 1024 generation Rsa_ PRIVATE_KEY.PEM generate RSA Public key, enter the OpenSSL rsa-in rsa_private_key.pem-pubout-out RSA_PUBLIC_KEY.PEM command Enter, generate a Rsa_ in the current folder The Public_key.pem file is the original merchant public key (please save the file properly, you need to use the file in the PHP development language), the following command performs the screenshot correctly: Generate the PKCS8 encoded private key, enter the command OpenSSL pkcs8-topk8-inform pem-i n rsa_private_key.pem-outform pem-nocrypt and enter, the current interface will directly show the result: Copy PKCS8 encoded private key to paste the copied content into a new Notepad, can be arbitrarily named, as long as you know this is PKCS8 The private key in the format (please save the file properly).

Use of public and private keys # Alipay


The encryption process can be divided into the following: (self-understanding, do not like to spray) the client private key to encrypt a certain section of information MD, and then the group purchase information and the information that was encrypted by the MD to do a session encryption into a session encryption package, and then the session key with Alipay public key encryption, Then encrypt the packet and encrypted session key to the background server to do decryption, first encrypted session key with the Alipay private key to decrypt the session key, and then use the session key to decrypt the encryption packet, and then take out the group purchase information and verification signature (calculate and encrypt the MD, only to do a verification), Then use the client's public key to do a signature verification, to prevent some problems later appear repudiation problem

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.