Module: AlipayGlobal::Service::Trade

Defined in:
lib/alipay_global/service/trade.rb

Constant Summary collapse

BOUNDARY =
"AaB03x"
CREATE_QUERY_REQUIRED_PARAMS =
%w( out_trade_no )
CREATE_TRADE_REQUIRED_PARAMS =
%w( notify_url subject out_trade_no currency )
CREATE_TRADE_OPTIONAL_PARAMS =
%w( return_url body total_fee rmb_fee order_gmt_create order_valid_time timeout_rule auth_token supplier seller_id seller_name seller_industry )

Class Method Summary collapse

Class Method Details

.batch_refund(refunds) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/alipay_global/service/trade.rb', line 69

def self.batch_refund(refunds)
  #DISABLED
  file = build_refund_file(refunds)

  params = {
    'service'         => 'forex_refund_file',
    'partner'         => AlipayGlobal.api_partner_id
  }

  uri = AlipayGlobal::Service.request_uri(params)

  form_params = { 'partner' => AlipayGlobal.api_partner_id, 'service' => 'forex_refund_file', 'refund_file' => File.read(file.path) }

  resp = RestClient.post uri.to_s, :partner => AlipayGlobal.api_partner_id, :file => File.new(file.path, 'rb'), :service => 'forex_refund_file'

  alipay_resp = Nokogiri::XML(resp.body)

  alipay_results = alipay_resp.at_xpath('//alipay')

  file.unlink
end

.build_query_uri(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/alipay_global/service/trade.rb', line 91

def self.build_query_uri(params)
  params = AlipayGlobal::Utils.stringify_keys(params)
  AlipayGlobal::Service.check_required_params(params, CREATE_QUERY_REQUIRED_PARAMS)

  params = {
    'service'         => 'single_trade_query',
    '_input_charset'  => 'utf-8',
    'partner'         => AlipayGlobal.api_partner_id
  }.merge(params)

  AlipayGlobal::Service.request_uri(params)
end

.build_refund_file(refunds) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/alipay_global/service/trade.rb', line 104

def self.build_refund_file(refunds)
  file = Tempfile.new(['refund','.txt'])
  refund_content = AlipayGlobal::Utils.write_refund_content(refunds)
  file.write(refund_content)
  file.close
  file
end

.build_refund_uri(refund) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/alipay_global/service/trade.rb', line 112

def self.build_refund_uri(refund)
  refund[:reason] = "no_reason" if !refund[:reason] 
  refund[:reason] = "no_reason" if refund[:reason].strip.length == 0
  params = {
    'service'         => 'forex_refund',
    '_input_charset'  => 'utf-8',
    'partner'         => AlipayGlobal.api_partner_id
  }.merge(refund)

  AlipayGlobal::Service.request_uri(params)
end

.create(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/alipay_global/service/trade.rb', line 14

def self.create(params)
  is_mobile = params.delete(:mobile) || params.delete("mobile")
  service_type = is_mobile ? "create_forex_trade_wap" : "create_forex_trade"

  params = AlipayGlobal::Utils.stringify_keys(params)
  AlipayGlobal::Service.check_required_params(params, CREATE_TRADE_REQUIRED_PARAMS)
  AlipayGlobal::Service.check_optional_params(params, CREATE_TRADE_OPTIONAL_PARAMS)

  params = {
    'service'         => service_type,
    '_input_charset'  => 'utf-8',
    'partner'         => AlipayGlobal.api_partner_id
  }.merge(params)

  AlipayGlobal::Service.request_uri(params).to_s
end

.process_alipay_trade_response(success, alipay_xml) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/alipay_global/service/trade.rb', line 43

def self.process_alipay_trade_response(success, alipay_xml)
  if success
    response_xml = alipay_xml.at_xpath('//response').at_xpath('//trade')
    {
      trade_no: response_xml.at_xpath('//trade_no').content,
      out_trade_no: response_xml.at_xpath('//out_trade_no').content,
      subject: response_xml.at_xpath('//subject').content,
      trade_status: response_xml.at_xpath('//trade_status').content
    }
  else
    alipay_xml.at_xpath('//error').content
  end
end

.refund(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/alipay_global/service/trade.rb', line 57

def self.refund(params)
  resp = Net::HTTP.get(build_refund_uri(params))

  alipay_results = Nokogiri::XML(resp).at_xpath('//alipay')

  alipay_success = alipay_results.at_xpath('//is_success').content == "T"

  alipay_reason = alipay_success ? "" : alipay_results.at_xpath('//error').content

  { success: alipay_success , message: alipay_reason }
end

.status(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/alipay_global/service/trade.rb', line 31

def self.status(params)
  resp = Net::HTTP.get(build_query_uri(params))

  alipay_results = Nokogiri::XML(resp).at_xpath('//alipay')

  alipay_success = alipay_results.at_xpath('//is_success').content == "T"

  status_response = process_alipay_trade_response(alipay_success,alipay_results) 

  { success: alipay_success , message: status_response }
end