Ruby on Rails realizes payment _ruby topic on Ping + + platform

Source: Internet
Author: User
Tags ruby on rails

The local database creates the order table.

Suggestions include the following fields, referring to the official API (HTTPS://PINGXX.COM/DOCUMENT/API):

Order_no:required

  The merchant order number, suitable for each channel requirement for this parameter, must be unique within the merchant system.
  alipay:1-64 bit,
  wx:1-32 bit,
  bfb:1-20 bit,
  upacp:8-40 bit,
  yeepay_wap:1-50
  bit, jdpay_wap:1-30 bit, 
   cnp_u:8-20 bit,
  cnp_f:8-20 bit,
  recommended to use 8-20 digits, require numbers or letters, do not allow special characters

App[id]:required

 To pay for the ID of the app object used, please login to the admin platform for viewing.

Subject:required

  The title of the item, which is the maximum of 32 Unicode characters, and the
  Full channel (Upacp/upacp_wap) of UnionPay is limited to 32 bytes.

Body:required

 Description of the product, which is the maximum of 128 Unicode characters, and the
 yeepay_wap limit to 100 Unicode characters for this parameter length.

Channel:required

 Pay for the use of Third-party payment channels (more please refer to API)
  Alipay: Alipay mobile payment
  Alipay_wap: Alipay Mobile Web page payment
  ALIPAY_QR: Pay treasure Sweep code payment
  Alipay_pc_direct: Alipay PC Web page to pay
  apple_pay:apple pay
  BFB: Baidu Wallet Mobile Quick pay
  Bfb_wap: Baidu Wallet Mobile Web page pay   
  WX: micro-Credit Payment
  wx_pub: Micro-letter Public account payment
  WX_PUB_QR: Micro-Credit public account sweep code payment
  jdpay_wap: Beijing East mobile Phone page payment

Amount:required

 Total order Amount, which is the smallest unit of currency in the corresponding currency,
 for example: RMB (if the total order amount is 1 yuan, please fill 100 here).

Client_ip:required

  Initiates the payment request Terminal's IP address, the format is IPV4, for example: 127.0.0.1.


These are the parameters that are required to create an order on the ping++ platform

The following are the parameters for the successful order creation and the successful callback of the ping++ platform

Paid: Payment status, default to False
refunded: refund status, default to False
Time_paid: Payment time
time_refunded: Refund Time
Charge_ No: Returned charge number
Transaction_no: Transaction number

Steps:

1. Create a local order record

def Create_order

 #获取参数  
 #判断参数合法性 
 
 order = order.new
 #保存订单信息, note the length of subject and body
 #生成订单号并保存
 order_no = (Time.now.to_ formatted_s (: Number)). to_s
 6.times{order_no<<rand. to_s}
 order.order_no = Order_no

 #获取ip并保存
 order.client_ip = request.remote_ip
 
 if order.save
  #返回成功信息
 else
  render_failure ( Order.errors.messages.first[1][0])
 End
 

2. Implementation of payment

Now create a record on the ping++ platform
1. Create a new method in the Order.rb file

 def pay_url
  #获取api_key以及app_id
  pingpp.api_key = pingplusplus.get_ping_settings["Ping_api_key"]
  app_id = pingplusplus.get_ping_settings["ping_app_id"]
  #不同支付渠道的回调地址 case
  Self.channel when
    "Alipay"
    Extra = {
   } when
    ' WX '
    extra = {
   } 
   end
  #ping + + platform Create a new order
  begin
   charge = PINGPP:: Charge.create (
     : Order_no => self.order_no,
     : App  => {: id => app_id},
     : Channel => Self.channel,
     : Amount => Self.amount.round (2) * 100.to_i,
     : Client_ip =>, Self.client_ip,
     : Currency => "CNY",
     : Subject => self.subject[0..31],
     : Body  => self.body[0..127],
     extra  => extra
     )
   
   Return charge
  Rescue pingpp::P ingpperror => error
    logger.error ' ping++ platform Create order failed '
    Logger.error Error.http_body return
    false
  end
 

2. Call the Pay_url method to create the order, return to the client charge object, the client takes charge object to ping++ platform to pay

 def confirm_and_payment
  order_no = params[:order_no]
  channel = Params[:channel]
  if Order_no.blank? | | Channel.blank?
   Render_failure ("parameter is incomplete!") ") and return-order
 
  = Order.where (order_no:order_no).
    Render_failure ("The order does not exist!") ") and return
  end

  charge = Order.pay_url
  if charge = = False
   render_failure (" Order payment failed! ") ") and return
  else
   order.update_attribute (: Charge_no, (Json.parse charge.to_s) [' ID '])
   render (: JSON = > Charge)
  end
 

Asynchronous notification Update Payment results

 def notify status = #判断请求是否有ping + + signature information if request.headers[' X-pingplusplus-signature '].blank? Status = 401 Logger.debug ' "to whom": = = = = = Payment CALLBACK Request SOURCE Error!!!!! ' Return end #获取签名信息 Raw_data = Request.body.read if request.headers[' x-pingplusplus-signature '].is_a? (Array) signature = request.headers[' x-pingplusplus-signature '][0].to_s else signature = request.headers[' X-PINGPL
  Usplus-signature '].to_s End # get "webhooks verify ping++ public key"pub_key_path = "#{rails.root}/config/rsa_public_key.pem" If Verify_signature (Raw_data, Signature, Pub_key_path) #处理接收的结果 event = Json.parse (raw_data) #付款成功 if EV  ent["type"] = = ' charge.succeeded ' # Developer adds the processing code for the payment asynchronous notification here Order_no = event[' data ' [' object '] [' order_no '] order
     = Order.where (order_no:order_no). Order_from = Order.status if order.present? #更新字段 order.paid = event[' data '] [' object '] [' paid '] if order.save status = 5 Else status =
     00End Else Logger.debug ' database does not have this record! ' End #退款成功 elsif event[' type '] = = ' refund.succeeded ' # Developer adds the processing code for the refund asynchronous notification here Order_no = event[' data '
     [' object '] [' order_no '] order = Order.where (order_no:order_no).
     #更新字段 order.time_refunded = time.at (event[' data '] [' object '] [' time_succeed ']) if order.save status = 200 else status = End Else Logger.debug ' database does not have this record! ' End else Logger.debug ' payment callback returns an unknown operation! ' End else Logger.debug ' payment callback request source Error!
 ' Status = 403 end render:nothing => true: the status => status end

Related Article

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.