PayPalPlugin - Updated for 1.6.0 and JavaScript global namespace cleanup

This commit is contained in:
shazron
2012-04-09 12:41:43 -07:00
parent 18da0b11e5
commit 76f79f96e1
3 changed files with 74 additions and 29 deletions

View File

@@ -5,17 +5,27 @@ by Shazron Abdullah
## Adding the Plugin to your project ##
Using this plugin requires [iPhone PhoneGap](http://github.com/phonegap/phonegap-iphone) and the PayPal Mobile Payments Library. The PayPal Mobile Payments Library can be downloaded [here](https://www.x.com/community/ppx/xspaces/mobile/mep).
Using this plugin requires [Cordova](http://github.com/apache/incubator-cordova-ios) and the PayPal Mobile Payments Library. The PayPal Mobile Payments Library can be downloaded [here](https://www.x.com/community/ppx/xspaces/mobile/mep).
1. Make sure your PhoneGap Xcode project has been [updated for the iOS 4 SDK](http://wiki.phonegap.com/Upgrade-your-PhoneGap-Xcode-Template-for-iOS-4)
1. Make sure your Cordova Xcode project has been [updated for Cordova 1.6.0](https://github.com/apache/incubator-cordova-ios/blob/master/guides/Cordova%20Plugin%20Upgrade%20Guide.md)
2. Add the "Paypal Mobile Payments" folder to your project (put in a suitable location under your project, then drag and drop it in)
3. Add the .h and .m files to your Plugins folder in your project
4. Add the .js files to your "www" folder on disk, and add reference(s) to the .js files as <link> tags in your html file(s)
5. See the sample project "PayPalPlugin-Host" for an example use (examine the code in www/index.html)
6. Make sure you check the "RELEASE NOTES" section below!
3. Add the .h and .m files to your Plugins folder in your project (as a Group "yellow folder" not a Reference "blue folder")
4. Add the .js files to your "www" folder on disk, and add reference(s) to the .js files as &lt;script&gt; tags in your html file(s)
5. In **Cordova.plist** (1.5.0 or greater) or **PhoneGap.plist** (1.4.1 or lesser), under the **Plugins** section, add an idential key and value of **"SAiOSPaypalPlugin"**
5. Make sure you check the **"RELEASE NOTES"** section below!
## RELEASE NOTES ##
### 20120409 ###
- Changed license to Apache 2.0 License
- Updated for Cordova 1.6.0 (backwards compatible to earlier versions as well)
- wrapped object in function closure (to prevent pollution of the global namespace)
- global constants are now namespaced under the global PayPal object
e.g if it was **PayPalPaymentType.DONATION** before, it's **PayPal.PaymentType.Donation** now
and it is also accessible under **window.plugins.paypal.PaymentType.Donation**
### 20101008 ###
* Initial release
* By default the PayPalPlugin-Host runs in ENV_NONE (offline) with a dummy PayPal ID. Change these in the Objective-C source (the code warns you in the Console)
@@ -34,13 +44,17 @@ The latest code (my fork) will always be [here](http://github.com/shazron/phoneg
## LICENSE ##
The MIT License
Copyright 2012 Shazron Abdullah
Copyright (c) 2010 Shazron Abdullah
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
http://www.apache.org/licenses/LICENSE-2.0
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -1,13 +1,23 @@
// //////////////////////////////////////
// Paypal PhoneGap Plugin
// Paypal Cordova Plugin
// by Shazron Abdullah
//
// Oct 8th 2010
// Initial implementation
// Apr 9th 2012
// Updated for Cordova 1.6.0, wrapped in function closure, constants are namespaced under the global PayPal object
// e.g if it was PayPalPaymentType.DONATION before, it's PayPal.PaymentType.Donation now
// and it is also accessible under window.plugins.paypal.PaymentType.Donation
//
// /////////////////////////
var PayPal = (function() {
// /////////////////////////
/*
* buttonType (unused currently)
*/
var PayPalButtonType = {
SAiOSPaypalPlugin.ButtonType = {
'BUTTON_68x24' : 0,
'BUTTON_68x33' : 1,
'BUTTON_118x24': 2,
@@ -21,7 +31,7 @@ var PayPalButtonType = {
/*
* PaymentType for window.plugins.prepare
*/
var PayPalPaymentType =
SAiOSPaypalPlugin.PaymentType =
{
'HARD_GOODS': 0,
'SERVICE' : 1,
@@ -32,7 +42,7 @@ var PayPalPaymentType =
/*
* errorType for PaypalPaymentEvent.Failed
*/
var PayPalFailureType =
SAiOSPaypalPlugin.FailureType =
{
'SYSTEM_ERROR' : 0,
'RECIPIENT_ERROR' : 1,
@@ -43,7 +53,7 @@ var PayPalFailureType =
/*
* Events to listen to after a user touches the payment button
*/
var PaypalPaymentEvent =
SAiOSPaypalPlugin.PaymentEvent =
{
/**
* Listen for this event to signify Paypal payment success. The event object will have these properties:
@@ -62,6 +72,9 @@ var PaypalPaymentEvent =
Failed : "PaypalPaymentEvent.Failed"
};
// get local ref to global PhoneGap/Cordova/cordova object for exec function
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
/**
* Constructor
*/
@@ -75,7 +88,7 @@ function SAiOSPaypalPlugin()
*/
SAiOSPaypalPlugin.prototype.prepare = function(paymentType)
{
PhoneGap.exec("SAiOSPaypalPlugin.prepare", paymentType);
cordovaRef.exec("SAiOSPaypalPlugin.prepare", paymentType);
}
/**
@@ -83,7 +96,7 @@ SAiOSPaypalPlugin.prototype.prepare = function(paymentType)
*/
SAiOSPaypalPlugin.prototype.pay = function()
{
PhoneGap.exec("SAiOSPaypalPlugin.pay");
cordovaRef.exec("SAiOSPaypalPlugin.pay");
}
/**
@@ -97,11 +110,9 @@ SAiOSPaypalPlugin.prototype.pay = function()
*/
SAiOSPaypalPlugin.prototype.setPaymentInfo = function(paymentProperties)
{
PhoneGap.exec("SAiOSPaypalPlugin.setPaymentInfo", paymentProperties);
cordovaRef.exec("SAiOSPaypalPlugin.setPaymentInfo", paymentProperties);
}
/**
* Install function
*/
@@ -116,6 +127,26 @@ SAiOSPaypalPlugin.install = function()
}
/**
* Add to PhoneGap constructor
* Add to Cordova constructor
*/
PhoneGap.addConstructor(SAiOSPaypalPlugin.install);
if (cordovaRef && cordovaRef.addConstructor) {
cordovaRef.addConstructor(SAiOSPaypalPlugin.install);
} else {
console.log("PayPal Cordova Plugin could not be installed.");
return null;
}
/**
* Return constants
*/
return {
ButtonType : SAiOSPaypalPlugin.ButtonType,
PaymentType : SAiOSPaypalPlugin.PaymentType,
FailureType : SAiOSPaypalPlugin.FailureType,
PaymentEvent: SAiOSPaypalPlugin.PaymentEvent
};
// /////////////////////////
})();
// /////////////////////////

View File

@@ -12,7 +12,7 @@
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
-->
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-1.6.0.js"></script>
<script type="text/javascript" charset="utf-8" src="SAiOSPaypalPlugin.js"></script>
<script type="text/javascript" charset="utf-8">
@@ -37,11 +37,11 @@
try {
// do your thing!
document.addEventListener(PaypalPaymentEvent.Success, onPaymentSuccess,false);
document.addEventListener(PaypalPaymentEvent.Canceled, onPaymentCanceled,false);
document.addEventListener(PaypalPaymentEvent.Failed, onPaymentFailed,false);
document.addEventListener(PayPal.PaymentEvent.Success, onPaymentSuccess,false);
document.addEventListener(PayPal.PaymentEvent.Canceled, onPaymentCanceled,false);
document.addEventListener(PayPal.PaymentEvent.Failed, onPaymentFailed,false);
window.plugins.paypal.prepare(PayPalPaymentType.DONATION);
window.plugins.paypal.prepare(PayPal.PaymentType.DONATION);
window.plugins.paypal.setPaymentInfo({
paymentCurrency : "USD",
paymentAmount : "10.00",