Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. <?php
  2. /**
  3. * @brief PayPal Gateway
  4. * @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
  5. * @copyright (c) Invision Power Services, Inc.
  6. * @license https://www.invisioncommunity.com/legal/standards/
  7. * @package Invision Community
  8. * @subpackage Nexus
  9. * @since 07 Mar 2014
  10. */
  11.  
  12. require_once '../../../../init.php';
  13. \IPS\Session\Front::i();
  14.  
  15. /* Load Transaction */
  16. try
  17. {
  18. $transaction = \IPS\nexus\Transaction::load( \IPS\Request::i()->nexusTransactionId );
  19.  
  20. if ( $transaction->status !== \IPS\nexus\Transaction::STATUS_PENDING )
  21. {
  22. throw new \OutofRangeException;
  23. }
  24. }
  25. catch ( \OutOfRangeException $e )
  26. {
  27. \IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=nexus&module=checkout&controller=checkout&do=transaction&id=&t=" . \IPS\Request::i()->nexusTransactionId, 'front', 'nexus_checkout', \IPS\Settings::i()->nexus_https ) );
  28. }
  29.  
  30. /* Process */
  31. try
  32. {
  33. /* Check fraud rules */
  34. $maxMind = NULL;
  35. if ( \IPS\Settings::i()->maxmind_key and ( !\IPS\Settings::i()->maxmind_gateways or \IPS\Settings::i()->maxmind_gateways == '*' or in_array( $transaction->method->id, explode( ',', \IPS\Settings::i()->maxmind_gateways ) ) ) )
  36. {
  37. $maxMind = new \IPS\nexus\Fraud\MaxMind\Request;
  38. $maxMind->setTransaction( $transaction );
  39. $maxMind->setTransactionType( 'paypal' );
  40. }
  41. $fraudResult = $transaction->runFraudCheck( $maxMind );
  42. if ( $fraudResult === \IPS\nexus\Transaction::STATUS_REFUSED )
  43. {
  44. $transaction->executeFraudAction( $fraudResult, FALSE );
  45. $transaction->sendNotification();
  46. \IPS\Output::i()->redirect( $transaction->url() );
  47. }
  48.  
  49. /* Billing Agreement */
  50. if ( isset( \IPS\Request::i()->billingAgreement ) )
  51. {
  52. /* Execute */
  53. $response = $transaction->method->api( "payments/billing-agreements/" . \IPS\Request::i()->token . "/agreement-execute" );
  54. $agreementId = $response['id'];
  55. if ( isset( $response['payer'] ) and isset( $response['payer']['status'] ) )
  56. {
  57. $extra = $transaction->extra;
  58. $extra['verified'] = $response['payer']['status'];
  59. $transaction->extra = $extra;
  60. }
  61.  
  62. /* Create Billing Agreement */
  63. $billingAgreement = new \IPS\nexus\Gateway\PayPal\BillingAgreement;
  64. $billingAgreement->gw_id = $agreementId;
  65. $billingAgreement->method = $transaction->method;
  66. $billingAgreement->member = $transaction->member;
  67. $billingAgreement->started = \IPS\DateTime::create();
  68. $billingAgreement->next_cycle = \IPS\DateTime::create()->add( new \DateInterval( 'P' . $response['plan']['payment_definitions'][0]['frequency_interval'] . mb_substr( $response['plan']['payment_definitions'][0]['frequency'], 0, 1 ) ) );
  69. $billingAgreement->save();
  70. $transaction->billing_agreement = $billingAgreement;
  71. $transaction->save();
  72.  
  73. /* Get the initial setup transaction if possible */
  74. $haveInitialTransaction = FALSE;
  75. $transactions = $transaction->method->api( "payments/billing-agreements/{$agreementId}/transactions?start_date=" . date( 'Y-m-d', time() - 86400 ) . '&end_date=' . date( 'Y-m-d' ), NULL, 'get' );
  76. foreach ( $transactions['agreement_transaction_list'] as $t )
  77. {
  78. if ( $t['status'] == 'Completed' )
  79. {
  80. $transaction->gw_id = $t['transaction_id'];
  81. $transaction->save();
  82.  
  83. if ( $fraudResult and $fraudResult !== \IPS\nexus\Transaction::STATUS_PAID )
  84. {
  85. $transaction->executeFraudAction( $fraudResult, TRUE );
  86. }
  87. else
  88. {
  89. $transaction->member->log( 'transaction', array(
  90. 'type' => 'paid',
  91. 'status' => $transaction::STATUS_PAID,
  92. 'id' => $transaction->id,
  93. 'invoice_id' => $transaction->invoice->id,
  94. 'invoice_title' => $transaction->invoice->title,
  95. ) );
  96.  
  97. $memberJustCreated = $transaction->approve();
  98. if ( $memberJustCreated )
  99. {
  100. \IPS\Session::i()->setMember( $memberJustCreated );
  101. \IPS\Member\Device::loadOrCreate( $memberJustCreated, FALSE )->updateAfterAuthentication( NULL );
  102. }
  103. }
  104.  
  105. $transaction->sendNotification();
  106. \IPS\Output::i()->redirect( $transaction->url() );
  107. }
  108. }
  109.  
  110. /* If it hasn't been processed yet (PayPal is a bit flakey), we need to try again in a few minutes */
  111. $transaction->status = \IPS\nexus\Transaction::STATUS_GATEWAY_PENDING;
  112. $transaction->save();
  113. $transaction->sendNotification();
  114. \IPS\Db::i()->update( 'core_tasks', array( 'enabled' => 1 ), "`key`='gatewayPending'" );
  115. \IPS\Output::i()->redirect( $transaction->url() );
  116. }
  117.  
  118. /* Normal */
  119. else
  120. {
  121. $response = $transaction->method->api( "payments/payment/{$transaction->gw_id}/execute", array(
  122. 'payer_id' => \IPS\Request::i()->PayerID,
  123. ) );
  124. $transaction->gw_id = $response['transactions'][0]['related_resources'][0]['authorization']['id']; // Was previously a payment ID. This sets it to the actual transaction ID for the authorization. At capture, it will be updated again to the capture transaction ID
  125. $transaction->auth = \IPS\DateTime::ts( strtotime( $response['transactions'][0]['related_resources'][0]['authorization']['valid_until'] ) );
  126. if ( isset( $response['payer'] ) and isset( $response['payer']['status'] ) )
  127. {
  128. $extra = $transaction->extra;
  129. $extra['verified'] = $response['payer']['status'];
  130. $transaction->extra = $extra;
  131. }
  132. $transaction->save();
  133. }
  134.  
  135. /* Capture */
  136. if ( $fraudResult )
  137. {
  138. $transaction->executeFraudAction( $fraudResult, TRUE );
  139. }
  140. if ( !$fraudResult or $fraudResult === \IPS\nexus\Transaction::STATUS_PAID )
  141. {
  142. $memberJustCreated = $transaction->captureAndApprove();
  143. if ( $memberJustCreated )
  144. {
  145. \IPS\Session::i()->setMember( $memberJustCreated );
  146. \IPS\Member\Device::loadOrCreate( $memberJustCreated, FALSE )->updateAfterAuthentication( NULL );
  147. }
  148. }
  149. $transaction->sendNotification();
  150.  
  151. /* Redirect */
  152. \IPS\Output::i()->redirect( $transaction->url() );
  153. }
  154. catch ( \Exception $e )
  155. {
  156. \IPS\Log::log( $e, 'paypal' );
  157. \IPS\Output::i()->redirect( $transaction->invoice->checkoutUrl()->setQueryString( array( '_step' => 'checkout_pay', 'err' => $e->getMessage() ) ) );
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement