本地?cái)?shù)據(jù)庫(kù)創(chuàng)建訂單表。
建議包含以下字段,參考官方API( https://pingxx.com/document/api#api-c-new):
order_no:required
商戶訂單號(hào),適配每個(gè)渠道對(duì)此參數(shù)的要求,必須在商戶系統(tǒng)內(nèi)唯一。 alipay: 1-64 位, wx: 1-32 位, bfb: 1-20 位, upacp: 8-40 位, yeepay_wap:1-50 位, jdpay_wap:1-30 位, cnp_u:8-20 位, cnp_f:8-20 位, 推薦使用 8-20 位,要求數(shù)字或字母,不允許特殊字符
app[id]:required
支付使用的 app 對(duì)象的 id,請(qǐng)登陸管理平臺(tái)查看。
subject:required
商品的標(biāo)題,該參數(shù)最長(zhǎng)為 32 個(gè) Unicode 字符, 銀聯(lián)全渠道(upacp/upacp_wap)限制在 32 個(gè)字節(jié)。
body:required
商品的描述信息,該參數(shù)最長(zhǎng)為 128 個(gè) Unicode 字符, yeepay_wap 對(duì)于該參數(shù)長(zhǎng)度限制為 100 個(gè) Unicode 字符。
channel:required
支付使用的第三方支付渠道(更多請(qǐng)參考api) alipay:支付寶手機(jī)支付 alipay_wap:支付寶手機(jī)網(wǎng)頁(yè)支付 alipay_qr:支付寶掃碼支付 alipay_pc_direct:支付寶 PC 網(wǎng)頁(yè)支付 apple_pay:Apple Pay bfb:百度錢(qián)包移動(dòng)快捷支付 bfb_wap:百度錢(qián)包手機(jī)網(wǎng)頁(yè)支付 wx:微信支付 wx_pub:微信公眾賬號(hào)支付 wx_pub_qr:微信公眾賬號(hào)掃碼支付 jdpay_wap:京東手機(jī)網(wǎng)頁(yè)支付
amount: required
訂單總金額, 單位為對(duì)應(yīng)幣種的最小貨幣單位, 例如:人民幣為分(如訂單總金額為 1 元,此處請(qǐng)?zhí)?100)。
client_ip: required
發(fā)起支付請(qǐng)求終端的 IP 地址,格式為 IPV4,如: 127.0.0.1。
以上是在ping++ 平臺(tái)創(chuàng)建訂單時(shí)需要的參數(shù)
以下是在ping++ 平臺(tái)創(chuàng)建訂單成功以及付款成功回調(diào)的參數(shù)
paid :支付狀態(tài),默認(rèn)為false refunded :退款狀態(tài),默認(rèn)為false time_paid :付款時(shí)間 time_refunded:退款時(shí)間 charge_no:返回的charge編號(hào) transaction_no :交易號(hào)
步驟:
1.本地創(chuàng)建一條訂單記錄
def create_order
#獲取參數(shù) #判斷參數(shù)合法性 order = Order.new #保存訂單信息,注意subject以及body的長(zhǎng)度 #生成訂單號(hào)并保存 order_no = (Time.now.to_formatted_s(:number)).to_s 6.times{ order_norand(10).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 end
2.執(zhí)行支付
現(xiàn)在ping++ 平臺(tái)創(chuàng)建一條記錄
1.在order.rb文件中新建一個(gè)方法
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"] #不同支付渠道的回調(diào)地址 case self.channel when "alipay" extra = { } when "wx" extra = { } end #ping++平臺(tái)新建一個(gè)訂單 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::PingppError => error logger.error 'ping++平臺(tái)創(chuàng)建訂單失敗' logger.error error.http_body return false end end
2.調(diào)用pay_url方法創(chuàng)建訂單,返回給客戶端charge對(duì)象,客戶端拿著charge對(duì)象去ping++ 平臺(tái)支付
def confirm_and_payment order_no = params[:order_no] channel = params[:channel] if order_no.blank? || channel.blank? render_failure("參數(shù)不完整!") and return end order = Order.where(order_no: order_no).first if order.blank? render_failure("訂單不存在!")and return end charge = order.pay_url if charge == false render_failure("訂單支付失?。?) and return else order.update_attribute(:charge_no ,(JSON.parse charge.to_s)['id']) render(:json => charge) end end
異步通知更新付款結(jié)果
def notify status = 400 #判斷請(qǐng)求是否有ping++的簽名信息 if request.headers['x-pingplusplus-signature'].blank? status = 401 logger.debug '【報(bào)哪家】:======付款回調(diào)請(qǐng)求來(lái)源錯(cuò)誤?。。。?!' 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-pingplusplus-signature'].to_s end # 獲取「Webhooks 驗(yàn)證 Ping++ 公鑰」 pub_key_path ="#{Rails.root}/config/rsa_public_key.pem" if verify_signature(raw_data, signature, pub_key_path) #處理接收的結(jié)果 event = JSON.parse(raw_data) #付款成功 if event["type"] == 'charge.succeeded' # 開(kāi)發(fā)者在此處加入對(duì)支付異步通知的處理代碼 order_no = event['data']['object']['order_no'] order = Order.where(order_no: order_no).first order_from = order.status if order.present? #更新字段 order.paid = event['data']['object']['paid'] if order.save status = 200 else status = 500 end else logger.debug '數(shù)據(jù)庫(kù)沒(méi)有該條記錄!' end #退款成功 elsif event['type'] == 'refund.succeeded' # 開(kāi)發(fā)者在此處加入對(duì)退款異步通知的處理代碼 order_no = event['data']['object']['order_no'] order = Order.where(order_no: order_no).first if order.present? #更新字段 order.time_refunded = Time.at(event['data']['object']['time_succeed']) if order.save status = 200 else status = 500 end else logger.debug '數(shù)據(jù)庫(kù)沒(méi)有該條記錄!' end else logger.debug '付款回調(diào)返回未知操作!' end else logger.debug '付款回調(diào)請(qǐng)求來(lái)源錯(cuò)誤!' status = 403 end render :nothing => true, :status => status end
標(biāo)簽:張家界 公主嶺 三沙 遼寧 梧州 永州 普洱 荊門(mén)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby on Rails在Ping ++ 平臺(tái)實(shí)現(xiàn)支付》,本文關(guān)鍵詞 Ruby,Rails,在,Ping,平臺(tái),實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。