-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-Paid.ps1
176 lines (124 loc) · 13.1 KB
/
Get-Paid.ps1
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function Get-Paid
{
<#
.Synopsis
Gets you paid
.Description
Handles getting payments or payment confirmation information.
.Example
# A Sample Charge with the Stripe API
Get-Paid -StripeKey sk_test_aElHsSizhc8XC0uESpNC1t64 -Currency usd -Amount 400 -CardNumber 4242424242424242 -ExpirationMonth 12 -ExpirationYear 2014 -CardVerficationCode 123
.Link
https://www.youtube.com/watch?v=XPS60qPh3vc
#>
[CmdletBinding(DefaultParameterSetName='Stripe')]
[OutputType([PSObject])]
param(
# The charge amount
[Parameter(Mandatory=$true,ParameterSetName='Stripe',Position=0, ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory=$true,ParameterSetName='StripeToken',Position=0, ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory=$true,ParameterSetName='StripeCardSwipe',Position=0, ValueFromPipelineByPropertyName=$true)]
[Double]
$Amount,
# The charge currency
[Parameter(Mandatory=$true,ParameterSetName='Stripe',Position=1, ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory=$true,ParameterSetName='StripeToken',Position=1, ValueFromPipelineByPropertyName=$true)]
[Parameter(Mandatory=$true,ParameterSetName='StripeCardSwipe',Position=0, ValueFromPipelineByPropertyName=$true)]
[string]
$Currency,
# The Stripe Token. This is used to process payments that use the [Stripe Checkout form](https://stripe.com/docs/checkout)
[Parameter(Mandatory=$true,ParameterSetName='StripeToken',Position=2, ValueFromPipelineByPropertyName=$true)]
[string]
$StripeToken,
# The card number
[Parameter(Mandatory=$true,ParameterSetName='Stripe',Position=2, ValueFromPipelineByPropertyName=$true)]
[string]
$CardNumber,
# The Expiration Month
[Parameter(Mandatory=$true,ParameterSetName='Stripe',Position=3, ValueFromPipelineByPropertyName=$true)]
[string]
$ExpirationMonth,
# The Expiration Year
[Parameter(Mandatory=$true,ParameterSetName='Stripe',Position=4, ValueFromPipelineByPropertyName=$true)]
[string]
$ExpirationYear,
# The Stripe Key. If provided once, it doesn't need to be provided again.
[Parameter(ParameterSetName='Stripe')]
[Parameter(ParameterSetName='StripeToken')]
[Parameter(ParameterSetName='StripeCardSwipe')]
[string]
$StripeKey,
# The Secure setting containing the Stripe Key. If provided once, it doesn't need to be provided again.
[Parameter(ParameterSetName='Stripe')]
[Parameter(ParameterSetName='StripeToken')]
[Parameter(ParameterSetName='StripeCardSwipe')]
[string]
$StripeKeySetting,
# The direct input from a card reader, as text.
[Parameter(Mandatory=$true,Position=0,ParameterSetName='StripeCardSwipe')]
[Security.SecureString]
$CardSwipeData,
# The PayPal Instant Payment Notification Info
[Parameter(Mandatory=$true,ParameterSetName='ConfirmPayPalIPN')]
[string]
$IPNInfo
)
begin {
#region Declare script block to get the stripe cred
$getStripeCred = {
if (-not $script:CachedStripeCred -or $StripeKey -or $StripeKeySetting) {
if (-not $StripeKey -and $StripeKeySetting) {
$StripeKey = Get-SecureSetting -Name $StripeKeySetting -ValueOnly
}
if (-not $StripeKey) {
Write-Error "Must provide a stripe key"
return
}
$script:CachedStripeCred = new-object Management.Automation.PSCredential "$StripeKey", (ConvertTo-SecureString -AsPlainText -Force -String " ")
}
}.ToString()
#endregion Declare script block to get the stripe cred
}
process {
if ($PSCmdlet.ParameterSetName -eq 'Stripe') {
Invoke-Expression $getStripeCred
$data = "amount=$($Amount * 100)",
"currency=$Currency",
"card[number]=$CardNumber",
"card[exp_month]=$ExpirationMonth",
"card[exp_year]=$ExpirationYear"<#,
"card[cvc]=$CardVerficationCode" #>
Get-Web -Url https://api.stripe.com/v1/charges -WebCredential $script:CachedStripeCred -Data $data -AsJson
} elseif ($PSCmdlet.ParameterSetName -eq 'StripeToken') {
Invoke-Expression $getStripeCred
$data = "amount=$($Amount * 100)",
"currency=$Currency",
"card=$CardToken"
Get-Web -Url https://api.stripe.com/v1/charges -WebCredential $script:CachedStripeCred -Data $data -AsJson
} elseif ($psCmdlet.ParameterSetName -eq 'StripeCardSwipe') {
Invoke-Expression $getStripeCred
$tempCred = New-Object Management.Automation.PSCredential "temp", $CardSwipeData
$actualSwipe = $tempCred.GetNetworkCredential().Password
$cardParts = $actualSwipe -split '\^'
$cardNumber = $cardParts[0].TrimStart("%B")
$cardExpirationYear = [datetime]::Now.Year.ToString().Substring(0, 2) + ($cardParts[2].Substring(0, 2) -as [uint32])
$cardExpirationMonth = $cardParts[2].Substring(2, 2) -as [uint32]
Get-Paid -Amount $Amount -Currency $Currency -StripeKey $script:CachedStripeCred.UserName -CardNumber $CardNumber -ExpirationMonth $cardExpirationMonth -ExpirationYear $cardExpirationYear
# The first part may have the card number.
} elseif ($PSCmdlet.ParameterSetName -eq 'ConfirmPayPalIPN') {
$req = [Net.HttpWebRequest]::Create("https://www.paypal.com/cgi-bin/webscr")
$req.Method = "POST";
$req.ContentType = "application/x-www-form-urlencoded"
$strRequest = $IPNInfo +
"&cmd=_notify-validate";
$req.ContentLength = $strRequest.Length;
$streamOut = New-Object IO.StreamWriter $req.GetRequestStream()
$streamOut.Write($strRequest);
$streamOut.Close();
$streamIn = New-Object IO.StreamReader($req.GetResponse().GetResponseStream());
$paypalResponse = $streamIn.ReadToEnd();
$null = $paypalResponse
$streamIn.Close();
}
}
}