0 12 min 1 week

  Continuing from the previous section, this section introduces the address book feature code and implements the user order placement and order payment functions.

  The address book refers to the address information of consumer users. After the user logs in successfully, they can maintain their own address information. A single user can have multiple address entries, but only one default address is allowed.

  Please add image description

  For address book management, we need to implement the following functions: query address list, add address, modify address, delete address, set default address, query default address. A total of 7 interfaces are included:

  1). Add address

  Please add image description

  2). Query all addresses of the logged-in user

  Please add image description

  3). Query default address

  Please add image description

  4). Modify address

  Please add image description

  5). Delete address by id

  Please add image description

  6). Query address by id

  Please add image description

  7). Set default address

  Please add image description

  The user’s address information will be stored in the address_book table, that is, the address book table. There is a field is_default in it. In fact, when we set the default address, we only need to update this field.

  Field name Data type Description Remarks id bigint Primary key auto-increment user_id bigint User ID Logical foreign key consignee varchar(50) Consignee sex varchar(2) Gender phone varchar(11) Phone number province_code varchar(12) Province code province_name varchar(32) Province name city_code varchar(12) City code city_name varchar(32) City name district_code varchar(12) District code district_name varchar(32) District name detail varchar(200) Detailed address information down to the door number label varchar(100) Label company, home, school is_default tinyint(1) Whether the default address 1 is yes 0 no

  For this kind of single-table CRUD operations, we have written many, so we can directly import them and make a test. Enter the sky-server module and import the address book module function code from the course materials:

  Controller layer

  com.sky.controller.user.AddressBookController

  Service layer implementation class

  com.sky.service.impl.AddressBookServiceImpl

  Mapper layer

  com.sky.mapper.AddressBookMapper

  mapper/AddressBookMapper.xml

  Use front-end and back-end integration testing:

  Log in, go to the homepage –> go to the personal center –> go to address management

  Please add image description

  Here, test the interfaces for adding new shipping addresses, viewing shipping addresses, setting default shipping addresses, and deleting shipping addresses, and observe the database data to ensure accuracyonline casino victory and What is it. The test is passed by submitting code.

  Users notify merchants of their purchase by placing orders, indicating that they have purchased goods and that the merchant needs to prepare and ship the goods. After placing an order, related order data will be generated, and the order data needs to reflect the following information:

  Insert image description here

  After the user adds dishes or sets to the shopping cart, they can click the “Go to checkout” button in the shopping cart, and the page will jump to the order confirmation page. Click the “Go to payment” button to complete the order operation. The effect diagram is as shown below:

  Please add image description

  The interface is as follows:

  Please add image description

  The data tables corresponding to the user order business are orders table and order_detail table (one-to-many relationship, one order is associated with multiple order details). When the user submits an order, a record needs to be inserted into the order table orders, and one or more records need to be inserted into order_detail (from the shopping cart).

  Table name meaning description orders order table mainly stores basic order information (such as: order number, status, amount, payment method, ordering user, recipient address, etc.) order_detail order detail table mainly stores detailed order information (such as: the information of the套餐 and dishes associated with the order)

  1). orders order table

  Field name data type description remarks id bigint primary key auto-increment numbervarchar(50) order number status int order status 1 pending payment 2 pending order 3 accepted 4 in transit 5 completed 6 cancelled user_id bigint user id logical foreign key address_book_id bigint address id logical foreign key order_time datetime order time checkout_time datetime payment time pay_method int payment method 1 WeChat payment 2 Alipay payment pay_status tinyint payment status 0 unpaid 1 paid 2 refund amount decimal(10,2) order amount remark varchar(100) remarks information phone varchar(11) redundant field address varchar(255) detailed address information redundant field consignee varchar(32) recipient redundant field cancel_reason varchar(255) order cancellation reason rejection_reason varchar(255) rejection reason cancel_time datetime order cancellation time estimated_delivery_time datetime estimated delivery time delivery_status tinyint delivery status 1 immediate delivery 0 select specific time delivery_time datetime delivery time pack_amount int packing fee tableware_number int tableware quantity tableware_status tinyint tableware quantity status 1 provided according to meal size 0 select specific quantitysports betting cheats and What is it

  2). order_detail order detail table

  Field name data type description remarks id bigint primary key auto-increment name varchar(32) product name redundant field image varchar(255) product image path redundant field order_id bigint order id logical foreign key dish_id bigint dish id logical foreign key setmeal_id bigint setmeal id logical foreign key dish_flavor varchar(50) dish flavor number int product quantity amount decimal(10,2) product unit price

  Design the DTO based on the parameters of the user order interface, in the sky-pojo module, OrdersSubmitDTO.java has been defined.

  Insert image description here

  Design the VO based on the return result of the user order interface, in the sky-pojo module, OrderSubmitVO.java has been defined.

  Insert image description here

  Controller layer

  Create the com.sky.controller.user.OrderController and provide the user order method

  Service layer implementation class

  Create the com.sky.service.impl.OrderServiceImpl implementation of the OrderService interface

  Mapper layer

  Create OrderMapper interface and corresponding xml mapping file

  OrderMapper.java

  OrderMapper.xml

  Create OrderDetailMapper interface and corresponding xml mapping file:

  OrderDetailMapper.java

  OrderDetailMapper.xml

  Function test, log in to the mini-program, complete the order operation, and at the same time, the data in the shopping cart will be deleted during the order operation

  Insert image description here

  View shopping_cart table:

  Insert image description here

  Go to checkout -> Go to payment

  Please add image description

  View orders table:

  Insert image description here

  View order_detail table:

  Insert image description here

  At the same time, the data in the shopping cart table is deleted:

  Insert image description here

  Test passed, submit code to github.

  After the user order is completed, the next step is order payment, that is, to complete the payment function. To implement WeChat payment, it is necessary to register a WeChat payment merchant account, which must have a business entity and a regular business license. Only after having these qualifications can you register a merchant account and open the payment permission.

  Individuals do not have this qualification, so when we study WeChat payment, we mainly need to understand the process of WeChat payment and be able to read the interface documents provided by the WeChat official, and be able to connect with third-party payment platforms. WeChat payment products:

  Insert image description here

  This project chooses mini-program payment, and can refer to the official website, WeChat mini-program payment sequence diagram:

  Insert image description here

  WeChat payment related interfaces:

  JSAPI order: the merchant system calls this interface to generate a pre-payment transaction order in the WeChat payment service background (corresponding to step 5 of the sequence diagram)

  Insert image description here

  WeChat mini-program triggers payment: obtain the necessary parameters prepay_id through the JSAPI order interface, and then use the mini-program payment method provided by WeChat payment to trigger mini-program payment (corresponding to step 10 of the sequence diagram)

  Insert image description here

  How to ensure data security? There are two key steps to complete WeChat payment:

  The first is to call an order interface of the WeChat background in the merchant system, that is, to generate a pre-payment transaction order. The second is that after the payment is successful, the WeChat background will push messages. The security requirements for the data of these two interfaces are actually very high.

  The methods provided by WeChat for data are encryption, decryption, and signing. To complete data encryption and decryption, it is necessary to prepare some files in advance, which are actually some certificates. WeChat payment platform certificate, merchant private key file:

  Insert image description here

  The WeChat background can call the payment result pushed by the merchant system, and here we will encounter a problem, that is, how can the WeChat background call our merchant system? Because this call process, in essence, is also an HTTP request.

  Currently, the IP address of the merchant system is the IP address of the current computer, which is just an IP address within a local area network, and the WeChat background cannot access it.

  Solution: Internet penetration. By using cpolar software, you can obtain a temporary domain, which is a public IP, so that the WeChat background can request the merchant system. Download address

  Copy authtoken:

  Insert image description here

  Execute the following command in the installation directory to set the configuration file on our computer:

  Insert image description here

  On Windows system, execute the command: cpolar.exe authoken ***

  Execute the command to obtain the temporary domain again:

  Insert image description here

  Insert image description here

  Access the API documentation, which was originally localhost:8080/doc.html, and now access it using a temporary domain to verify the validity of the temporary domain:

  Insert image description here

  The teacher asked to import the code from the materials, download a hook, but it couldn’t be used even after importing. The keys are all expired. The teacher also cannot provide the WeChat payment platform certificate and merchant private key file, which involves merchant privacy. The code is placed here, and it can be checked later when it is needed for future development.

  WeChat payment related configuration

  application-dev.yml

  application.yml

  WeChatProperties.java: Read configuration (already defined)

  Controller layer

  Add the payment method to OrderController.java

  com.sky.controller.notify.PayNotifyController.java

  Service layer implementation class

  Implement the payment and paySuccess methods in OrderServiceImpl.java

  WeChatPayUtil has already been defined in the common module under com.sky.utils:

  Mapper layer

  Add getByNumberAndUserId and update methods to OrderMapper.java

  Add to OrderMapper.xml

  Since individuals cannot apply for WeChat payment qualifications, it is necessary to bypass this part of the code to continue this project. You can directly modify this part of the code in the WeChat mini-program, comment out the entire requestPayment method, and then uncomment the redirection below:

  Insert image description here

  Insert image description here

  Comment out the following part in the payment method of OrderServiceImpl in the backend code, path:
lottery secretsJoin us
  Insert image description here

  It is equivalent to not actually executing the paysuccess method, so the logic of modifying the order status is also added to the payment function, and the final payment function is as follows:

  The backend and mini-program end are recompiled, and the request can be re-made. Finally, you can see that the payment has been successfully made:

  Insert image description here

  Test successful, submit the code to github.