-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.php
110 lines (88 loc) · 3.76 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Get the PayPal Digital Goods class definitions from the PayPal Digital Goods PHP Library.
*/
require_once( 'lib/paypal-digital-goods/paypal-digital-goods.class.php' );
require_once( 'lib/paypal-digital-goods/paypal-subscription.class.php' );
require_once( 'lib/paypal-digital-goods/paypal-purchase.class.php' );
/**
* A central function for settings the credentials for both subscription & purchase
* objects with the PayPal_Digital_Goods_Configuration registry class.
*/
function set_credentials() {
/*
PayPal_Digital_Goods_Configuration::username( 'your_api_username' );
PayPal_Digital_Goods_Configuration::password( 'your_api_password' );
PayPal_Digital_Goods_Configuration::signature( 'your_api_signature' );
*/
PayPal_Digital_Goods_Configuration::username( 'digita_1308916325_biz_api1.gmail.com' );
PayPal_Digital_Goods_Configuration::password( '1308916362' );
PayPal_Digital_Goods_Configuration::signature( 'AFnwAcqRkyW0yPYgkjqTkIGqPbSfAyVFbnFAjXCRltVZFzlJyi2.HbxW' );
PayPal_Digital_Goods_Configuration::return_url( get_script_uri( 'return.php?paypal=paid' ) );
PayPal_Digital_Goods_Configuration::cancel_url( get_script_uri( 'return.php?paypal=cancel' ) );
PayPal_Digital_Goods_Configuration::business_name( 'Demo Store' );
PayPal_Digital_Goods_Configuration::notify_url( get_script_uri( 'return.php?paypal=notify' ) );
// Uncomment the line below to switch to the live PayPal site
//PayPal_Digital_Goods_Configuration::environment( 'live' );
if( PayPal_Digital_Goods_Configuration::username() == 'your_api_username' || PayPal_Digital_Goods_Configuration::password() == 'your_api_password' || PayPal_Digital_Goods_Configuration::signature() == 'your_api_signature' )
exit( 'You must set your API credentials in ' . __FILE__ . ' for this example to work.' );
}
/**
* Creates and returns a PayPal DG Purchase Object
*/
function create_example_purchase() {
set_credentials();
$purchase_details = array(
'name' => 'Digital Good Purchase Example',
'description' => 'Example Digital Good Purchase',
'amount' => '15.50',
'tax_amount' => '2.50',
'items' => array(
array( // First item
'item_name' => 'First item name',
'item_description' => 'This is a description of the first item in the cart, it costs $9.00',
'item_amount' => '10.00',
'item_tax' => '1.00',
'item_quantity' => 1,
'item_number' => 'XF100',
),
array( // Second item
'item_name' => 'Second Item',
'item_description' => 'This is a description of the SECOND item in the cart, it costs $1.00 but there are 3 of them.',
'item_amount' => '1.00',
'item_tax' => '0.50',
'item_quantity' => 3,
'item_number' => 'XJ100',
),
)
);
return new PayPal_Purchase( $purchase_details );
}
/**
* Creates and returns a PayPal DG Subscription Object
*/
function create_example_subscription() {
set_credentials();
$subscription_details = array(
'description' => 'Example Subscription: $10 sign-up fee then $2/week for the next four weeks.',
'initial_amount' => '10.00',
'amount' => '2.00',
'period' => 'Week',
'frequency' => '1',
'total_cycles' => '4',
);
return new PayPal_Subscription( $subscription_details );
}
/**
* Helper function to get the URI for the script
*/
function get_script_uri( $script = 'index.php' ){
// IIS Fix
if( empty( $_SERVER['REQUEST_URI'] ) )
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
// Strip off query string
$url = preg_replace( '/\?.*$/', '', $_SERVER['REQUEST_URI'] );
//$url = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';
$url = 'http://'.$_SERVER['HTTP_HOST'].implode( '/', ( explode( '/', $_SERVER['REQUEST_URI'], -1 ) ) ) . '/';
return $url . $script;
}