paypkg 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,233 @@
1
+ #####################
2
+ ### COUNTRY CODES ###
3
+ #####################
4
+
5
+ module PaypalCountries
6
+ PAYPAL_COUNTRIES = {
7
+ "ALAND ISLANDS" => "AX",
8
+ "ALBANIA" => "AL",
9
+ "ALGERIA" => "DZ",
10
+ "AMERICAN SAMOA" => "AS",
11
+ "ANDORRA" => "AD",
12
+ "ANGOLA" => "AO",
13
+ "ANGUILLA" => "AI",
14
+ "ANTARCTICA" => "AQ",
15
+ "ANTIGUA AND BARBUDA" => "AG",
16
+ "ARGENTINA" => "AR",
17
+ "ARMENIA" => "AM",
18
+ "ARUBA" => "AW",
19
+ "AUSTRALIA" => "AU",
20
+ "AUSTRIA" => "AT",
21
+ "AZERBAIJAN" => "AZ",
22
+ "BAHAMAS" => "BS",
23
+ "BAHRAIN" => "BH",
24
+ "BANGLADESH" => "BD",
25
+ "BARBADOS" => "BB",
26
+ "BELGIUM" => "BE",
27
+ "BELIZE" => "BZ",
28
+ "BENIN" => "BJ",
29
+ "BERMUDA" => "BM",
30
+ "BHUTAN" => "BT",
31
+ "BOLIVIA" => "BO",
32
+ "BOSNIA-HERZEGOVINA" => "BA",
33
+ "BOTSWANA" => "BW",
34
+ "BOUVET ISLAND" => "BV",
35
+ "BRAZIL" => "BR",
36
+ "BRITISH INDIAN OCEAN TERRITORY" => "IO",
37
+ "BRUNEI DARUSSALAM" => "BN",
38
+ "BULGARIA" => "BG",
39
+ "BURKINA FASO" => "BF",
40
+ "BURUNDI" => "BI",
41
+ "CAMBODIA" => "KH",
42
+ "CANADA" => "CA",
43
+ "CAPE VERDE" => "CV",
44
+ "CAYMAN ISLANDS" => "KY",
45
+ "CENTRAL AFRICAN REPUBLIC" => "CF",
46
+ "CHAD" => "TD",
47
+ "CHILE" => "CL",
48
+ "CHINA" => "CN (For domestic Chinese bank transactions only)",
49
+ "CHRISTMAS ISLAND" => "CX",
50
+ "COCOS (KEELING) ISLANDS" => "CC",
51
+ "COLOMBIA" => "CO",
52
+ "COMOROS" => "KM",
53
+ "DEMOCRATIC REPUBLIC OF CONGO" => "CD",
54
+ "CONGO" => "CG",
55
+ "COOK ISLANDS" => "CK",
56
+ "COSTA RICA" => "CR",
57
+ "CROATIA" => "HR",
58
+ "CYPRUS" => "CY",
59
+ "CZECH REPUBLIC" => "CZ",
60
+ "DENMARK" => "DK",
61
+ "DJIBOUTI" => "DJ",
62
+ "DOMINICA" => "DM",
63
+ "DOMINICAN REPUBLIC" => "DO",
64
+ "ECUADOR" => "EC",
65
+ "EGYPT" => "EG",
66
+ "EL SALVADOR" => "SV",
67
+ "ERITERIA" => "ER",
68
+ "ESTONIA" => "EE",
69
+ "ETHIOPIA" => "ET",
70
+ "FALKLAND ISLANDS (MALVINAS)" => "FK",
71
+ "FAROE ISLANDS" => "FO",
72
+ "FIJI" => "FJ",
73
+ "FINLAND" => "FI",
74
+ "FRANCE" => "FR",
75
+ "FRENCH GUIANA" => "GF",
76
+ "FRENCH POLYNESIA" => "PF",
77
+ "FRENCH SOUTHERN TERRITORIES" => "TF",
78
+ "GABON" => "GA",
79
+ "GAMBIA" => "GM",
80
+ "GEORGIA" => "GE",
81
+ "GERMANY" => "DE",
82
+ "GHANA" => "GH",
83
+ "GIBRALTAR" => "GI",
84
+ "GREECE" => "GR",
85
+ "GREENLAND" => "GL",
86
+ "GRENADA" => "GD",
87
+ "GUADELOUPE" => "GP",
88
+ "GUAM" => "GU",
89
+ "GUATEMALA" => "GT",
90
+ "GUERNSEY" => "GG",
91
+ "GUINEA" => "GN",
92
+ "GUINEA BISSAU" => "GW",
93
+ "GUYANA" => "GY",
94
+ "HEARD ISLAND AND MCDONALD ISLANDS" => "HM",
95
+ "HOLY SEE (VATICAN CITY STATE)" => "VA",
96
+ "HONDURAS" => "HN",
97
+ "HONG KONG" => "HK",
98
+ "HUNGARY" => "HU",
99
+ "ICELAND" => "IS",
100
+ "INDIA" => "IN",
101
+ "INDONESIA" => "ID",
102
+ "IRELAND" => "IE",
103
+ "ISLE OF MAN" => "IM",
104
+ "ISRAEL" => "IL",
105
+ "ITALY" => "IT",
106
+ "JAMAICA" => "JM",
107
+ "JAPAN" => "JP",
108
+ "JERSEY" => "JE",
109
+ "JORDAN" => "JO",
110
+ "KAZAKHSTAN" => "KZ",
111
+ "KENYA" => "KE",
112
+ "KIRIBATI" => "KI",
113
+ "KOREA, REPUBLIC OF" => "KR",
114
+ "KUWAIT" => "KW",
115
+ "KYRGYZSTAN" => "KG",
116
+ "LAOS" => "LA",
117
+ "LATVIA" => "LV",
118
+ "LESOTHO" => "LS",
119
+ "LIECHTENSTEIN" => "LI",
120
+ "LITHUANIA" => "LT",
121
+ "LUXEMBOURG" => "LU",
122
+ "MACAO" => "MO",
123
+ "MACEDONIA" => "MK",
124
+ "MADAGASCAR" => "MG",
125
+ "MALAWI" => "MW",
126
+ "MALAYSIA" => "MY",
127
+ "MALDIVES" => "MV",
128
+ "MALI" => "ML",
129
+ "MALTA" => "MT",
130
+ "MARSHALL ISLANDS" => "MH",
131
+ "MARTINIQUE" => "MQ",
132
+ "MAURITANIA" => "MR",
133
+ "MAURITIUS" => "MU",
134
+ "MAYOTTE" => "YT",
135
+ "MEXICO" => "MX",
136
+ "MICRONESIA, FEDERATED STATES OF" => "FM",
137
+ "MOLDOVA, REPUBLIC OF" => "MD",
138
+ "MONACO" => "MC",
139
+ "MONGOLIA" => "MN",
140
+ "MONTENEGRO" => "ME",
141
+ "MONTSERRAT" => "MS",
142
+ "MOROCCO" => "MA",
143
+ "MOZAMBIQUE" => "MZ",
144
+ "NAMIBIA" => "NA",
145
+ "NAURU" => "NR",
146
+ "NEPAL" => "NP",
147
+ "NETHERLANDS" => "NL",
148
+ "NETHERLANDS ANTILLES" => "AN",
149
+ "NEW CALEDONIA" => "NC",
150
+ "NEW ZEALAND" => "NZ",
151
+ "NICARAGUA" => "NI",
152
+ "NIGER" => "NE",
153
+ "NIUE" => "NU",
154
+ "NORFOLK ISLAND" => "NF",
155
+ "NORTHERN MARIANA ISLANDS" => "MP",
156
+ "NORWAY" => "NO",
157
+ "OMAN" => "OM",
158
+ "PALAU" => "PW",
159
+ "PALESTINE" => "PS",
160
+ "PANAMA" => "PA",
161
+ "PARAGUAY" => "PY",
162
+ "PAPUA NEW GUINEA" => "PG",
163
+ "PERU" => "PE",
164
+ "PHILIPPINES" => "PH",
165
+ "PITCAIRN" => "PN",
166
+ "POLAND" => "PL",
167
+ "PORTUGAL" => "PT",
168
+ "PUERTO RICO" => "PR",
169
+ "QATAR" => "QA",
170
+ "REUNION" => "RE",
171
+ "ROMANIA" => "RO",
172
+ "REPUBLIC OF SERBIA" => "RS",
173
+ "RUSSIAN FEDERATION" => "RU",
174
+ "RWANDA" => "RW",
175
+ "SAINT HELENA" => "SH",
176
+ "SAINT KITTS AND NEVIS" => "KN",
177
+ "SAINT LUCIA" => "LC",
178
+ "SAINT PIERRE AND MIQUELON" => "PM",
179
+ "SAINT VINCENT AND THE GRENADINES" => "VC",
180
+ "SAMOA" => "WS",
181
+ "SAN MARINO" => "SM",
182
+ "SAO TOME AND PRINCIPE" => "ST",
183
+ "SAUDI ARABIA" => "SA",
184
+ "SENEGAL" => "SN",
185
+ "SEYCHELLES" => "SC",
186
+ "SIERRA LEONE" => "SL",
187
+ "SINGAPORE" => "SG",
188
+ "SLOVAKIA" => "SK",
189
+ "SLOVENIA" => "SI",
190
+ "SOLOMON ISLANDS" => "SB",
191
+ "SOMALIA" => "SO",
192
+ "SOUTH AFRICA" => "ZA",
193
+ "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS" => "GS",
194
+ "SPAIN" => "ES",
195
+ "SRI LANKA" => "LK",
196
+ "SURINAME" => "SR",
197
+ "SVALBARD AND JAN MAYEN" => "SJ",
198
+ "SWAZILAND" => "SZ",
199
+ "SWEDEN" => "SE",
200
+ "SWITZERLAND" => "CH",
201
+ "TAIWAN, PROVINCE OF CHINA" => "TW",
202
+ "TAJIKISTAN" => "TJ",
203
+ "TANZANIA, UNITED REPUBLIC OF" => "TZ",
204
+ "THAILAND" => "TH",
205
+ "TIMOR-LESTE" => "TL",
206
+ "TOGO" => "TG",
207
+ "TOKELAU" => "TK",
208
+ "TONGA" => "TO",
209
+ "TRINIDAD AND TOBAGO" => "TT",
210
+ "TUNISIA" => "TN",
211
+ "TURKEY" => "TR",
212
+ "TURKMENISTAN" => "TM",
213
+ "TURKS AND CAICOS ISLANDS" => "TC",
214
+ "TUVALU" => "TV",
215
+ "UGANDA" => "UG",
216
+ "UKRAINE" => "UA",
217
+ "UNITED ARAB EMIRATES" => "AE",
218
+ "UNITED KINGDOM" => "GB",
219
+ "UNITED STATES" => "US",
220
+ "UNITED STATES MINOR OUTLYING ISLANDS" => "UM",
221
+ "URUGUAY" => "UY",
222
+ "UZBEKISTAN" => "UZ",
223
+ "VANUATU" => "VU",
224
+ "VENEZUELA" => "VE",
225
+ "VIETNAM" => "VN",
226
+ "VIRGIN ISLANDS, BRITISH" => "VG",
227
+ "VIRGIN ISLANDS, U.S." => "VI",
228
+ "WALLIS AND FUTUNA" => "WF",
229
+ "WESTERN SAHARA" => "EH",
230
+ "YEMEN" => "YE",
231
+ "ZAMBIA" => "ZM"
232
+ }
233
+ end
@@ -0,0 +1,32 @@
1
+ ######################
2
+ ### CURRENCY CODES ###
3
+ ######################
4
+
5
+ module PaypalCurrencies
6
+ PAYPAL_CURRENCIES = {
7
+ "Australian dollar" => "AUD",
8
+ "Brazilian real" => "BRL",
9
+ "Canadian dollar" => "CAD",
10
+ "Czech koruna" => "CZK",
11
+ "Danish krone" => "DKK",
12
+ "Euro" => "EUR",
13
+ "Hong Kong dollar" => "HKD",
14
+ "Hungarian forint" => "HUF",
15
+ "Israeli new shekel" => "ILS",
16
+ "Japanese yen" => "JPY",
17
+ "Malaysian ringgit" => "MYR",
18
+ "Mexican peso" => "MXN",
19
+ "New Taiwan dollar" => "TWD",
20
+ "New Zealand dollar" => "NZD",
21
+ "Norwegian krone" => "NOK",
22
+ "Philippine peso" => "PHP",
23
+ "Polish złoty" => "PLN",
24
+ "Pound sterling" => "GBP",
25
+ "Singapore dollar" => "SGD",
26
+ "Swedish krona" => "SEK",
27
+ "Swiss franc" => "CHF",
28
+ "Thai baht" => "THB",
29
+ "Turkish lira" => "TRY",
30
+ "United States dollar" => "USD"
31
+ }
32
+ end
@@ -0,0 +1,35 @@
1
+ ######################
2
+ ### LANGUAGE CODES ###
3
+ ######################
4
+
5
+ module PaypalLanguages
6
+ PAYPAL_LANGUAGES = [
7
+ "da_DK",
8
+ "de_DE",
9
+ "en_AU",
10
+ "en_GB",
11
+ "en_US",
12
+ "es_ES",
13
+ "es_XC",
14
+ "fr_CA",
15
+ "fr_FR",
16
+ "fr_XC",
17
+ "he_IL",
18
+ "id_ID",
19
+ "it_IT",
20
+ "ja_JP",
21
+ "nl_NL",
22
+ "no_NO",
23
+ "pl_PL",
24
+ "pt_BR",
25
+ "pt_PT",
26
+ "ru_RU",
27
+ "sv_SE",
28
+ "th_TH",
29
+ "tr_TR",
30
+ "zh_CN",
31
+ "zh_HK",
32
+ "zh_TW",
33
+ "zh_XC"
34
+ ]
35
+ end
@@ -0,0 +1,26 @@
1
+ ###############################################
2
+ ### Full or Partial Refund Previous Payment ###
3
+ ###############################################
4
+
5
+ class Paypkg
6
+ # You can request a partial or full refund of a previous sale.
7
+ # You CANNOT, however, refund more than the original sale.
8
+ # If you need to do that, you'll have to do it manually
9
+ # through your PalPal account.
10
+ #
11
+ # If you think you'll ever want to look up the refund,
12
+ # you'll have to save the refund_id in your database.
13
+ #
14
+ # @param sale_id [String] Required.
15
+ # @param amount [Numeric] Required.
16
+ def refund_sale(sale_id, amount)
17
+ formatted_amount = "%0.2f"%amount
18
+ call_paypal("/v1/payments/sale/#{sale_id}/refund", "{
19
+ 'amount': {
20
+ 'total': #{formatted_amount},
21
+ 'currency': 'USD'
22
+ }
23
+ }")
24
+ return (@status.last=='201') && (@hash.last[:state]=='completed')
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ ######################################
2
+ ### Retrieve a card from the vault ###
3
+ ######################################
4
+
5
+ class Paypkg
6
+ # Retrieve a card from the vault.
7
+ # @param vault_id [String] The vault_id PayPal assigned when
8
+ # you stored this card.
9
+ # @return [Hash] The card data, less the card number: only the
10
+ # last 4 digits of the card number are returned. You can get
11
+ # an error from this call if the card expired and PayPal
12
+ # automatically removed it from the vault. If that happens,
13
+ # you should delete it from your database.
14
+ def retrieve_credit_card(vault_id)
15
+ call_paypal("/v1/vault/credit-card/#{vault_id}")
16
+ return (@status.last=='200') && (@hash.last[:state]=='ok')
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ ###################################
2
+ ### Retrieve Refund Transaction ###
3
+ ###################################
4
+
5
+ class Paypkg
6
+ # Use the refund_id to look up the refund.
7
+ # @param refund_id [String] Required.
8
+ # @return [json String] The same json string retured when
9
+ # the refund was orginally completed.
10
+ def retrieve_refund_transaction(refund_id)
11
+ call_paypal("/v1/payments/refund/#{refund_id}")
12
+ return (@status.last=='200') && (['pending', 'completed', 'refunded', 'partially_refunded'].index(@hash.last[:state])>0)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ #################################
2
+ ### Retrieve Sale Transaction ###
3
+ #################################
4
+
5
+ class Paypkg
6
+ # Use the sale_id to look up the sale.
7
+ # @param sale_id [String] Required.
8
+ # @return [json String] The same json string retured when
9
+ # the sale was orginally completed.
10
+ def retrieve_sale_transaction(sale_id)
11
+ call_paypal("/v1/payments/sale/#{sale_id}")
12
+ return (@status.last=='200') && (['pending', 'completed', 'refunded', 'partially_refunded'].index(@hash.last[:state])>0)
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ #########################
2
+ ### Store Credit Card ###
3
+ #########################
4
+
5
+ class Paypkg
6
+ # Store a credit card on the PalPal servers (in the vault, as the refer to it).
7
+ # A card so stored can be used later to make charges, as long as the expire
8
+ # date has not elapsed. Once the expire date elapses, eventually PayPal
9
+ # will automatically remove the card. The card vault_id will be returned
10
+ # from this call: you must save it in your database.
11
+ # @param type [String] Required.
12
+ # @param number [String] Required.
13
+ # @param expire_month [Numeric] Required.
14
+ # @param expire_year [Numeric] Required.
15
+ # @param cvv2 [String] Required.
16
+ # @param first_name [String] Required.
17
+ # @param last_name [String] Required.
18
+ # @param line1 [String] Required.
19
+ # @param line2 [String] Optional. To omit this parameter, use nil.
20
+ # @param city [String] Required.
21
+ # @param state [String] Required.
22
+ # @param postal_code [String] Required.
23
+ # @param country_code [String] Required.
24
+ # @return [String} The vault_id: you must save this in your database!
25
+ def store_credit_card(type, number, expire_month, expire_year, cvv2, first_name,
26
+ last_name, line1, line2, city, state, postal_code, country_code, payer_id)
27
+ json = "{
28
+ 'payer_id':'#{payer_id}',
29
+ 'type':'#{type}',
30
+ 'number':'#{number}',
31
+ 'expire_month':'#{expire_month}',
32
+ 'expire_year':'#{expire_year}',
33
+ 'cvv2':'#{cvv2}',
34
+ 'first_name':'#{first_name}',
35
+ 'last_name':'#{last_name}',
36
+ 'billing_address':{
37
+ 'line1':'#{line1}',\n"
38
+ json << " 'line2':'#{line2}',\n" if line2
39
+ json << " 'city':'#{city}',
40
+ 'state':'#{state}',
41
+ 'postal_code':'#{postal_code}',
42
+ 'country_code':'#{country_code}'
43
+ }
44
+ }"
45
+ call_paypal("/v1/vault/credit-card", json)
46
+ return (@status.last=='201') && (@hash.last[:state]=='ok')
47
+ end
48
+ end
@@ -0,0 +1,71 @@
1
+ ############################
2
+ ### Validate Credit Card ###
3
+ ############################
4
+
5
+ class Paypkg
6
+ # Validate a credit card by charging $0.01, then voiding the authorization.
7
+ # The reason for doing this is to validate that an actual charge can be made
8
+ # to the card. If you just attempt to store the card, and you get a valid
9
+ # response, it just means that the card data had no syntactical errors,
10
+ # not that you could ACTUALLY charge the card.
11
+ # @param type [String] Required.
12
+ # @param number [String] Required.
13
+ # @param expire_month [Numeric] Required.
14
+ # @param expire_year [Numeric] Required.
15
+ # @param cvv2 [String] Required.
16
+ # @param first_name [String] Required.
17
+ # @param last_name [String] Required.
18
+ # @param line1 [String] Required if any other address fields are present.
19
+ # @param city [String] Required if any other address fields are present.
20
+ # @param state [String] Required if any other address fields are present.
21
+ # @param postal_code [String] Required if any other address fields are present.
22
+ # @param country_code [String] Required if any other address fields are present.
23
+ def validate_credit_card(type, number, expire_month, expire_year, cvv2, \
24
+ first_name, last_name, line1, city, state, postal_code, country_code)
25
+ data = ""
26
+ data << "{
27
+ 'intent':'authorize',
28
+ 'payer':{
29
+ 'payment_method':'credit_card',
30
+ 'funding_instruments':[
31
+ {
32
+ 'credit_card':{
33
+ 'number':'#{number}',
34
+ 'type':'#{type}',
35
+ 'expire_month':#{expire_month},
36
+ 'expire_year':#{expire_year},
37
+ 'cvv2':'#{cvv2}',
38
+ 'first_name':'#{first_name}',
39
+ 'last_name':'#{last_name}',"
40
+ if (!line1.empty?) || (!city.empty?) || (!state.empty?) || (!postal_code.empty?) || (!country_code.empty?)
41
+ data << " 'billing_address':{
42
+ 'line1':'#{line1}',
43
+ 'city':'#{city}',
44
+ 'state':'#{state}',
45
+ 'postal_code':'#{postal_code}',
46
+ 'country_code':'#{country_code}'
47
+ }"
48
+ end
49
+ data << " }
50
+ }
51
+ ]
52
+ },
53
+ 'transactions':[
54
+ {
55
+ 'amount':{
56
+ 'total':0.01,
57
+ 'currency':'USD'
58
+ },
59
+ 'description':'This is a validation transaction.'
60
+ }
61
+ ]
62
+ }"
63
+ call_paypal("/v1/payments/payment", data)
64
+ if (@status.last=='201') && (@hash.last[:state]=='approved')
65
+ link = @hash.last[:transactions][0][:related_resources][0][:authorization][:links].select{|link| link[:rel]=='void'}
66
+ call_paypal(link[0][:href], nil, :method => :post, :reset => :no)
67
+ return true if (@status.last=='200') && (@hash.last[:state]=='voided')
68
+ end
69
+ return false
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Version
2
+ VERSION = "0.1.1"
3
+ end